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