diff options
Diffstat (limited to 'js/mediators')
-rw-r--r-- | js/mediators/io-mediator.js | 167 |
1 files changed, 134 insertions, 33 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 65218526..dcafb38d 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 | // |
@@ -210,35 +213,51 @@ exports.IoMediator = Montage.create(Component, { | |||
210 | parseNinjaTemplateToHtml: { | 213 | parseNinjaTemplateToHtml: { |
211 | enumerable: false, | 214 | enumerable: false, |
212 | value: function (template) { | 215 | value: function (template) { |
216 | var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])); | ||
217 | regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); | ||
213 | //Injecting head and body into old document | 218 | //Injecting head and body into old document |
214 | template.document.content.document.body.innerHTML = template.body; | 219 | template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, ''); |
215 | template.document.content.document.head.innerHTML = template.head; | 220 | template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, ''); |
216 | //Getting all CSS (style or link) tags | 221 | //Getting all CSS (style or link) tags |
217 | var styletags = template.document.content.document.getElementsByTagName('style'), | 222 | var styletags = template.document.content.document.getElementsByTagName('style'), |
218 | linktags = template.document.content.document.getElementsByTagName('link'); | 223 | linktags = template.document.content.document.getElementsByTagName('link'), |
219 | //Looping through link tags and removing file recreated elements | 224 | toremovetags = []; |
220 | for (var j in styletags) { | 225 | //Getting styles tags to be removed from document |
221 | if (styletags[j].getAttribute) { | 226 | if (styletags.length) { |
222 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll | 227 | for (var j=0; j<styletags.length; j++) { |
223 | try { | 228 | if (styletags[j].getAttribute) { |
224 | //Checking head first | 229 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) { |
225 | template.document.content.document.head.removeChild(styletags[j]); | 230 | toremovetags.push(styletags[j]); |
226 | } catch (e) { | 231 | } else if (styletags[j].getAttribute('data-ninja-external-url')) { |
227 | try { | 232 | 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 | } | 233 | } |
234 | |||
235 | } | 234 | } |
236 | } | 235 | } |
237 | } | 236 | } |
238 | //TODO: Add logic to only enble tags we disabled | 237 | //Removing styles tags from document |
238 | for (var h=0; toremovetags[h]; h++) { | ||
239 | try { | ||
240 | //Checking head first | ||
241 | template.document.content.document.head.removeChild(toremovetags[h]); | ||
242 | } catch (e) { | ||
243 | try { | ||
244 | //Checking body if not in head | ||
245 | template.document.content.document.body.removeChild(toremovetags[h]); | ||
246 | } catch (e) { | ||
247 | //Error, not found! | ||
248 | } | ||
249 | } | ||
250 | } | ||
251 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) | ||
239 | for (var l in linktags) { | 252 | for (var l in linktags) { |
240 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll | 253 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll |
241 | linktags[l].removeAttribute('disabled'); | 254 | for (var p=0; toremovetags[p]; p++) { |
255 | if (toremovetags[p].getAttribute('href') === linktags[l].getAttribute('href')) { | ||
256 | if (!toremovetags[p].getAttribute('data-ninja-disabled')) { | ||
257 | linktags[l].removeAttribute('disabled'); | ||
258 | } | ||
259 | } | ||
260 | } | ||
242 | } | 261 | } |
243 | } | 262 | } |
244 | //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) | 263 | //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) |
@@ -252,10 +271,12 @@ exports.IoMediator = Montage.create(Component, { | |||
252 | if (template.styles[i].ownerNode.getAttribute) { | 271 | if (template.styles[i].ownerNode.getAttribute) { |
253 | //Checking for node not to be loaded from file | 272 | //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')) { | 273 | 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 | 274 | if(docStyles[styleCounter]) { |
256 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); | 275 | //Inseting data from rules array into tag as string |
257 | //Syncing <style> tags count since it might be mixed with <link> | 276 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); |
258 | styleCounter++; | 277 | //Syncing <style> tags count since it might be mixed with <link> |
278 | styleCounter++; | ||
279 | } | ||
259 | } | 280 | } |
260 | } | 281 | } |
261 | } | 282 | } |
@@ -281,9 +302,11 @@ exports.IoMediator = Montage.create(Component, { | |||
281 | if (template.css[i].ownerNode.getAttribute) { | 302 | 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 | 303 | 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 | 304 | //Inseting data from rules array into <style> as string |
284 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); | 305 | if (docStyles[styleCounter] && !template.css[i].ownerNode.getAttribute('data-ninja-external-url')) { |
285 | styleCounter++; | 306 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); |
286 | } else { | 307 | styleCounter++; |
308 | } | ||
309 | } else if (!template.css[i].ownerNode.getAttribute('data-ninja-template')){ | ||
287 | //Checking for attributes to be added to tag upon saving | 310 | //Checking for attributes to be added to tag upon saving |
288 | for (var k in docLinks) { | 311 | for (var k in docLinks) { |
289 | if (docLinks[k].getAttribute) { | 312 | if (docLinks[k].getAttribute) { |
@@ -300,8 +323,34 @@ exports.IoMediator = Montage.create(Component, { | |||
300 | } | 323 | } |
301 | } | 324 | } |
302 | } | 325 | } |
326 | |||
327 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
328 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
329 | |||
330 | |||
331 | var cleanedCss, | ||
332 | dirtyCss = this.getCssFromRules(template.css[i].cssRules), | ||
333 | fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'), | ||
334 | fileRootUrl = this.application.ninja.coreIoApi.rootUrl+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0], | ||
335 | 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)); | ||
336 | |||
337 | function parseNinjaUrl (url) { | ||
338 | if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) { | ||
339 | return this.getUrlfromNinjaUrl(url, fileRootUrl, fileUrl); | ||
340 | } else { | ||
341 | return url; | ||
342 | } | ||
343 | } | ||
344 | |||
345 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
346 | /////////////////////////////////////////////////////////////////////////////////////////// | ||
347 | |||
348 | |||
349 | |||
350 | |||
303 | //Saving data from rules array converted to string into <link> file | 351 | //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)}); | 352 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: cleanedCss}); |
353 | //TODO: Add error handling for saving files | ||
305 | } | 354 | } |
306 | } | 355 | } |
307 | } | 356 | } |
@@ -349,8 +398,60 @@ exports.IoMediator = Montage.create(Component, { | |||
349 | //Setting string in tag | 398 | //Setting string in tag |
350 | webgltag.innerHTML = json; | 399 | webgltag.innerHTML = json; |
351 | } | 400 | } |
401 | var cleanHTML = template.document.content.document.documentElement.outerHTML.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaRootUrl.bind(this)); | ||
402 | //console.log(this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), ''))); | ||
403 | function parseNinjaRootUrl (url) { | ||
404 | if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) { | ||
405 | return this.getUrlfromNinjaUrl(url, rootUrl, rootUrl.replace(new RegExp((this.application.ninja.coreIoApi.rootUrl).replace(/\//gi, '\\\/'), 'gi'), '')+'file.ext');//Wrong parameters | ||
406 | } else { | ||
407 | return url; | ||
408 | } | ||
409 | } | ||
410 | //console.log(rootUrl, this.application.ninja.coreIoApi.rootUrl, this.application.ninja.documentController.documentHackReference.root, this.application.ninja.coreIoApi.cloudData.root); | ||
411 | //console.log(this.getPrettyHtml(template.document.content.document.documentElement.outerHTML)); | ||
412 | //return; | ||
413 | // | ||
414 | return this.getPrettyHtml(cleanHTML.replace(this.getAppTemplatesUrlRegEx(), '')); | ||
415 | } | ||
416 | }, | ||
417 | //////////////////////////////////////////////////////////////////// | ||
418 | // | ||
419 | getUrlfromNinjaUrl: { | ||
420 | enumerable: false, | ||
421 | value: function (url, fileRootUrl, fileUrl) { | ||
422 | //console.log("Params: ", url, fileRootUrl, fileUrl); | ||
423 | //console.log("Getting: " + url); | ||
424 | // | ||
425 | if (url.indexOf(fileRootUrl) !== -1) { | ||
426 | url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), ''); | ||
427 | } else { | ||
428 | //TODO: Clean up vars | ||
429 | var assetsDirs = (url.replace(new RegExp((this.application.ninja.coreIoApi.rootUrl).replace(/\//gi, '\\\/'), 'gi'), '')).split('/'); | ||
430 | var fileDirs = (fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0]).split('/'); | ||