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