diff options
Diffstat (limited to 'js/io/system/ninjalibrary.js')
-rw-r--r-- | js/io/system/ninjalibrary.js | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js new file mode 100644 index 00000000..960bdef9 --- /dev/null +++ b/js/io/system/ninjalibrary.js | |||
@@ -0,0 +1,351 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /* ///////////////////////////////////////////////////////////////////// | ||
8 | //////////////////////////////////////////////////////////////////////// | ||
9 | NOTES: | ||
10 | |||
11 | //////////////////////////////////////////////////////////////////////// | ||
12 | ///////////////////////////////////////////////////////////////////// */ | ||
13 | // | ||
14 | var Montage = require("montage/core/core").Montage; | ||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | // | ||
17 | exports.NinjaLibrary = Montage.create(Object.prototype, { | ||
18 | //////////////////////////////////////////////////////////////////// | ||
19 | // | ||
20 | _chromeApi: { | ||
21 | enumerable: false, | ||
22 | value: null | ||
23 | }, | ||
24 | //////////////////////////////////////////////////////////////////// | ||
25 | // | ||
26 | chromeApi: { | ||
27 | enumerable: false, | ||
28 | get: function() { | ||
29 | return this._chromeApi; | ||
30 | }, | ||
31 | set: function(value) { | ||
32 | this._chromeApi = value; | ||
33 | } | ||
34 | }, | ||
35 | //////////////////////////////////////////////////////////////////// | ||
36 | // | ||
37 | _coreApi: { | ||
38 | enumerable: false, | ||
39 | value: null | ||
40 | }, | ||
41 | //////////////////////////////////////////////////////////////////// | ||
42 | // | ||
43 | coreApi: { | ||
44 | enumerable: false, | ||
45 | get: function() { | ||
46 | return this._coreApi; | ||
47 | }, | ||
48 | set: function(value) { | ||
49 | this._coreApi = value; | ||
50 | } | ||
51 | }, | ||
52 | //////////////////////////////////////////////////////////////////// | ||
53 | // | ||
54 | _libs: { | ||
55 | enumerable: false, | ||
56 | value: null | ||
57 | }, | ||
58 | //////////////////////////////////////////////////////////////////// | ||
59 | // | ||
60 | libs: { | ||
61 | enumerable: false, | ||
62 | get: function() { | ||
63 | return this._libs; | ||
64 | }, | ||
65 | set: function(value) { | ||
66 | this._libs = value; | ||
67 | } | ||
68 | }, | ||
69 | //////////////////////////////////////////////////////////////////// | ||
70 | // | ||
71 | _libsToSync: { | ||
72 | enumerable: false, | ||
73 | value: 0 | ||
74 | }, | ||
75 | //////////////////////////////////////////////////////////////////// | ||
76 | // | ||
77 | _syncedLibs: { | ||
78 | enumerable: false, | ||
79 | value: 0 | ||
80 | }, | ||
81 | //////////////////////////////////////////////////////////////////// | ||
82 | // | ||
83 | copyLibToCloud: { | ||
84 | enumerable: false, | ||
85 | value: function (path, libName) { | ||
86 | // | ||
87 | if(this.coreApi.directoryExists({uri: path+libName}).status === 404) { | ||
88 | this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { | ||
89 | for (var i in contents) { | ||
90 | if (libName === contents[i].name) { | ||
91 | //Getting contents of library to be copied | ||
92 | this.chromeApi.directoryContents(contents[i], function (lib) { | ||
93 | //Creating directory structure from subfolders | ||
94 | this.copyDirectoryToCloud(path, contents[i], path, function (status) {console.log(status)}); | ||
95 | }.bind(this)); | ||
96 | break; | ||
97 | } | ||
98 | } | ||
99 | }.bind(this)); | ||
100 | } else { | ||
101 | //Error | ||
102 | } | ||
103 | } | ||
104 | }, | ||
105 | //////////////////////////////////////////////////////////////////// | ||
106 | // | ||
107 | copyDirectoryToCloud: { | ||
108 | enumerable: true, | ||
109 | value: function(root, folder, fileRoot, callback) { | ||
110 | // | ||
111 | if (folder.name) { | ||
112 | var dir; | ||
113 | if (root) { | ||
114 | dir = root+'/'+folder.name; | ||
115 | } else { | ||
116 | dir = folder.name; | ||
117 | } | ||
118 | // | ||
119 | if (!this.coreApi.createDirectory({uri: dir})) { | ||
120 | //Error occured while creating folders | ||
121 | return; | ||
122 | } | ||
123 | } | ||
124 | // | ||
125 | if (folder.isDirectory) { | ||
126 | this.chromeApi.directoryContents(folder, function (contents) { | ||
127 | for (var i in contents) { | ||
128 | if (contents[i].isDirectory) { | ||
129 | this.copyDirectoryToCloud(dir, contents[i], fileRoot); | ||
130 | } else if (contents[i].isFile){ | ||
131 | //File to copy | ||
132 | this.chromeApi.fileContent(contents[i].fullPath, function (result) { | ||
133 | // | ||
134 | //this.coreApi.createFile({uri: fileRoot+result.file.fullPath, contents: blob.getBlob(result.data.type), contentType: result.data.type}); | ||
135 | this.coreApi.createFile({uri: fileRoot+result.file.fullPath, contents: result.content}); | ||
136 | }.bind(this)); | ||
137 | } | ||
138 | } | ||
139 | }.bind(this)); | ||
140 | } | ||
141 | } | ||
142 | }, | ||
143 | //////////////////////////////////////////////////////////////////// | ||
144 | // | ||
145 | synchronize: { | ||
146 | enumerable: true, | ||
147 | value: function(chromeLibs, chrome) { | ||
148 | //TODO: Remove | ||
149 | window.wipeLibrary = this.deleteLibraries.bind(this); | ||
150 | // | ||
151 | this.chromeApi = chrome; | ||
152 | // | ||
153 | var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = [], copied; | ||
154 | //Getting known json list of libraries to copy to chrome | ||
155 | xhr.open("GET", '/js/io/system/ninjalibrary.json', false); | ||
156 | xhr.send(); | ||
157 | //Checkng for correct reponse | ||
158 | if (xhr.readyState === 4) { | ||
159 | //Parsing json libraries | ||
160 | libs = JSON.parse(xhr.response); | ||
161 | // | ||
162 | this.libs = libs.libraries; | ||
163 | // | ||
164 | if (chromeLibs.length > 0) { | ||
165 | // | ||
166 | for (i=0; chromeLibs[i]; i++) { | ||
167 | copied = false; | ||
168 | for (var j in libs.libraries) { | ||
169 | if (String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase() === chromeLibs[i]) { | ||
170 | copied = true; | ||
171 | } | ||
172 | } | ||
173 | // | ||
174 | if (!copied) { | ||
175 | if (libs.libraries[j].file) { | ||
176 | tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file}); | ||
177 | } else { | ||
178 | tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path}); | ||
179 | } | ||
180 | } else { | ||
181 | //TODO: Remove, currently manually removing copied libraries | ||
182 | //this.chromeApi.directoryDelete(chromeLibs[i]); | ||
183 | } | ||
184 | } | ||
185 | |||
186 | } else { | ||
187 | //No library is present, must copy all | ||
188 | for (var j in libs.libraries) { | ||
189 | //name: used to folder container contents | ||
190 | //path: url of descriptor json or single file to load (descriptor has list of files) | ||
191 | //singular: indicates the path is the file to be loaded into folder | ||
192 | if (libs.libraries[j].file) { | ||
193 | tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file}); | ||
194 | } else { | ||
195 | tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path}); | ||
196 | } | ||
197 | } | ||
198 | } | ||
199 | // | ||
200 | this._libsToSync = tocopylibs.length; | ||
201 | // | ||
202 | if (tocopylibs.length > 0) { | ||
203 | for (i=0; tocopylibs[i]; i++) { | ||
204 | //Checking for library to be single file | ||
205 | if (tocopylibs[i].file) { | ||
206 | //Creating root folder | ||
207 | this.chromeApi.directoryNew('/'+tocopylibs[i].name); | ||
208 | //Getting file contents | ||
209 | xhr = new XMLHttpRequest(); | ||
210 | xhr.open("GET", tocopylibs[i].path, false); | ||
211 | xhr.responseType = "arraybuffer"; | ||
212 | xhr.send(); | ||
213 | //Checking for status | ||
214 | if (xhr.readyState === 4) { //TODO: add check for mime type | ||
215 | //Creating new file from loaded content | ||
216 | this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, function (status) {if(status) this.libraryCopied()}.bind(this)); | ||
217 | } else { | ||
218 | //Error creating single file library | ||
219 | } | ||
220 | } else { | ||
221 | //Creating root folder | ||
222 | this.chromeApi.directoryNew('/'+tocopylibs[i].name); | ||
223 | //Getting file contents | ||
224 | xhr = new XMLHttpRequest(); | ||
225 | xhr.open("GET", tocopylibs[i].path, false); | ||
226 | xhr.send(); | ||
227 | //Checking for status | ||