1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5'use strict';
6
7/**
8 * Dialog to confirm the operation for conflicted file operations.
9 *
10 * @param {HTMLElement} parentNode Node to be parent for this dialog.
11 * @constructor
12 * @extends {FileManagerDialogBase}
13 */
14function ConflictDialog(parentNode) {
15  FileManagerDialogBase.call(this, parentNode);
16
17  /**
18   * Callback to be called when the showing task is completed. The first
19   * argument is which button is pressed. The second argument is whether to
20   * apply all or not.
21   *
22   * @type {function(ConflictDialog.Result, boolean)}
23   * @private
24   */
25  this.callback_ = null;
26
27  /**
28   * Checkbox to specify whether to apply the selection to all entries or not.
29   * @type {HTMLElement}
30   * @private
31   */
32  this.applyAllCheckbox_ = parentNode.ownerDocument.createElement('input');
33  this.applyAllCheckbox_.id = 'conflict-confirm-dialog-apply-all-checkbox';
34  this.applyAllCheckbox_.type = 'checkbox';
35
36  // Apply all line.
37  var applyAllLabel = parentNode.ownerDocument.createElement('label');
38  applyAllLabel.textContent = str('CONFLICT_DIALOG_APPLY_TO_ALL');
39  applyAllLabel.setAttribute('for', this.applyAllCheckbox_.id);
40
41  /**
42   * Element of the keep both button.
43   * @type {HTMLElement}
44   * @private
45   */
46  this.keepBothButton_ = parentNode.ownerDocument.createElement('button');
47  this.keepBothButton_.textContent = str('CONFLICT_DIALOG_KEEP_BOTH');
48  this.keepBothButton_.addEventListener(
49      'click',
50      this.hideWithResult_.bind(this, ConflictDialog.Result.KEEP_BOTH));
51
52  /**
53   * Element of the replace button.
54   * @type {HTMLElement}
55   * @private
56   */
57  this.replaceButton_ = parentNode.ownerDocument.createElement('button');
58  this.replaceButton_.textContent = str('CONFLICT_DIALOG_REPLACE');
59  this.replaceButton_.addEventListener(
60      'click',
61      this.hideWithResult_.bind(this, ConflictDialog.Result.REPLACE));
62
63  // Buttons line.
64  var buttons = this.okButton_.parentNode;
65  buttons.insertBefore(this.applyAllCheckbox_, this.okButton_);
66  buttons.insertBefore(applyAllLabel, this.okButton_);
67  buttons.replaceChild(this.keepBothButton_, this.okButton_);
68  buttons.appendChild(this.replaceButton_);
69
70  // Frame
71  this.frame_.id = 'conflict-confirm-dialog';
72}
73
74/**
75 * Result of conflict confirm dialogs.
76 * @enum {string}
77 * @const
78 */
79ConflictDialog.Result = Object.freeze({
80  KEEP_BOTH: 'keepBoth',
81  CANCEL: 'cancel',
82  REPLACE: 'replace'
83});
84
85ConflictDialog.prototype = {
86  __proto__: FileManagerDialogBase.prototype
87};
88
89/**
90 * Shows the conflict confirm dialog.
91 *
92 * @param {string} fileName Filename that is conflicted.
93 * @param {function(ConflictDialog.Result, boolean)} callback Complete
94 *     callback. See also ConflictDialog#callback_.
95 * @return {boolean} True if the dialog can show successfully. False if the
96 *     dialog failed to show due to an existing dialog.
97 */
98ConflictDialog.prototype.show = function(fileName, callback) {
99  if (this.callback_)
100    return false;
101
102  this.callback_ = callback;
103  FileManagerDialogBase.prototype.showOkCancelDialog.call(
104      this,
105      '', // We dont't show the title for the dialog.
106      strf('CONFLICT_DIALOG_MESSAGE', fileName));
107  return true;
108};
109
110/**
111 * Handles cancellation.
112 * @param {Event} event Click event.
113 * @private
114 */
115ConflictDialog.prototype.onCancelClick_ = function(event) {
116  this.hideWithResult_(ConflictDialog.Result.CANCEL);
117};
118
119/**
120 * Hides the dialog box with the result.
121 * @param {ConflictDialog.Result} result Result.
122 * @private
123 */
124ConflictDialog.prototype.hideWithResult_ = function(result) {
125  this.hide(function() {
126    if (!this.callback_)
127      return;
128    this.callback_(result, this.applyAllCheckbox_.checked);
129    this.callback_ = null;
130    this.applyAllCheckbox_.checked = false;
131  }.bind(this));
132};
133