diff options
Diffstat (limited to 'js/mediators')
-rw-r--r-- | js/mediators/io-mediator.js | 234 |
1 files changed, 187 insertions, 47 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 65218526..c22d95be 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -41,9 +41,12 @@ exports.IoMediator = Montage.create(Component, { | |||
41 | }, | 41 | }, |
42 | //////////////////////////////////////////////////////////////////// | 42 | //////////////////////////////////////////////////////////////////// |
43 | // | 43 | // |
44 | appTemplatesUrl: { | 44 | getAppTemplatesUrlRegEx: { |
45 | enumerable: false, | 45 | enumerable: false, |
46 | value: new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi') | 46 | value: function () { |
47 | var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi'); | ||
48 | return regex; | ||
49 | } | ||
47 | }, | 50 | }, |
48 | //////////////////////////////////////////////////////////////////// | 51 | //////////////////////////////////////////////////////////////////// |
49 | // | 52 | // |
@@ -149,17 +152,7 @@ exports.IoMediator = Montage.create(Component, { | |||
149 | // | 152 | // |
150 | switch (file.mode) { | 153 | switch (file.mode) { |
151 | case 'html': | 154 | case 'html': |
152 | //Copy webGL library if needed | 155 | |
153 | if (file.webgl && file.webgl.length > 0) { | ||
154 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { | ||
155 | //Checking for RDGE library to be available | ||
156 | if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') { | ||
157 | this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(file.document.root, (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase()); | ||
158 | } else { | ||
159 | //TODO: Error handle no available library to copy | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | 156 | ||
164 | //TODO: Add check for Monatage library to copy | 157 | //TODO: Add check for Monatage library to copy |
165 | 158 | ||
@@ -210,35 +203,51 @@ exports.IoMediator = Montage.create(Component, { | |||
210 | parseNinjaTemplateToHtml: { | 203 | parseNinjaTemplateToHtml: { |
211 | enumerable: false, | 204 | enumerable: false, |
212 | value: function (template) { | 205 | value: function (template) { |
206 | var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])); | ||
207 | regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); | ||
213 | //Injecting head and body into old document | 208 | //Injecting head and body into old document |
214 | template.document.content.document.body.innerHTML = template.body; | 209 | template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, ''); |
215 | template.document.content.document.head.innerHTML = template.head; | 210 | template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, ''); |
216 | //Getting all CSS (style or link) tags | 211 | //Getting all CSS (style or link) tags |
217 | var styletags = template.document.content.document.getElementsByTagName('style'), | 212 | var styletags = template.document.content.document.getElementsByTagName('style'), |
218 | linktags = template.document.content.document.getElementsByTagName('link'); | 213 | linktags = template.document.content.document.getElementsByTagName('link'), |
219 | //Looping through link tags and removing file recreated elements | 214 | toremovetags = []; |
220 | for (var j in styletags) { | 215 | //Getting styles tags to be removed from document |
221 | if (styletags[j].getAttribute) { | 216 | if (styletags.length) { |
222 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll | 217 | for (var j=0; j<styletags.length; j++) { |
223 | try { | 218 | if (styletags[j].getAttribute) { |
224 | //Checking head first | 219 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) { |
225 | template.document.content.document.head.removeChild(styletags[j]); | 220 | toremovetags.push(styletags[j]); |
226 | } catch (e) { | 221 | } else if (styletags[j].getAttribute('data-ninja-external-url')) { |
227 | try { | 222 | toremovetags.push(styletags[j]); |
228 | //Checking body if not in head | ||
229 | template.document.content.document.body.removeChild(styletags[j]); | ||
230 | } catch (e) { | ||
231 | //Error, not found! | ||
232 | } | ||
233 | } | 223 | } |
234 | |||
235 | } | 224 | } |
236 | } | 225 | } |
237 | } | 226 | } |
238 | //TODO: Add logic to only enble tags we disabled | 227 | //Removing styles tags from document |
228 | for (var h=0; toremovetags[h]; h++) { | ||
229 | try { | ||
230 | //Checking head first | ||
231 | template.document.content.document.head.removeChild(toremovetags[h]); | ||
232 | } catch (e) { | ||
233 | try { | ||
234 | //Checking body if not in head | ||
235 | template.document.content.document.body.removeChild(toremovetags[h]); | ||
236 | } catch (e) { | ||
237 | //Error, not found! | ||
238 | } | ||
239 | } | ||
240 | } | ||
241 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) | ||
239 | for (var l in linktags) { | 242 | for (var l in linktags) { |
240 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll | 243 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll |
241 | linktags[l].removeAttribute('disabled'); | 244 | for (var p=0; toremovetags[p]; p++) { |
245 | if (toremovetags[p].getAttribute('href') === linktags[l].getAttribute('href')) { | ||
246 | if (!toremovetags[p].getAttribute('data-ninja-disabled')) { | ||
247 | linktags[l].removeAttribute('disabled'); | ||
248 | } | ||
249 | } | ||
250 | } | ||
242 | } | 251 | } |
243 | } | 252 | } |
244 | //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) | 253 | //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) |
@@ -252,10 +261,12 @@ exports.IoMediator = Montage.create(Component, { | |||
252 | if (template.styles[i].ownerNode.getAttribute) { | 261 | if (template.styles[i].ownerNode.getAttribute) { |
253 | //Checking for node not to be loaded from file | 262 | //Checking for node not to be loaded from file |
254 | if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.styles[i].ownerNode.getAttribute('data-ninja-template')) { | 263 | if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.styles[i].ownerNode.getAttribute('data-ninja-template')) { |
255 | //Inseting data from rules array into tag as string | 264 | if(docStyles[styleCounter]) { |
256 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); | 265 | //Inseting data from rules array into tag as string |
257 | //Syncing <style> tags count since it might be mixed with <link> | 266 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); |
258 | styleCounter++; | 267 | //Syncing <style> tags count since it might be mixed with <link> |
268 | styleCounter++; | ||
269 | } | ||
259 | } | 270 | } |
260 | } | 271 | } |
261 | } | 272 | } |
@@ -281,9 +292,11 @@ exports.IoMediator = Montage.create(Component, { | |||
281 | if (template.css[i].ownerNode.getAttribute) { | 292 | if (template.css[i].ownerNode.getAttribute) { |
282 | if (template.css[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.css[i].ownerNode.getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll | 293 | if (template.css[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.css[i].ownerNode.getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll |
283 | //Inseting data from rules array into <style> as string | 294 | //Inseting data from rules array into <style> as string |
284 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); | 295 | if (docStyles[styleCounter] && !template.css[i].ownerNode.getAttribute('data-ninja-external-url')) { |
285 | styleCounter++; | 296 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); |
286 | } else { | 297 | styleCounter++; |
298 | } | ||
299 | } else if (!template.css[i].ownerNode.getAttribute('data-ninja-template')){ | ||
287 | //Checking for attributes to be added to tag upon saving | 300 | //Checking for attributes to be added to tag upon saving |
288 | for (var k in docLinks) { | 301 | for (var k in docLinks) { |
289 | if (docLinks[k].getAttribute) { | 302 | if (docLinks[k].getAttribute) { |
@@ -300,23 +313,67 @@ exports.IoMediator = Montage.create(Component, { | |||
300 | } | 313 | } |
301 | } | 314 | } |
302 | } | 315 | } |
316 | |||
317 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
318 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
319 | |||
320 | |||
321 | var cleanedCss, | ||
322 | dirtyCss = this.getCssFromRules(template.css[i].cssRules), | ||
323 | fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'), | ||
324 | fileRootUrl = this.application.ninja.coreIoApi.rootUrl+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0], | ||
325 | cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this)); | ||
326 | |||
327 | function parseNinjaUrl (url) { | ||
328 | if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) { | ||
329 | return this.getUrlfromNinjaUrl(url, fileRootUrl, fileUrl); | ||
330 | } else { | ||
331 | return url; | ||
332 | } | ||
333 | } | ||
334 | |||
335 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
336 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
337 | |||
338 | |||
339 | |||
340 | |||
303 | //Saving data from rules array converted to string into <link> file | 341 | //Saving data from rules array converted to string into <link> file |
304 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: this.getCssFromRules(template.css[i].cssRules)}); | 342 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: cleanedCss}); |
343 | //TODO: Add error handling for saving files | ||
305 | } | 344 | } |
306 | } | 345 | } |
307 | } | 346 | } |
308 | } | 347 | } |
309 | } | 348 | } |
310 | //Checking for webGL elements in document | 349 | //Checking for webGL elements in document |
311 | if (template.webgl && template.webgl.length) { | 350 | if (template.webgl && template.webgl.length > 0) { |
351 | var rdgeDirName, rdgeVersion; | ||
352 | //Copy webGL library if needed | ||
353 | for (var i in this.application.ninja.coreIoApi.ninjaLibrary.libs) { | ||
354 | //Checking for RDGE library to be available | ||
355 | if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') { | ||
356 | rdgeDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name+this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase(); | ||
357 | rdgeVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version; | ||