diff options
Diffstat (limited to 'js/io/system/ninjalibrary.js')
-rw-r--r-- | js/io/system/ninjalibrary.js | 490 |
1 files changed, 245 insertions, 245 deletions
diff --git a/js/io/system/ninjalibrary.js b/js/io/system/ninjalibrary.js index 1467d18f..be59fd07 100644 --- a/js/io/system/ninjalibrary.js +++ b/js/io/system/ninjalibrary.js | |||
@@ -34,7 +34,7 @@ var Montage = require("montage/core/core").Montage; | |||
34 | //////////////////////////////////////////////////////////////////////// | 34 | //////////////////////////////////////////////////////////////////////// |
35 | // | 35 | // |
36 | exports.NinjaLibrary = Montage.create(Object.prototype, { | 36 | exports.NinjaLibrary = Montage.create(Object.prototype, { |
37 | //////////////////////////////////////////////////////////////////// | 37 | //////////////////////////////////////////////////////////////////// |
38 | // | 38 | // |
39 | _chromeApi: { | 39 | _chromeApi: { |
40 | value: null | 40 | value: null |
@@ -42,7 +42,7 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
42 | //////////////////////////////////////////////////////////////////// | 42 | //////////////////////////////////////////////////////////////////// |
43 | // | 43 | // |
44 | chromeApi: { | 44 | chromeApi: { |
45 | get: function() {return this._chromeApi;}, | 45 | get: function() {return this._chromeApi;}, |
46 | set: function(value) {this._chromeApi = value;} | 46 | set: function(value) {this._chromeApi = value;} |
47 | }, | 47 | }, |
48 | //////////////////////////////////////////////////////////////////// | 48 | //////////////////////////////////////////////////////////////////// |
@@ -53,7 +53,7 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
53 | //////////////////////////////////////////////////////////////////// | 53 | //////////////////////////////////////////////////////////////////// |
54 | // | 54 | // |
55 | coreApi: { | 55 | coreApi: { |
56 | get: function() {return this._coreApi;}, | 56 | get: function() {return this._coreApi;}, |
57 | set: function(value) {this._coreApi = value;} | 57 | set: function(value) {this._coreApi = value;} |
58 | }, | 58 | }, |
59 | //////////////////////////////////////////////////////////////////// | 59 | //////////////////////////////////////////////////////////////////// |
@@ -64,7 +64,7 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
64 | //////////////////////////////////////////////////////////////////// | 64 | //////////////////////////////////////////////////////////////////// |
65 | // | 65 | // |
66 | libs: { | 66 | libs: { |
67 | get: function() {return this._libs;}, | 67 | get: function() {return this._libs;}, |
68 | set: function(value) {this._libs = value;} | 68 | set: function(value) {this._libs = value;} |
69 | }, | 69 | }, |
70 | //////////////////////////////////////////////////////////////////// | 70 | //////////////////////////////////////////////////////////////////// |
@@ -81,114 +81,114 @@ exports.NinjaLibrary = Montage.create(Object.prototype, { | |||
81 | // | 81 | // |
82 | copyLibToCloud: { | 82 | copyLibToCloud: { |
83 | value: function (path, libName, callback) { | 83 | value: function (path, libName, callback) { |
84 | var libCheck = this.coreApi.directoryExists({uri: path+libName}); | 84 | var libCheck = this.coreApi.directoryExists({uri: path+libName}); |
85 | //Checking for library to exists | 85 | //Checking for library to exists |
86 | if(libCheck.status === 404) { | 86 | if(libCheck.status === 404) { |
87 | //Getting contents to begin copying | 87 | //Getting contents to begin copying |
88 | this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { | 88 | this.chromeApi.directoryContents(this.chromeApi.fileSystem.root, function (contents) { |
89 | for (var i in contents) { | 89 | for (var i in contents) { |
90 | if (libName === contents[i].name) { | 90 | if (libName === contents[i].name) { |
91 | //Getting contents of library to be copied | 91 | //Getting contents of library to be copied |
92 | this.chromeApi.directoryContents(contents[i], function (lib) { | 92 | this.chromeApi.directoryContents(contents[i], function (lib) { |
93 | //Copying to cloud | 93 | //Copying to cloud |
94 | if (callback) { | 94 | if (callback) { |
95 | this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0, callback: callback.bind(this)}); | 95 | this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0, callback: callback.bind(this)}); |
96 | } else { | 96 | } else { |
97 | this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0}); | 97 | this.copyDirectoryToCloud(path, contents[i], path, {total: 0, copied: 0}); |
98 | } | 98 | } |
99 | }.bind(this)); | 99 | }.bind(this)); |
100 | break; | 100 | break; |
101 | } | 101 | } |
102 | } | 102 | } |
103 | }.bind(this)); | 103 | }.bind(this)); |
104 | } else if (libCheck.status === 204){ | 104 | } else if (libCheck.status === 204){ |
105 | //Already present, so sending success | 105 | //Already present, so sending success |
106 | if (callback) callback(true); | 106 | if (callback) callback(true); |
107 | } else { | 107 | } else { |
108 | if (callback) callback(false); | 108 | if (callback) callback(false); |
109 | } | 109 | } |
110 | } | 110 | } |
111 | }, | 111 | }, |
112 | //////////////////////////////////////////////////////////////////// | 112 | //////////////////////////////////////////////////////////////////// |
113 | // | 113 | // |
114 | copyDirectoryToCloud: { | 114 | copyDirectoryToCloud: { |
115 | value: function(root, folder, fileRoot, tracking) { | 115 | value: function(root, folder, fileRoot, tracking) { |
116 | //Setting up directory name | 116 | //Setting up directory name |
117 | if (folder.name) { | 117 | if (folder.name) { |
118 | var dir; | 118 | var dir; |
119 | if (root) { | 119 | if (root) { |
120 | dir = root+'/'+folder.name; | 120 | dir = root+'/'+folder.name; |
121 | } else { | 121 | } else { |
122 | dir = folder.name; | 122 | dir = folder.name; |
123 | } | 123 | } |
124 | //Creating directory | 124 | //Creating directory |
125 | if (!this.coreApi.createDirectory({uri: dir.replace(/\/\//gi, '/')})) { | 125 | if (!this.coreApi.createDirectory({uri: dir.replace(/\/\//gi, '/')})) { |
126 | //Error occured while creating folders | 126 | //Error occured while creating folders |
127 | return; //TODO: Add error handling | 127 | return; //TODO: Add error handling |
128 | } | 128 | } |
129 | } | 129 | } |
130 | //Checking for directory | 130 | //Checking for directory |
131 | if (folder.isDirectory) { | 131 | if (folder.isDirectory) { |
132 | //Using Chrome API to get directory contents | 132 | //Using Chrome API to get directory contents |
133 | this.chromeApi.directoryContents(folder, function (contents) { | 133 | this.chromeApi.directoryContents(folder, function (contents) { |
134 | //Looping through children of directory to copy | 134 | //Looping through children of directory to copy |
135 | for (var i in contents) { | 135 | for (var i in contents) { |
136 | //Checking for file or directory | 136 | //Checking for file or directory |
137 | if (contents[i].isDirectory) { | 137 | if (contents[i].isDirectory) { |
138 | //Recursive call if directory | 138 | //Recursive call if directory |
139 | this.copyDirectoryToCloud(dir, contents[i], fileRoot, tracking); | 139 | this.copyDirectoryToCloud(dir, contents[i], fileRoot, tracking); |
140 | } else if (contents[i].isFile){ | 140 | } else if (contents[i].isFile){ |
141 | // | 141 | // |
142 | tracking.total++; | 142 | tracking.total++; |
143 | //Copying file | 143 | //Copying file |
144 | this.chromeApi.fileContent(contents[i].fullPath, function (result) { | 144 | this.chromeApi.fileContent(contents[i].fullPath, function (result) { |
145 | //Using binary when copying files to allow images and such to work | 145 | //Using binary when copying files to allow images and such to work |
146 | var file = this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content}); | 146 | var file = this.coreApi.createFile({uri: (fileRoot+result.file.fullPath).replace(/\/\//gi, '/'), contents: result.content}); |
147 | //Checking for file copy success | 147 | //Checking for file copy success |
148 | if (file.status === 201) { | 148 | if (file.status === 201) { |
149 | tracking.copied++; | 149 | tracking.copied++; |
150 | } else { | 150 | } else { |
151 | //Error | 151 | //Error |
152 | tracking.callback(false); | 152 | tracking.callback(false); |
153 | } | 153 | } |
154 | //Checking for all files to be copied to make callback | 154 | //Checking for all files to be copied to make callback |
155 | if (tracking.copied === tracking.total && tracking.callback) { | 155 | if (tracking.copied === tracking.total && tracking.callback) { |
156 | tracking.callback(true); | 156 | tracking.callback(true); |
157 | } | 157 | } |
158 | }.bind(this)); | 158 | }.bind(this)); |
159 | } | 159 | } |
160 | } | 160 | } |
161 | }.bind(this)); | 161 | }.bind(this)); |
162 | } | 162 | } |
163 | } | 163 | } |
164 | }, | 164 | }, |
165 | //////////////////////////////////////////////////////////////////// | 165 | //////////////////////////////////////////////////////////////////// |
166 | // | 166 | // |
167 | synchronize: { | 167 | synchronize: { |
168 | value: function(chromeLibs, chrome) { | 168 | value: function(chromeLibs, chrome) { |
169 | //TODO: Remove | 169 | //TODO: Remove |
170 | window.wipeLibrary = this.deleteLibraries.bind(this); | 170 | window.wipeLibrary = this.deleteLibraries.bind(this); |
171 | //Getting instance of browser file API | 171 | //Getting instance of browser file API |
172 | this.chromeApi = chrome; | 172 | this.chromeApi = chrome; |
173 | //Local variables | 173 | //Local variables |
174 | var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = []; | 174 | var i, l, libs, libjson, xhr = new XMLHttpRequest(), tocopylibs = []; |
175 | //Getting known json list of libraries to copy to chrome (will be on a CDN later) | 175 | //Getting known json list of libraries to copy to chrome (will be on a CDN later) |
176 | xhr.open("GET", '/js/io/system/ninjalibrary.json', false); | 176 | xhr.open("GET", '/js/io/system/ninjalibrary.json', false); |
177 | xhr.send(); | 177 | xhr.send(); |
178 | //Checkng for correct reponse | 178 | //Checkng for correct reponse |
179 | if (xhr.readyState === 4) { | 179 | if (xhr.readyState === 4) { |
180 | //Parsing json libraries | 180 | //Parsing json libraries |
181 | libs = JSON.parse(xhr.response); | 181 | libs = JSON.parse(xhr.response); |
182 | //Storing JSON data | 182 | //Storing JSON data |
183 | this.libs = libs.libraries; | 183 | this.libs = libs.libraries; |
184 | //Checking for preview libraries to avoid duplicates | 184 | //Checking for preview libraries to avoid duplicates |
185 | if (chromeLibs.length > 0) { | 185 | if (chromeLibs.length > 0) { |
186 | //Looping through libraries on browser file system | 186 | //Looping through libraries on browser file system |
187 | for (i=0; chromeLibs[i]; i++) { | 187 | for (i=0; chromeLibs[i]; i++) { |
188 | for (var j in libs.libraries) { | 188 | for (var j in libs.librari |