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