1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
/* <copyright>
Copyright (c) 2012, Motorola Mobility, Inc
All Rights Reserved.
BSD License.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Motorola Mobility nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */
var Montage = require("montage/core/core").Montage,
pickerNavigatorReel = require("js/io/ui/file-picker/picker-navigator.reel").PickerNavigator,
filePickerModelModule = require("js/io/ui/file-picker/file-picker-model"),
Popup = require("montage/ui/popup/popup.reel").Popup;
//singleton with functions to create a new file picker instance and utilities to format or filter the model data
var FilePickerController = exports.FilePickerController = Montage.create(require("montage/ui/component").Component, {
/**
* Register a listener for file open event
*/
deserializedFromTemplate:{
writable:false,
enumerable:true,
value:function(){
this.eventManager.addEventListener("openFilePicker", this, false);
}
},
pickerNavChoices:{
enumerable: true,
value: null
},
filePickerPopupType:{
enumerable: false,
value: "filePicker"
},
handleOpenFilePicker: {
value: function(evt) {
this.showFilePicker(evt.detail);
}
},
/**
*this function is used to create an instance of a file picker
*
* parameters:
* settings is an object containing :
* callback [optional]: the call back function which will be used to send the selected URIs back. If undefined then an event is fired with the selected uri
* callbackScope : required if callback is set
* pickerMode [optional]: ["read", "write"] : specifies if the file picker is opened to read a file/folder or to save a file
* currentFilter [optional]: if a current filter needs to be applied [ex: .psd]
* allFileFilters [optional]: list of filters that user can use to filter the view
* inFileMode [optional]: true => allow file selection , false => allow directory selection
* allowNewFileCreation [optional]: flag to specify whether or not it should return URI(s) to item(s) that do not exist. i.e. a user can type a filename to a new file that doesn't yet exist in the file system.
* allowMultipleSelections [optional]: allowMultipleSelections
* pickerName: name for montage custom popup
*
* return: none
*/
showFilePicker:{
writable:false,
enumerable:true,
value:function(settings){
var callback, callbackScope, pickerMode, currentFilter, allFileFilters, inFileMode, allowNewFileCreation, allowMultipleSelections, pickerName;
if(!!settings){
if(typeof settings.callback !== "undefined"){callback = settings.callback;}
if(typeof settings.pickerMode !== "undefined"){pickerMode = settings.pickerMode;}
if(typeof settings.currentFilter !== "undefined"){currentFilter = settings.currentFilter;}
if(typeof settings.allFileFilters !== "undefined"){allFileFilters = settings.allFileFilters;}
if(typeof settings.inFileMode !== "undefined"){inFileMode = settings.inFileMode;}
if(typeof settings.allowNewFileCreation !== "undefined"){allowNewFileCreation = settings.allowNewFileCreation;}
if(typeof settings.allowMultipleSelections !== "undefined"){allowMultipleSelections = settings.allowMultipleSelections;}
if(typeof settings.pickerName !== "undefined"){this.filePickerPopupType = settings.pickerName;}
}
if(settings.pickerName === "saveAsDirectoryPicker"){//need to set the picker mode in a better way
pickerMode = "write";
}else{
pickerMode = "read";
}
var aModel = filePickerModelModule.FilePickerModel.create();
var topLevelDirectories = null;
var driveData = this.application.ninja.coreIoApi.getDirectoryContents({uri:"", recursive:false, returnType:"all"});
if(driveData.success){
topLevelDirectories = (JSON.parse(driveData.content)).children;
}else{
var errorCause = "";
if(driveData.status === null){
errorCause = "Service Unavailable"
}else{
errorCause = driveData.status;
}
aModel.fatalError = " ** Unable to get files [Error: "+ errorCause +"]";
}
//dummy data - TODO:remove after testing
//aModel.currentFilter = "*.html, *.png";
//aModel.currentFilter = "*.jpg";
aModel.currentFilter = "*.*";
aModel.inFileMode = true;
aModel.fileFilters = [".html, .htm", ".jpg, .jpeg, .png, .gif", ".js, .json", ".css", ".txt, .rtf", ".doc, .docx", ".pdf", ".avi, .mov, .mpeg, .ogg, .webm", "*.*"];
//-end - dummy data
if(!!currentFilter){aModel.currentFilter = currentFilter;}
if(typeof inFileMode !== "undefined"){aModel.inFileMode = inFileMode;}
aModel.topLevelDirectories = topLevelDirectories;
if(!!topLevelDirectories && !!topLevelDirectories[0]){
aModel.currentRoot = aModel.currentLogicalDrive = topLevelDirectories[0].uri;
}
//populate the last opened folder first, if none then populate default root
var storedUri = null;
var sessionStorage = window.sessionStorage;
try{
if(pickerMode === "write"){
storedUri = sessionStorage.getItem("lastSavedFolderURI");
}else if(inFileMode === true){
storedUri = sessionStorage.getItem("lastOpenedFolderURI_fileSelection");
}else if(inFileMode === false){
storedUri = sessionStorage.getItem("lastOpenedFolderURI_folderSelection");
}
}catch(e){
if(e.code == 22){
sessionStorage.clear();
}
}
if(!!storedUri){
aModel.currentRoot = decodeURI(storedUri);
}
if(!!allFileFilters){aModel.fileFilters = allFileFilters;}
if(!!callback){aModel.callback = callback;}
if(!!callbackScope){aModel.callbackScope = callbackScope;}
if(typeof pickerMode !== "undefined"){aModel.pickerMode = pickerMode;}
//logic: get file content data onDemand from the REST api for the default or last opened root. Cache the data in page [in local cache ? dirty fs? ]. Filter on client side to reduce network calls.
this.openFilePickerAsModal(callback, aModel);
//to open this on another modal dialog, make it a popup instead above the modal dialog container layer
}
},
openFilePickerAsModal:{
writable:false,
enumerable:true,
value:function(callback, aModel){
var pickerNavChoices = this.pickerNavChoices = Montage.create(pickerNavigatorReel);
var initUri = aModel.currentRoot;
//remove extra / at the end
if((initUri.length > 1) && (initUri.charAt(initUri.length - 1) === "/")){
initUri = initUri.substring(0, (initUri.length - 1));
}
pickerNavChoices.mainContentData = this.prepareContentList(initUri, aModel);
pickerNavChoices.pickerModel = aModel;
var popup = Popup.create();
popup.content = pickerNavChoices;
popup.modal = true;
popup.type = this.filePickerPopupType;//should be set always to not default to the single custom popup layer
popup.show();
pickerNavChoices.popup = popup;//handle to be used for hiding the popup
}
},
openFilePickerAsPopup:{
writable:false,
enumerable:true,
value:function(){}
},
expandDirectory:{
writable:false,
enumerable:true,
value: function(root, currentFilter, inFileMode){
//populate children in dom
}
},
refreshDirectoryCache:{
writable:false,
enumerable:true,
value:function(directoryUri){
if(directoryContentCache[directoryUri] !== null){
directoryContentCache[directoryUri] = null; //invalidate the cached content
//fetch fresh content
}
}
},
/**
* queries the cache to build contents array. If not found queries the file system
*
* parameters:
* folderUri
* aModel: model instance per picker instance, containing
*/
prepareContentList:{
writable: false,
enumerable:true,
value:function(folderUri, aModel, fromCache, checkStaleness){
var contentList = [],
childrenArray = [];
var folderContent = null;
// query filesystem and populate cache
if(((typeof fromCache !== "undefined") && (fromCache === false))
|| !this._directoryContentCache[folderUri]
|| !this._directoryContentCache[folderUri].children){
//get data using IO api
try{
var iodata = this.application.ninja.coreIoApi.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"});
//console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content);
if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){
folderContent = JSON.parse(iodata.content);
}
}catch(e){
console.error("Error to IO uri: "+folderUri+"\n"+e.message);
}
if(!!folderContent){
//contentList = folderContent.children;//need to apply filters and mode
this.cacheContentForRandomAccess(folderUri, folderContent);
}
}
//now from cache - apply filters and mode
if((!!this._directoryContentCache[folderUri])
&& (this._directoryContentCache[folderUri].type === "directory")
&& (typeof this._directoryContentCache[folderUri].children !== "undefined")
&& (this._directoryContentCache[folderUri].children !== null)){
//console.log("$$$ this._directoryContentCache");
//console.log(this._directoryContentCache);
//check for directory staleness.... if stale query filesystem
if((typeof checkStaleness === "undefined") || (checkStaleness === true)){
this.checkIfStale(folderUri);
}
childrenArray = this._directoryContentCache[folderUri].children;
//prepare content array for folder uri
childrenArray.forEach(function(item){
if(this._directoryContentCache[item]){
//apply mode and filtering here
if(aModel.inFileMode){// if in file selection mode, do filtering
if((this._directoryContentCache[item].type === "directory") || !aModel.currentFilter){//no filetering
contentList.push(this._directoryContentCache[item]);
}
else if(aModel.currentFilter){
if(this.applyFilter(this._directoryContentCache[item].name, aModel.currentFilter)){
contentList.push(this._directoryContentCache[item]);
}
}
}else{// if in folder selection mode
if(this._directoryContentCache[item].type === "directory"){
contentList.push(this._directoryContentCache[item]);
}
}
}
}, this);
}
else if((typeof this._directoryContentCache[folderUri] !== 'undefined') && (this._directoryContentCache[folderUri].type === "file")){//if the uri is for a file
//check for directory staleness.... if stale query filesystem
if((typeof checkStaleness === "undefined") || (checkStaleness === true)){
this.checkIfStale(folderUri);
}
contentList.push(this._directoryContentCache[folderUri]);
}
//end - from cache
return contentList;
}
},
/**
* populates/updates cache for a uri
*/
cacheContentForRandomAccess:{
writable:false,
enumerable:true,
value: function(directoryUri, directoryContents){
var that = this;
//assumption: directoryContents will have only its direct files and subfolders
//uri is the unique identifier
//check if the directoryUri exists in cache
//if not add uri content object, prepare children's uri array,then add/update objects for children
if(!this._directoryContentCache[directoryUri]){//uri not in cache... so add it
//add uri content object
this._directoryContentCache[directoryUri] = {"type":directoryContents.type,"name":directoryContents.name,"uri":directoryUri};
if(!!directoryContents.size){
this._directoryContentCache[directoryUri].size = directoryContents.size;
}
if(!!directoryContents.creationDate){
this._directoryContentCache[directoryUri].creationDate = directoryContents.creationDate;
}
if(!!directoryContents.modifiedDate){
this._directoryContentCache[directoryUri].modifiedDate = directoryContents.modifiedDate;
}
//store the current queried time for refreshing cache logic
this._directoryContentCache[directoryUri].queriedTimeStamp = (new Date()).getTime();
if(!!directoryContents.children && directoryContents.children.length > 0){
this._directoryContentCache[directoryUri].children = [];
//add the uri to this._directoryContentCache[directoryUri].children, and add the child's description objects
directoryContents.children.forEach(function(obj){
//add uri to parent's children list
that._directoryContentCache[directoryUri].children.push(obj.uri);
//add the child object
that._directoryContentCache[obj.uri] = obj;
//store the current queried time for refreshing cache logic
that._directoryContentCache[obj.uri].queriedTimeStamp = (new Date()).getTime();
} ,this);
}
}else{//uri in cache... so update it AND its children
this._directoryContentCache[directoryUri].type = directoryContents.type;
this._directoryContentCache[directoryUri].name = directoryContents.name;
if(!!directoryContents.size){
this._directoryContentCache[directoryUri].size = directoryContents.size;
}
if(!!directoryContents.creationDate){
this._directoryContentCache[directoryUri].creationDate = directoryContents.creationDate;
}
if(!!directoryContents.modifiedDate){
this._directoryContentCache[directoryUri].modifiedDate = directoryContents.modifiedDate;
}
//store the current queried time for refreshing cache logic
this._directoryContentCache[directoryUri].queriedTimeStamp = (new Date()).getTime();
if(!!directoryContents.children && directoryContents.children.length > 0){
// logic to clear off objects from cache if they no longer exist in the filesystem
//better logic - use isUpdatedFlag for a folder .. then compare modified date
//hack for now - clear up the old children and add new ones
var tempArr = this._directoryContentCache[directoryUri].children;
if( !!tempArr && Array.isArray(tempArr) && tempArr.length>0){
tempArr.forEach(function(uriString){
if(!!that._directoryContentCache[uriString]){
delete that._directoryContentCache[uriString];
}
});
}
this._directoryContentCache[directoryUri].children = [];
//add the uri to this._directoryContentCache[directoryUri].children, and add the child's description objects
directoryContents.children.forEach(function(obj){
//add uri to parent's children list
that._directoryContentCache[directoryUri].children.push(obj.uri);
//add the child object
that._directoryContentCache[obj.uri] = obj;
//store the current queried time for refreshing cache logic
that._directoryContentCache[obj.uri].queriedTimeStamp = (new Date()).getTime();
} ,this);
}else{
this._directoryContentCache[directoryUri].children = [];
}
}
//console.log("$$$ "+directoryUri+" modifiedDate = "+this._directoryContentCache[directoryUri].modifiedDate);
//console.log("$$$ "+directoryUri+" queriedTimeStamp = "+this._directoryContentCache[directoryUri].queriedTimeStamp);
}
},
applyFilter:{
writable: false,
enumerable:true,
value:function(fileName , filters){
if(filters.indexOf("*.*") !== -1){return true;}
//console.log(""+fileName);
var filtersArr = filters.split(",");
var passed = false;
for(var i=0; i< filtersArr.length; i++){
filtersArr[i] = filtersArr[i].trim();
//console.log(filtersArr[i]);
var fileType = filtersArr[i].substring(filtersArr[i].indexOf(".") );
//console.log(""+fileType);
//ignore uppercase
fileName = fileName.toLowerCase();
fileType = fileType.toLowerCase();
if(fileName.indexOf(fileType, fileName.length - fileType.length) !== -1){//ends with file type
passed = true;
break;
}
}
return passed;
}
},
/**
* Stale Time (ms) for each resource
* Logic: the last queried time for a resource is compared to stale time. If stale, then file system is queried
*/
cacheStaleTime:{
writable: false,
enumerable: false,
value: 5000
},
checkIfStale: {
writable: false,
enumerable: true,
value: function(folderUri){
var wasStale = false;
var folderContent = null;
//check for directory staleness.... if stale query filesystem
if((new Date()).getTime() > (this._directoryContentCache[folderUri].queriedTimeStamp + this.cacheStaleTime)){
try{
var ifModifiedResponse = this.application.ninja.coreIoApi.isDirectoryModified({uri:folderUri, recursive:false, returnType:"all"}, this._directoryContentCache[folderUri].queriedTimeStamp);
//console.log("ifModifiedResponse");
//console.log(ifModifiedResponse);
}catch(e){
console.error("Error to IO uri with isDirectoryModified: "+folderUri+"\n"+e.message);
}
if(ifModifiedResponse && ifModifiedResponse.status === 304){
//do nothing since the uri has not changed
}else if(ifModifiedResponse && (ifModifiedResponse.status === 200)){
wasStale = true;
//uri has changed. so update cache
try{
var iodata = this.application.ninja.coreIoApi.getDirectoryContents({uri:folderUri, recursive:false, returnType:"all"});
//console.log("IO:getDirectoryContents:Response:\n"+"uri="+folderUri+"\n status="+iodata.status+"\n content= "+iodata.content);
if(iodata.success && (iodata.status === 200) && (iodata.content !== null)){
folderContent = JSON.parse(iodata.content);
}
}catch(e){
console.error("Error to IO uri: "+folderUri+"\n"+e.message);
}
if(!!folderContent){
this.cacheContentForRandomAccess(folderUri, folderContent);
}
}
}
return wasStale;
}
},
/**
* This will store the directory content per session
* check session storage for this
*/
_directoryContentCache:{
writable:true,
enumerable:false,
value:{}
},
clearCache:{
writable:false,
enumerable: true,
value: function(){
this._directoryContentCache = {};
}
}
});
|