- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / apps_debugger / js / pack_item_overlay.js
1 // Copyright (c) 2012 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 cr.define('apps_dev_tool', function() {
6   'use strict';
7
8 /** const*/ var AppsDevTool = apps_dev_tool.AppsDevTool;
9
10   /**
11    * Hides the present overlay showing.
12    */
13   var hideOverlay = function() {
14     AppsDevTool.showOverlay(null);
15   };
16
17   /**
18    * PackItemOverlay class
19    * Encapsulated handling of the 'Pack Item' overlay page.
20    * @constructor
21    */
22   function PackItemOverlay() {}
23
24   cr.addSingletonGetter(PackItemOverlay);
25
26   PackItemOverlay.prototype = {
27     initializePage: function() {
28       var overlay = $('overlay');
29       cr.ui.overlay.setupOverlay(overlay);
30       cr.ui.overlay.globalInitialization();
31       overlay.addEventListener('cancelOverlay', hideOverlay.bind(this));
32
33       $('pack-item-dismiss').addEventListener('click',
34           hideOverlay.bind(this));
35       $('pack-item-commit').addEventListener('click',
36           this.handleCommit_.bind(this));
37       $('browse-private-key').addEventListener('click',
38           this.handleBrowsePrivateKey_.bind(this));
39     },
40
41     /**
42      * Handles a click on the pack button.
43      * @param {Event} e The click event.
44      * @private
45      */
46     handleCommit_: function(e) {
47       var itemPath = $('item-root-dir').value;
48       var privateKeyPath = $('item-private-key').value;
49       chrome.developerPrivate.packDirectory(
50           itemPath, privateKeyPath, 0, this.onCommit_);
51     },
52
53     /**
54      * Handles a commit on the pack request.
55      * @param {string} response Message returned by packing api.
56      * @private
57      */
58     onCommit_: function(response) {
59       if (response.status == 'SUCCESS')
60         PackItemOverlay.showSuccessMessage(response);
61       else if (response.status == 'ERROR')
62         PackItemOverlay.showError(response);
63       else
64         PackItemOverlay.showWarningMessage(response);
65     },
66
67     /**
68      * Handles the showing of the item private key file.
69      * @param {Event} e Change event.
70      * @private
71      */
72     handleBrowsePrivateKey_: function(e) {
73       chrome.developerPrivate.choosePath('FILE', 'PEM', function(filePath) {
74         $('item-private-key').value = filePath;
75       });
76     },
77   };
78
79   /**
80    * Wrap up the pack process by showing the success |message| and closing
81    * the overlay.
82    * @param {string} message The message to show to the user.
83    */
84   PackItemOverlay.showSuccessMessage = function(response) {
85     alertOverlay.setValues(
86         loadTimeData.getString('packExtensionOverlay'),
87         response.message,
88         loadTimeData.getString('ok'),
89         '',
90         hideOverlay,
91         null);
92     AppsDevTool.showOverlay($('alertOverlay'));
93   };
94
95   /**
96    * An alert overlay showing |message|, and upon acknowledgement, close
97    * the alert overlay and return to showing the PackItemOverlay.
98    * @param {string} message The message to show to the user.
99    */
100   PackItemOverlay.showError = function(response) {
101     alertOverlay.setValues(
102         loadTimeData.getString('packExtensionErrorTitle'),
103         response.message /* message returned by the packiing api */,
104         loadTimeData.getString('ok'),
105         '',
106         function() {
107           AppsDevTool.showOverlay($('packItemOverlay'));
108         },
109         null);
110     AppsDevTool.showOverlay($('alertOverlay'));
111   };
112
113   /**
114    * An alert overlay showing |message| as warning and proceeding after the
115    * user confirms the action.
116    * @param {response} response returned by the packItem API.
117    */
118   PackItemOverlay.showWarningMessage = function(response) {
119     alertOverlay.setValues(
120         loadTimeData.getString('packExtensionWarningTitle'),
121         response.message /* message returned by the packing api */,
122         loadTimeData.getString('packExtensionProceedAnyway'),
123         loadTimeData.getString('cancel'),
124         function() {
125           chrome.developerPrivate.packDirectory(
126               response.item_path,
127               response.pem_path,
128               response.override_flags,
129               PackItemOverlay.showSuccessMessage);
130           hideOverlay();
131         },
132         hideOverlay);
133     AppsDevTool.showOverlay($('alertOverlay'));
134   };
135
136   // Export
137   return {
138     PackItemOverlay: PackItemOverlay,
139   };
140 });