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.js322
1 files changed, 322 insertions, 0 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js
new file mode 100644
index 00000000..7b524189
--- /dev/null
+++ b/js/io/system/ninjalibrary.js
@@ -0,0 +1,322 @@
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
118 /*
119var ui8a = new Uint8Array(result.content);
120 console.log(ui8a);
121
122 var blob = new window.WebKitBlobBuilder;
123 blob.append(result.content);
124*/
125 //console.log(blob.getBlob(result.data.type));
126 //this.coreApi.createFile({uri: fileRoot+result.file.fullPath, contents: result.content, contentType: result.data.type});
127 //this.coreApi.createFile({uri: fileRoot+result.file.fullPath, contents: blob.getBlob(result.data.type), contentType: result.data.type});
128 this.coreApi.createFile({uri: fileRoot+result.file.fullPath, contents: result.content});
129 }.bind(this));
130 }
131 }
132 }.bind(this));
133 }
134 }
135 },
136 ////////////////////////////////////////////////////////////////////
137 //
138 synchronize: {
139 enumerable: true,
140 value: function(chromeLibs, chrome) {
141 //
142 this.chromeApi = chrome;
143 //
144 var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = [], copied;
145 //Getting known json list of libraries to copy to chrome
146 xhr.open("GET", '/ninja-internal/js/io/system/ninjalibrary.json', false);
147 xhr.send();
148 //Checkng for correct reponse
149 if (xhr.readyState === 4) {
150 //Parsing json libraries
151 libs = JSON.parse(xhr.response);
152 //
153 if (chromeLibs.length > 0) {
154 //
155 for (i=0; chromeLibs[i]; i++) {
156 copied = false;
157 for (var j in libs.libraries) {
158 if (String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase() === chromeLibs[i]) {
159 copied = true;
160 }
161 }
162 //
163 if (!copied) {
164 if (libs.libraries[j].file) {
165 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file});
166 } else {
167 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path});
168 }
169 } else {
170 //TODO: Remove, currently manually removing copied libraries
171 //this.chromeApi.directoryDelete(chromeLibs[i]);
172 }
173 }
174
175 } else {
176 //No library is present, must copy all
177 for (var j in libs.libraries) {
178 //name: used to folder container contents
179 //path: url of descriptor json or single file to load (descriptor has list of files)
180 //singular: indicates the path is the file to be loaded into folder
181 if (libs.libraries[j].file) {
182 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file});
183 } else {
184 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path});
185 }
186 }
187 }
188 //
189 this._libsToSync = tocopylibs.length;
190 //
191 if (tocopylibs.length > 0) {
192 for (i=0; tocopylibs[i]; i++) {
193 //Checking for library to be single file
194 if (tocopylibs[i].file) {
195 //Creating root folder
196 this.chromeApi.directoryNew('/'+tocopylibs[i].name);
197 //Getting file contents
198 xhr = new XMLHttpRequest();
199 xhr.open("GET", tocopylibs[i].path, false);
200 xhr.responseType = "arraybuffer";
201 xhr.send();
202 //Checking for status
203 if (xhr.readyState === 4) { //TODO: add check for mime type
204 //Creating new file from loaded content
205 this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, function (status) {if(status) this.libraryCopied()}.bind(this));
206 } else {
207 //Error creating single file library
208 }
209 } else {
210 //Creating root folder
211 this.chromeApi.directoryNew('/'+tocopylibs[i].name);
212 //Getting file contents
213 xhr = new XMLHttpRequest();
214 xhr.open("GET", tocopylibs[i].path, false);
215 xhr.send();
216 //Checking for status
217 if (xhr.readyState === 4) {
218 //
219 libjson = JSON.parse(xhr.response);
220 //
221 for (l=0; libjson.directories[l]; l++) {
222 libjson.dirsToCreate = libjson.directories.length;
223 libjson.dirsCreated = 0;