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.js233
1 files changed, 233 insertions, 0 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js
new file mode 100644
index 00000000..fa2e7d1f
--- /dev/null
+++ b/js/io/system/ninjalibrary.js
@@ -0,0 +1,233 @@
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 Core API reference in NINJA: this.application.ninja.coreIoApi
12
13////////////////////////////////////////////////////////////////////////
14///////////////////////////////////////////////////////////////////// */
15//
16var Montage = require("montage/core/core").Montage;
17////////////////////////////////////////////////////////////////////////
18//
19exports.NinjaLibrary = Montage.create(Object.prototype, {
20 ////////////////////////////////////////////////////////////////////
21 //
22 _chromeApi: {
23 enumerable: false,
24 value: null
25 },
26 ////////////////////////////////////////////////////////////////////
27 //
28 chromeApi: {
29 enumerable: false,
30 get: function() {
31 return this._chromeApi;
32 },
33 set: function(value) {
34 this._chromeApi = value;
35 }
36 },
37 ////////////////////////////////////////////////////////////////////
38 //
39 _libsToSync: {
40 enumerable: false,
41 value: 0
42 },
43 ////////////////////////////////////////////////////////////////////
44 //
45 _syncedLibs: {
46 enumerable: false,
47 value: 0
48 },
49 ////////////////////////////////////////////////////////////////////
50 //
51 synchronize: {
52 enumerable: true,
53 value: function(chromeLibs, chrome) {
54 //
55 this.chromeApi = chrome;
56 //
57 var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = [], copied;
58 //Getting known json list of libraries to copy to chrome
59 xhr.open("GET", '/ninja-internal/js/io/system/ninjalibrary.json', false);
60 xhr.send();
61 //Checkng for correct reponse
62 if (xhr.readyState === 4) {
63 //Parsing json libraries
64 libs = JSON.parse(xhr.response);
65 //
66 if (chromeLibs.length > 0) {
67 //
68 for (i=0; chromeLibs[i]; i++) {
69 copied = false;
70 for (var j in libs.libraries) {
71 if (String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase() === chromeLibs[i]) {
72 copied = true;
73 }
74 }
75 //
76 if (!copied) {
77 if (libs.libraries[j].file) {
78 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file});
79 } else {
80 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path});
81 }
82 } else {
83 //TODO: Remove, currently manually removing copied libraries
84 this.chromeApi.directoryDelete(chromeLibs[i]);
85 }
86 }
87
88 } else {
89 //No library is present, must copy all
90 for (var j in libs.libraries) {
91 //name: used to folder container contents
92 //path: url of descriptor json or single file to load (descriptor has list of files)
93 //singular: indicates the path is the file to be loaded into folder
94 if (libs.libraries[j].file) {
95 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path, file: libs.libraries[j].file});
96 } else {
97 tocopylibs.push({name: String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase(), path: libs.libraries[j].path});
98 }
99 }
100 }
101 //
102 this._libsToSync = tocopylibs.length;
103 //
104 if (tocopylibs.length > 0) {
105 for (i=0; tocopylibs[i]; i++) {
106 //Checking for library to be single file
107 if (tocopylibs[i].file) {
108 //Creating root folder
109 this.chromeApi.directoryNew('/'+tocopylibs[i].name);
110 //Getting file contents
111 xhr = new XMLHttpRequest();
112 xhr.open("GET", tocopylibs[i].path, false);
113 xhr.send();
114 //Checking for status
115 if (xhr.readyState === 4) { //TODO: add check for mime type
116 //Creating new file from loaded content
117 this.chromeApi.fileNew('/'+tocopylibs[i].name+'/'+tocopylibs[i].file, xhr.response, function (status) {if(status) this.libraryCopied()}.bind(this));
118 } else {
119 //Error creating single file library
120 }
121 } else {
122 //Creating root folder
123 this.chromeApi.directoryNew('/'+tocopylibs[i].name);
124 //Getting file contents
125 xhr = new XMLHttpRequest();
126 xhr.open("GET", tocopylibs[i].path, false);
127 xhr.send();
128 //Checking for status
129 if (xhr.readyState === 4) {
130 //
131 libjson = JSON.parse(xhr.response);
132 //
133 for (l=0; libjson.directories[l]; l++) {
134 libjson.dirsToCreate = libjson.directories.length;
135 libjson.dirsCreated = 0;
136 libjson.filesToCreate = libjson.files.length;
137 libjson.filesCreated = 0;
138 libjson.local = tocopylibs[i].name;
139 libjson.main = this;
140 this.createDirectory(tocopylibs[i].name, libjson.directories[l], function (status) {
141 //Checking for success on directories created
142 if (status) {
143 this.dirsCreated++;
144 }
145 //All directories created
146 if (this.dirsCreated === this.dirsToCreate) {
147 var xhr, i;
148 for (i=0; this.files[i]; i++) {
149 xhr = new XMLHttpRequest();
150 xhr.open("GET", this.root+this.files[i], false);
151 xhr.send();
152 //Checking for status
153 if (xhr.readyState === 4) {
154 this.main.chromeApi.fileNew(this.local+'/'+this.files[i], xhr.response, function (status) {
155 if (status) {
156 this.filesCreated++;
157 }
158 if (this.filesCreated === this.filesToCreate) {
159 this.main.libraryCopied();
160 }
161 }.bind(this));
162 }
163 }
164 }
165 }.bind(libjson));
166 }
167 } else {
168 //Error
169 }
170 }
171 }
172 } else {
173 //Dispatching ready event since nothing to copy
174 this._dispatchEvent();
175 }
176 } else {
177 //Error
178 }
179 }
180 },
181 ////////////////////////////////////////////////////////////////////
182 //
183 createDirectory: {
184 enumerable: true,
185 value: function(root, folder, callback) {
186 //
187 if (folder.name) {
188 if (root) {
189 dir = root+'/'+folder.name;
190 } else {
191 dir = folder.name;
192 }
193 //
194 this.chromeApi.directoryNew(dir, function (status) {if (callback)callback(status)});
195 }
196 //
197 if (folder.children) {
198 for (var j in folder.children) {
199 if (root) {
200 this.createDirectory(root+'/'+folder.name, folder.children[j]);
201 } else {
202 this.createDirectory(folder.name, folder.children[j]);
203 }
204 }
205 }
206 }
207 },
208 ////////////////////////////////////////////////////////////////////
209 //
210 libraryCopied: {
211 enumerable: true,
212 value: function() {
213 this._syncedLibs++;
214 if (this._syncedLibs === this._libsToSync) {
215 this._dispatchEvent();
216 }
217 }
218 },
219 ////////////////////////////////////////////////////////////////////
220 //
221 _dispatchEvent: {