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.js310
1 files changed, 310 insertions, 0 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js
new file mode 100644
index 00000000..3914ec29
--- /dev/null
+++ b/js/io/system/ninjalibrary.js
@@ -0,0 +1,310 @@
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: result.content, 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", '/ninja-internal/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.send();
191 //Checking for status
192 if (xhr.readyState === 4) { //TODO: add check for mime type
193 //Creating new file from loaded content
194 //this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, function (status) {if(status) this.libraryCopied()}.bind(this));
195 } else {
196 //Error creating single file library
197 }
198 } else {
199 //Creating root folder
200 this.chromeApi.directoryNew('/'+tocopylibs[i].name);
201 //Getting file contents
202 xhr = new XMLHttpRequest();
203 xhr.open("GET", tocopylibs[i].path, false);
204 xhr.send();
205 //Checking for status
206 if (xhr.readyState === 4) {
207 //
208 libjson = JSON.parse(xhr.response);
209 //
210 for (l=0; libjson.directories[l]; l++) {
211 libjson.dirsToCreate = libjson.directories.length;
212 libjson.dirsCreated = 0;
213 libjson.filesToCreate = libjson.files.length;
214 libjson.filesCreated = 0;
215 libjson.local = tocopylibs[i].name;
216 libjson.main = this;
217 this.createDirectory(tocopylibs[i].name, libjson.directories[l], function (status) {
218 //Checking for success on directories created
219 if (status) {
220 this.dirsCreated++;
221 }
222 //All directories created
223