- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / 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 '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  */
14 function 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   var applyAllLine = parentNode.ownerDocument.createElement('div');
41   applyAllLine.className = 'apply-all-line';
42   applyAllLine.appendChild(this.applyAllCheckbox_);
43   applyAllLine.appendChild(applyAllLabel);
44
45   /**
46    * Element of the keep both button.
47    * @type {HTMLElement}
48    * @private
49    */
50   this.keepBothButton_ = parentNode.ownerDocument.createElement('button');
51   this.keepBothButton_.textContent = str('CONFLICT_DIALOG_KEEP_BOTH');
52   this.keepBothButton_.addEventListener(
53       'click',
54       this.hideWithResult_.bind(this, ConflictDialog.Result.KEEP_BOTH));
55
56   /**
57    * Element of the replace button.
58    * @type {HTMLElement}
59    * @private
60    */
61   this.replaceButton_ = parentNode.ownerDocument.createElement('button');
62   this.replaceButton_.textContent = str('CONFLICT_DIALOG_REPLACE');
63   this.replaceButton_.addEventListener(
64       'click',
65       this.hideWithResult_.bind(this, ConflictDialog.Result.REPLACE));
66
67   // Buttons line.
68   var buttons = this.okButton_.parentNode;
69   buttons.replaceChild(this.keepBothButton_, this.okButton_);
70   buttons.appendChild(this.replaceButton_);
71
72   // Frame
73   this.frame_.id = 'conflict-confirm-dialog';
74   this.frame_.insertBefore(applyAllLine, buttons);
75 }
76
77 /**
78  * Result of conflict confirm dialogs.
79  * @enum {string}
80  * @const
81  */
82 ConflictDialog.Result = Object.freeze({
83   KEEP_BOTH: 'keepBoth',
84   CANCEL: 'cancel',
85   REPLACE: 'replace'
86 });
87
88 ConflictDialog.prototype = {
89   __proto__: FileManagerDialogBase.prototype
90 };
91
92 /**
93  * Shows the conflict confirm dialog.
94  *
95  * @param {string} fileName Filename that is conflicted.
96  * @param {function(ConflictDialog.Result, boolean)} callback Complete
97  *     callbak. See also ConflictDialog#callback_.
98  * @return {boolean} True if the dialog can show successfully. False if the
99  *     dialog failed to show due to an existing dialog.
100  */
101 ConflictDialog.prototype.show = function(fileName, callback) {
102   if (this.callback_)
103     return false;
104
105   this.callback_ = callback;
106   FileManagerDialogBase.prototype.showOkCancelDialog.call(
107       this,
108       str('CONFLICT_DIALOG_TITLE'),
109       strf('CONFLICT_DIALOG_MESSAGE', fileName));
110   return true;
111 };
112
113 /**
114  * Handles cancellation.
115  * @param {Event} event Click event.
116  * @private
117  */
118 ConflictDialog.prototype.onCancelClick_ = function(event) {
119   this.hideWithResult_(ConflictDialog.Result.CANCEL);
120 };
121
122 /**
123  * Hides the dialog box with the result.
124  * @param {ConflictDialog.Result} result Result.
125  * @private
126  */
127 ConflictDialog.prototype.hideWithResult_ = function(result) {
128   this.hide(function() {
129     if (!this.callback_)
130       return;
131     this.callback_(result, this.applyAllCheckbox_.checked);
132     this.callback_ = null;
133     this.applyAllCheckbox_.checked = false;
134   }.bind(this));
135 };