Updated application sources
[apps/web/sample/FileManager.git] / project / js / app.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global tizen, $, app, Ui, Model, Helpers, Config, Clipboard*/
19
20 var App = null;
21
22 (function () { // strict mode wrapper
23     'use strict';
24
25     /**
26      * Creates a new application object
27      *
28      * @class Application
29      * @constructor
30      */
31     App = function App() {
32     };
33
34     App.prototype = {
35         /**
36          * @type Array
37          */
38         requires: [
39             'js/app.config.js',
40             'js/app.model.js',
41             'js/app.ui.js',
42             'js/app.ui.templateManager.js',
43             'js/app.ui.templateManager.modifiers.js',
44             'js/app.systemIO.js',
45             'js/app.helpers.js',
46             'js/app.clipboard.js'
47         ],
48
49         /**
50          * @type Model
51          */
52         model: null,
53
54         /**
55          * @type Ui
56          */
57         ui: null,
58
59         /**
60          * @type Config
61          */
62         config: null,
63
64         /**
65          * @type SystemIO
66          */
67         systemIO: null,
68
69         /**
70          * @type Helpers
71          */
72         helpers: null,
73
74         /**
75          * @type {string}
76          */
77         currentPath: 'root',
78
79         /**
80          *
81          */
82         currentDirHandle: null,
83
84         /**
85          * @type {Clipboard}
86          */
87         clipboard: null,
88
89         /**
90          * Initialization
91          */
92         init: function App_init() {
93             this.config = new Config();
94             this.model = new Model();
95             this.ui = new Ui();
96             this.helpers = new Helpers();
97             this.clipboard = new Clipboard();
98
99             this.initUi();
100         },
101
102         /**
103          * UI initialization
104          */
105         initUi: function App_initUi() {
106             this.ui.init(this.model.getInternalStorages());
107         },
108
109         /**
110          * Displays media storages
111          */
112         displayStorages: function App_displayStorages() {
113             this.currentPath = '';
114             if (!this.ui.editMode) {
115                 this.ui.scrollContentTo(0);
116             }
117             this.ui.displayStorages(this.model.getInternalStorages());
118         },
119
120         /**
121          * Displays specified folder
122          * @param {string} path
123          * @param {bool} [refresh=false]
124          */
125         displayFolder: function App_displayFolder(path, refresh) {
126             var self = this;
127
128             refresh = refresh || false;
129
130             // get folder data and push into rendering method
131             this.model.getFolderData(path, function (dir, nodes) {
132                 // on success
133
134                 // update current path
135                 self.currentPath = path;
136
137                 // update current dir handle
138                 self.currentDirHandle = dir;
139
140                 // display folder UI
141                 if (refresh === undefined) {
142                     self.ui.scrollContentTo(0);
143                 }
144                 self.ui.displayFolder(path, nodes, refresh);
145             });
146         },
147
148         /**
149          * Opens specified file
150          * @params {string} uri File URI
151          */
152         openFile: function App_openFile(uri, fullUri) {
153             tizen.filesystem.resolve(
154                 fullUri,
155                 function (file) {
156                     this.model.openFile(fullUri);
157                 }.bind(this),
158                 function () {
159                     // file doesn't exists
160                     this.ui.alertPopup('File does no longer exist',
161                                this.refreshCurrentPage.bind(this, true));
162                 }.bind(this)
163             );
164         },
165
166         /**
167          * Displays parent location
168          */
169         goLevelUp: function App_goLevelUp() {
170             // split current path and get proper path for parent location
171             var newPath = this.currentPath.split('/').slice(0, -1).join('/');
172
173             if (newPath !== '') {
174                 this.displayFolder(newPath);
175             } else {
176                 this.displayStorages();
177             }
178         },
179
180         /**
181          * creates new dir in currently viewed dir
182          * @param {string} dirName
183          * @return {boolean} return status
184          */
185         createDir: function App_createDir(dirName, callback) {
186             var status = true;
187             if (this.currentDirPath !== '') {
188                 try {
189                     this.currentDirHandle.createDirectory(dirName);
190                 } catch (e) {
191                     status = false;
192                     app.ui.alertPopup(e.message, callback);
193                 }
194                 this.refreshCurrentPage();
195             } else {
196                 status = false;
197                 app.ui.alertPopup(
198                     'You can\'t create new nodes in the main view'
199                 );
200             }
201             return status;
202         },
203
204         /**
205          * Triggers refresh current page
206          */
207         refreshCurrentPage: function App_refreshCurrentPage(refresh) {
208             refresh = refresh || false;
209             if (this.currentPath === 'root') {
210                 this.ui.toggleInfoPopup();
211                 return;
212             }
213             if (this.currentPath !== '') {
214                 app.model.isStorageExists(this.currentPath,
215                     app.displayFolder.bind(app, app.model.currentPath, refresh),
216                     function () {
217                         app.ui.popupHardClose();
218                         app.displayStorages();
219                         setTimeout(
220                             function () {
221                                 app.ui.alertPopup(
222                                     'Path "' +
223                                         app.model.currentPath +
224                                         '" does no longer exist'
225                                 );
226                             },
227                             200
228                         );
229                     });
230             } else {
231                 this.displayStorages();
232             }
233         },
234
235         /**
236          * Deletes nodes with specified paths
237          * @param {string[]} nodes nodePaths
238          */
239         deleteNodes: function App_deleteNodes(nodes) {
240             this.model.deleteNodes(
241                 nodes,
242                 this.currentDirHandle,
243                 this.ui.removeNodeFromList.bind(this.ui)
244             );
245         },
246
247         /**
248          * @param {string[]} paths filepaths
249          * @param {number} mode clipboard mode
250          */
251         saveToClipboard: function App_saveToClipboard(paths, mode) {
252             var clipboardLength = this.clipboard.add(paths);
253
254             if (clipboardLength > 0) {
255                 this.clipboard.setMode(mode);
256                 app.ui.alertPopup('Data saved in clipboard');
257                 this.ui.clearTabbars();
258             } else {
259                 app.ui.alertPopup(
260                     'Error occured. Data has not been saved in clipboard'
261                 );
262             }
263
264             this.ui.refreshPasteActionBtn(this.clipboard.isEmpty());
265         },
266
267         /**
268          * Paste nodes from clipboard to current dir
269          */
270         pasteClipboard: function App_pasteClipboard() {
271             var clipboardData = this.clipboard.get();
272
273             if (clipboardData.length === 0) {
274                 app.ui.alertPopup('Clipboard is empty');
275                 return false;
276             }
277
278             if (this.clipboard.getMode() === this.clipboard.COPY_MODE_ID) {
279                 this.model.copyNodes(
280                     this.currentDirHandle,
281                     clipboardData,
282                     this.currentPath,
283                     this.onPasteClipboardSuccess.bind(this)
284                 );
285             } else {
286                 this.model.moveNodes(
287                     this.currentDirHandle,
288                     clipboardData,
289                     this.currentPath,
290                     this.onPasteClipboardSuccess.bind(this)
291                 );
292             }
293
294             this.ui.refreshPasteActionBtn(this.clipboard.isEmpty());
295
296             return true;
297         },
298
299         emptyClipboard: function App_emptyClipboard() {
300             return this.clipboard.get().length === 0;
301         },
302
303         /**
304          * Handler for paste clipboard success
305          */
306         onPasteClipboardSuccess: function App_onPasteClipboardSuccess() {
307             this.clipboard.clear();
308             this.refreshCurrentPage();
309         },
310
311         /**
312          * App exit
313          */
314         exit: function App_exit() {
315             tizen.application.getCurrentApplication().exit();
316         }
317     };
318 }());