diff options
-rw-r--r-- | js/io/system/ninjalibrary.js | 126 |
1 files changed, 51 insertions, 75 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js index 3de5fb69..dab623c1 100644 --- a/js/io/system/ninjalibrary.js +++ b/js/io/system/ninjalibrary.js | |||
@@ -4,12 +4,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | /* ///////////////////////////////////////////////////////////////////// | ||
8 | //////////////////////////////////////////////////////////////////////// | 7 | //////////////////////////////////////////////////////////////////////// |
9 | NOTES: | ||
10 | |||
11 | //////////////////////////////////////////////////////////////////////// | ||
12 | ///////////////////////////////////////////////////////////////////// */ | ||
13 | // | 8 | // |
14 | var Montage = require("montage/core/core").Montage; | 9 | var Montage = require("montage/core/core").Montage; |
15 | //////////////////////////////////////////////////////////////////////// | 10 | //////////////////////////////////////////////////////////////////////// |
@@ -18,73 +13,53 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
18 | //////////////////////////////////////////////////////////////////// | 13 | //////////////////////////////////////////////////////////////////// |
19 | // | 14 | // |
20 | _chromeApi: { | 15 | _chromeApi: { |
21 | enumerable: false, | ||
22 | value: null | 16 | value: null |
23 | }, | 17 | }, |
24 | //////////////////////////////////////////////////////////////////// | 18 | //////////////////////////////////////////////////////////////////// |
25 | // | 19 | // |
26 | chromeApi: { | 20 | chromeApi: { |
27 | enumerable: false, | 21 | get: function() {return this._chromeApi;}, |
28 | get: function() { | 22 | set: function(value) {this._chromeApi = value;} |
29 | return this._chromeApi; | ||
30 | }, | ||
31 | set: function(value) { | ||
32 | this._chromeApi = value; | ||
33 | } | ||
34 | }, | 23 | }, |
35 | //////////////////////////////////////////////////////////////////// | 24 | //////////////////////////////////////////////////////////////////// |
36 | // | 25 | // |
37 | _coreApi: { | 26 | _coreApi: { |
38 | enumerable: false, | ||
39 | value: null | 27 | value: null |
40 | }, | 28 | }, |
41 | //////////////////////////////////////////////////////////////////// | 29 | //////////////////////////////////////////////////////////////////// |
42 | // | 30 | // |
43 | coreApi: { | 31 | coreApi: { |
44 | enumerable: false, | 32 | get: function() {return this._coreApi;}, |
45 | get: function() { | 33 | set: function(value) {this._coreApi = value;} |
46 | return this._coreApi; | ||
47 | }, | ||
48 | set: function(value) { | ||
49 | this._coreApi = value; | ||
50 | } | ||
51 | }, | 34 | }, |
52 | //////////////////////////////////////////////////////////////////// | 35 | //////////////////////////////////////////////////////////////////// |
53 | // | 36 | // |
54 | _libs: { | 37 | _libs: { |
55 | enumerable: false, | ||
56 | value: null | 38 | value: null |
57 | }, | 39 | }, |
58 | //////////////////////////////////////////////////////////////////// | 40 | //////////////////////////////////////////////////////////////////// |
59 | // | 41 | // |
60 | libs: { | 42 | libs: { |
61 | enumerable: false, | 43 | get: function() {return this._libs;}, |
62 | get: function() { | 44 | set: function(value) {this._libs = value;} |
63 | return this._libs; | ||
64 | }, | ||
65 | set: function(value) { | ||
66 | this._libs = value; | ||
67 | } | ||
68 | }, | 45 | }, |
69 | //////////////////////////////////////////////////////////////////// | 46 | //////////////////////////////////////////////////////////////////// |
70 | // | 47 | // |
71 | _libsToSync: { | 48 | _libsToSync: { |
72 | enumerable: false, | ||
73 | value: 0 | 49 | value: 0 |
74 | }, | 50 | }, |
75 | //////////////////////////////////////////////////////////////////// | 51 | //////////////////////////////////////////////////////////////////// |
76 | // | 52 | // |
77 | _syncedLibs: { | 53 | _syncedLibs: { |
78 | enumerable: false, | ||
79 | value: 0 | 54 | value: 0 |
80 | }, | 55 | }, |
81 | //////////////////////////////////////////////////////////////////// | 56 | //////////////////////////////////////////////////////////////////// |
82 | // | 57 | // |
83 | copyLibToCloud: { | 58 | copyLibToCloud: { |
84 | enumerable: false, | ||
85 | value: function (path, libName, callback) { | 59 | value: function (path, libName, callback) { |
86 | // | 60 | //Checking for library to exists |
87 | if(this.coreApi.directoryExists({uri: path+libName}).status === 404) { | 61 | if(this.coreApi.directoryExists({uri: path+libName}).status === 404) { |
62 | //Getting contents to begin copying | ||
88 | this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { | 63 | this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { |
89 | for (var i in contents) { | 64 | for (var i in contents) { |
90 | if (libName === contents[i].name) { | 65 | if (libName === contents[i].name) { |
@@ -98,16 +73,15 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
98 | } | 73 | } |
99 | }.bind(this)); | 74 | }.bind(this)); |
100 | } else { | 75 | } else { |
101 | //Error | 76 | //TODO: Add error handling |
102 | } | 77 | } |
103 | } | 78 | } |
104 | }, | 79 | }, |
105 | //////////////////////////////////////////////////////////////////// | 80 | //////////////////////////////////////////////////////////////////// |
106 | // | 81 | // |
107 | copyDirectoryToCloud: { | 82 | copyDirectoryToCloud: { |
108 | enumerable: true, | ||
109 | value: function(root, folder, fileRoot, callback) { | 83 | value: function(root, folder, fileRoot, callback) { |
110 | // | 84 | //Setting up directory name |
111 | if (folder.name) { | 85 | if (folder.name) { |
112 | var dir; | 86 | var dir; |
113 | if (root) { | 87 | if (root) { |
@@ -115,23 +89,26 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
115 | } else { | 89 | } else { |
116 | dir = folder.name; | 90 | dir = folder.name; |
117 | } | 91 | } |
118 | // | 92 | //Creating directory |
119 | if (!this.coreApi.createDirectory({uri: dir.replace(/\/\//gi, '/')})) { | 93 | if (!this.coreApi.createDirectory({uri: dir.replace(/\/\//gi, '/')})) { |
120 | //Error occured while creating folders | 94 | //Error occured while creating folders |
121 | return; | 95 | return; //TODO: Add error handling |
122 | } | 96 | } |
123 | } | 97 | } |
124 | // | 98 | //Checking for directory |
125 | if (folder.isDirectory) { | 99 | if (folder.isDirectory) { |
100 | //Using Chrome API to get directory contents | ||
126 | this.chromeApi.directoryContents(folder, function (contents) { | 101 | this.chromeApi.directoryContents(folder, function (contents) { |
102 | //Looping through children of directory to copy | ||
127 | for (var i in contents) { | 103 | for (var i in contents) { |
104 | //Checking for file or directory | ||
128 | if (contents[i].isDirectory) { | 105 | if (contents[i].isDirectory) { |
106 | //Recursive call if directory | ||
129 | this.copyDirectoryToCloud(dir, contents[i], fileRoot); | 107 | this.copyDirectoryToCloud(dir, contents[i], fileRoot); |
130 | } else if (contents[i].isFile){ | 108 | } else if (contents[i].isFile){ |
131 | //File to copy | 109 | //Copying file |
132 | this.chromeApi.fileContent(contents[i].fullPath, function (result) { | 110 | this.chromeApi.fileContent(contents[i].fullPath, function (result) { |
133 | // | 111 | //Using binary when copying files to allow images and such to work |
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).replace(/\/\//gi, '/'), contents: result.content}); | 112 | this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content}); |
136 | }.bind(this)); | 113 | }.bind(this)); |
137 | } | 114 | } |
@@ -145,34 +122,31 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
145 | //////////////////////////////////////////////////////////////////// | 122 | //////////////////////////////////////////////////////////////////// |
146 | // | 123 | // |
147 | synchronize: { | 124 | synchronize: { |
148 | enumerable: true, | ||
149 | value: function(chromeLibs, chrome) { | 125 | value: function(chromeLibs, chrome) { |
150 | //TODO: Remove | 126 | //TODO: Remove |
151 | window.wipeLibrary = this.deleteLibraries.bind(this); | 127 | window.wipeLibrary = this.deleteLibraries.bind(this); |
152 | // | 128 | //Getting instance of browser file API |
153 | this.chromeApi = chrome; | 129 | this.chromeApi = chrome; |
154 | // | 130 | //Local variables |
155 | // debugger; | ||
156 | var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = []; | 131 | var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = []; |
157 | //Getting known json list of libraries to copy to chrome | 132 | //Getting known json list of libraries to copy to chrome (will be on a CDN later) |
158 | xhr.open("GET", '/js/io/system/ninjalibrary.json', false); | 133 | xhr.open("GET", '/js/io/system/ninjalibrary.json', false); |
159 | xhr.send(); | 134 | xhr.send(); |
160 | //Checkng for correct reponse | 135 | //Checkng for correct reponse |
161 | if (xhr.readyState === 4) { | 136 | if (xhr.readyState === 4) { |
162 | //Parsing json libraries | 137 | //Parsing json libraries |
163 | libs = JSON.parse(xhr.response); | 138 | libs = JSON.parse(xhr.response); |
164 | // | 139 | //Storing JSON data |
165 | this.libs = libs.libraries; | 140 | this.libs = libs.libraries; |
166 | // | 141 | //Checking for preview libraries to avoid duplicates |
167 | if (chromeLibs.length > 0) { | 142 | if (chromeLibs.length > 0) { |
168 | // | 143 | //Looping through libraries on browser file system |
169 | for (i=0; chromeLibs[i]; i++) { | 144 | for (i=0; chromeLibs[i]; i++) { |
170 | for (var j in libs.libraries) { | 145 | for (var j in libs.libraries) { |
171 | if (String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase() === chromeLibs[i]) { | 146 | //Checking for matching names (directories are libraries names) |
172 | //TODO: Remove, currently manually removing copied libraries | 147 | if (String(libs.libraries[j].name+libs.libraries[j].version).toLowerCase() !== chromeLibs[i]) { |
173 | // //this.chromeApi.directoryDelete(chromeLibs[i]); | 148 | //Checking for library to be single file (special case) |
174 | } else { | 149 | if (libs.libraries[j].file) { |
175 | if (libs.libraries[j].file) { | ||