f6138c5f86ee25f1d8325f67b5a1d5af5881d113
[samples/web/FileManager.git] / 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: '',
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                         this.addEvents();
85                 },
86
87                 /**
88                  * UI initialization
89                  */
90                 initUi: function App_initUi() {
91                         this.ui.init(this.model.getInternalStorages());
92                 },
93
94                 /**
95                  * Add pages events
96                  */
97                 addEvents: function App_addEvents() {
98                         var self = this;
99                         document.addEventListener('webkitvisibilitychange', function () {
100                                 self.refreshCurrentPage();
101                         });
102                         // workaround: page refresh for on/off keyboard
103                         window.addEventListener('softkeyboardchange', function () {
104                                 $.mobile.activePage.page('refresh');
105                         });
106                 },
107
108                 /**
109                  * Displays media storages
110                  */
111                 displayStorages: function App_displayStorages() {
112                         this.currentPath = '';
113                         if (!this.ui.editMode) {
114                                 this.ui.scrollContentTo(0);
115                         }
116                         this.ui.displayStorages(this.model.getInternalStorages());
117                 },
118
119                 /**
120                  * Displays specified folder
121                  * @param {string} path
122                  * @param {bool} [refresh=false]
123                  */
124                 displayFolder: function App_displayFolder(path, refresh) {
125                         var self = this;
126
127                         refresh = refresh || false;
128
129                         // get folder data and push into rendering method
130                         this.model.getFolderData(path, function (dir, nodes) {
131                                 // on success
132
133                                 // update current path
134                                 self.currentPath = path;
135
136                                 // update current dir handle
137                                 self.currentDirHandle = dir;
138
139                                 // display folder UI
140                                 if (refresh === undefined) {
141                                         self.ui.scrollContentTo(0);
142                                 }
143                                 self.ui.displayFolder(path, nodes, refresh);
144                         });
145                 },
146
147                 /**
148                  * Opens specified file
149                  * @params {string} uri File URI
150                  */
151                 openFile: function App_openFile(uri, fullUri) {
152                         var ext = this.helpers.getFileExtension(uri),
153                                 mime = this.helpers.resolveMimeType(ext);
154
155                         if (mime !== '') {
156                                 this.model.openFile(fullUri, mime);
157                         } else {
158                                 console.error('Unsupported mime type for extension ' + ext);
159                         }
160                 },
161
162                 /**
163                  * Displays parent location
164                  */
165                 goLevelUp: function App_goLevelUp() {
166                         // split current path and get proper path for parent location
167                         var newPath = this.currentPath.split('/').slice(0, -1).join('/');
168
169                         if (newPath !== '') {
170                                 this.displayFolder(newPath);
171                         } else {
172                                 this.displayStorages();
173                         }
174                 },
175
176                 /**
177                  * creates new dir in currently viewed dir
178                  * @param {string} dirName
179                  * @return {boolean} return status
180                  */
181                 createDir: function App_createDir(dirName) {
182                         var status = true;
183                         if (this.currentDirPath !== '') {
184                                 try {
185                                         this.currentDirHandle.createDirectory(dirName);
186                                 } catch (e) {
187                                         status = false;
188                                         app.ui.alertPopup(e.message);
189                                 }
190                                 this.refreshCurrentPage();
191                         } else {
192                                 status = false;
193                                 app.ui.alertPopup("You can't create new nodes in the main view");
194                         }
195                         return status;
196                 },
197
198                 /**
199                  * Triggers refresh current page
200                  */
201                 refreshCurrentPage: function App_refreshCurrentPage() {
202                         if (this.currentPath !== '') {
203                                 this.displayFolder(this.currentPath, true);
204                         } else {
205                                 this.displayStorages();
206                         }
207                 },
208
209                 /**
210                  * Deletes nodes with specified paths
211                  * @param {string[]} nodes nodePaths
212                  */
213                 deleteNodes: function App_deleteNodes(nodes) {
214                         this.model.deleteNodes(nodes, this.currentDirHandle, this.ui.removeNodeFromList.bind(this.ui));
215                 },
216
217                 /**
218                  * @param {string[]} paths filepaths
219                  * @param {number} mode clipboard mode
220                  */
221                 saveToClipboard: function App_saveToClipboard(paths, mode) {
222                         var clipboardLength = this.clipboard.add(paths);
223
224                         if (clipboardLength > 0) {
225                                 this.clipboard.setMode(mode);
226                                 app.ui.alertPopup('Data saved in clipboard');
227                                 this.ui.clearTabbars();
228                         } else {
229                                 app.ui.alertPopup('Error occured. Data has not been saved in clipboard');
230                         }
231
232                         this.ui.refreshPasteActionBtn(this.clipboard.isEmpty());
233                 },
234
235                 /**
236                  * Paste nodes from clipboard to current dir
237                  */
238                 pasteClipboard: function App_pasteClipboard() {
239                         var clipboardData = this.clipboard.get();
240
241                         if (clipboardData.length === 0) {
242                                 app.ui.alertPopup('Clipboard is empty');
243                                 return false;
244                         }
245
246                         if (this.clipboard.getMode() === this.clipboard.COPY_MODE_ID) {
247                                 this.model.copyNodes(this.currentDirHandle, clipboardData, this.currentPath, this.onPasteClipboardSuccess.bind(this));
248                         } else {
249                                 this.model.moveNodes(this.currentDirHandle, clipboardData, this.currentPath, this.onPasteClipboardSuccess.bind(this));
250                         }
251
252                         this.ui.refreshPasteActionBtn(this.clipboard.isEmpty());
253
254                         return true;
255                 },
256
257                 emptyClipboard: function App_emptyClipboard() {
258                         return this.clipboard.get().length === 0;
259                 },
260
261                 /**
262                  * Handler for paste clipboard success
263                  */
264                 onPasteClipboardSuccess: function App_onPasteClipboardSuccess() {
265                         this.clipboard.clear();
266                         this.refreshCurrentPage();
267                 },
268
269                 /**
270                  * App exit
271                  */
272                 exit: function App_exit() {
273                         tizen.application.getCurrentApplication().exit();
274                 }
275         };
276 }());