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