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
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
/* /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
NOTES:
Core API reference in NINJA: this.application.ninja.coreIoApi
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////// */
//
var Montage = require("montage/core/core").Montage;
////////////////////////////////////////////////////////////////////////
//
exports.NinjaLibrary = Montage.create(Object.prototype, {
////////////////////////////////////////////////////////////////////
//
_chromeApi: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//
chromeApi: {
enumerable: false,
get: function() {
return this._chromeApi;
},
set: function(value) {
this._chromeApi = value;
}
},
////////////////////////////////////////////////////////////////////
//
synchronize: {
enumerable: true,
value: function(chromeLibs, chrome) {
//
this.chromeApi = chrome;
//
var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = [];
//Getting known json list of libraries to copy to chrome
xhr.open("GET", '/ninja-internal/js/io/system/ninjalibrary.json', false);
xhr.send();
//Checkng for correct reponse
if (xhr.readyState === 4) {
//Parsing json libraries
libs = JSON.parse(xhr.response);
//
if (chromeLibs.length > 0) {
//TODO: Remove
for (i=0; chromeLibs[i]; i++) {
this.chromeApi.directoryDelete(chromeLibs[i]);
}
} else {
//No library is present, must copy all
for (var j in libs.libraries) {
//name: used to folder container contents
//path: url of descriptor json or single file to load (descriptor has list of files)
//singular: indicates the path is the file to be loaded into folder
if (libs.libraries[j].file) {
tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file});
} else {
tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path});
}
}
}
//
if (tocopylibs.length > 0) {
for (i=0; tocopylibs[i]; i++) {
//Checking for library to be single file
if (tocopylibs[i].file) {
//Creating root folder
this.chromeApi.directoryNew('/'+tocopylibs[i].name);
//Getting file contents
xhr = new XMLHttpRequest();
xhr.open("GET", tocopylibs[i].path, false);
xhr.send();
//Checking for status
if (xhr.readyState === 4) { //TODO: add check for mime type
//Creating new file from loaded content
this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response);
} else {
//Error creating single file library
}
} else {
//Creating root folder
this.chromeApi.directoryNew('/'+tocopylibs[i].name);
//Getting file contents
xhr = new XMLHttpRequest();
xhr.open("GET", tocopylibs[i].path, false);
xhr.send();
//Checking for status
if (xhr.readyState === 4) {
//
libjson = JSON.parse(xhr.response);
//
for (l=0; libjson.directories[l]; l++) {
libjson.dirsToCreate = libjson.directories.length;
libjson.dirsCreated = 0;
libjson.filesToCreate = libjson.files.length;
libjson.filesCreated = 0;
libjson.api = this.chromeApi;
libjson.local = tocopylibs[i].name;
this.createDirectory(tocopylibs[i].name, libjson.directories[l], function (status) {
//Checking for success on directories created
if (status) {
this.dirsCreated++;
}
//All directories created
if (this.dirsCreated === this.dirsToCreate) {
var xhr, i;
for (i=0; this.files[i]; i++) {
xhr = new XMLHttpRequest();
xhr.open("GET", this.root+this.files[i], false);
xhr.send();
//Checking for status
if (xhr.readyState === 4) {
this.api.fileNew(this.local+'/'+this.files[i], xhr.response, function (status) {
if (status) {
this.filesCreated++;
}
if (this.filesCreated === this.filesToCreate) {
//TODO: Add logic for task completed
}
}.bind(this));
}
}
}
}.bind(libjson));
}
} else {
//Error
}
}
}
} else {
//No libraries to copy
}
} else {
//Error
}
}
},
////////////////////////////////////////////////////////////////////
//
createDirectory: {
enumerable: true,
value: function(root, folder, callback) {
//
if (folder.name) {
if (root) {
dir = root+'/'+folder.name;
} else {
dir = folder.name;
}
//
this.chromeApi.directoryNew(dir, function (status) {if (callback)callback(status)});
}
//
if (folder.children) {
for (var j in folder.children) {
if (root) {
this.createDirectory(root+'/'+folder.name, folder.children[j]);
} else {
this.createDirectory(folder.name, folder.children[j]);
}
}
}
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
});
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
|