aboutsummaryrefslogtreecommitdiff
path: root/js/mediators/io-mediator.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r--js/mediators/io-mediator.js113
1 files changed, 82 insertions, 31 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index e763c67c..0a851a4d 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 //
@@ -211,36 +214,59 @@ exports.IoMediator = Montage.create(Component, {
211 enumerable: false, 214 enumerable: false,
212 value: function (template) { 215 value: function (template) {
213 //Injecting head and body into old document 216 //Injecting head and body into old document
214 template.document.content.document.body.innerHTML = template.body;
215 template.document.content.document.head.innerHTML = template.head; 217 template.document.content.document.head.innerHTML = template.head;
218 template.document.content.document.body.innerHTML = template.body;
216 //Getting all CSS (style or link) tags 219 //Getting all CSS (style or link) tags
217 var styletags = template.document.content.document.getElementsByTagName('style'), 220 var styletags = template.document.content.document.getElementsByTagName('style'),
218 linktags = template.document.content.document.getElementsByTagName('link'); 221 linktags = template.document.content.document.getElementsByTagName('link'),
219 //Looping through link tags and removing file recreated elements 222 toremovetags = [];
220 for (var j in styletags) { 223 //Getting styles tags to be removed from document
221 if (styletags[j].getAttribute) { 224 if (styletags.length) {
222 if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll 225 for (var j=0; j<styletags.length; j++) {
223 try { 226 if (styletags[j].getAttribute) {
224 //Checking head first 227 if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {
225 template.document.content.document.head.removeChild(styletags[j]); 228 toremovetags.push(styletags[j]);
226 } catch (e) {
227 try {
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 } 229 }
234
235 } 230 }
236 } 231 }
237 } 232 }
238 //TODO: Add logic to only enble tags we disabled 233 //Removing styles tags from document
234 for (var h=0; toremovetags[h]; h++) {
235 try {
236 //Checking head first
237 template.document.content.document.head.removeChild(toremovetags[h]);
238 } catch (e) {
239 try {
240 //Checking body if not in head
241 template.document.content.document.body.removeChild(toremovetags[h]);
242 } catch (e) {
243 //Error, not found!
244 }
245 }
246 }
247 //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all)
239 for (var l in linktags) { 248 for (var l in linktags) {
240 if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll 249 if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll
241 linktags[l].removeAttribute('disabled'); 250 for (var p=0; toremovetags[p]; p++) {
251 if (toremovetags[p].getAttribute('data-ninja-file-url') === ('/'+linktags[l].getAttribute('href'))) {
252 if (!toremovetags[p].getAttribute('data-ninja-disabled')) {
253 linktags[l].removeAttribute('disabled');
254 }
255 }
256 }
242 } 257 }
243 } 258 }
259
260
261 /////////////////////////////////////////////////////////////////////////////////////////
262 /////////////////////////////////////////////////////////////////////////////////////////
263
264 //TODO: Add logic for parse CSS string correct URLs since referencing is lost
265
266 /////////////////////////////////////////////////////////////////////////////////////////
267 /////////////////////////////////////////////////////////////////////////////////////////
268
269
244 //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) 270 //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS)
245 if (template.styles) { 271 if (template.styles) {
246 //Getting all style tags 272 //Getting all style tags
@@ -252,10 +278,12 @@ exports.IoMediator = Montage.create(Component, {
252 if (template.styles[i].ownerNode.getAttribute) { 278 if (template.styles[i].ownerNode.getAttribute) {
253 //Checking for node not to be loaded from file 279 //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')) { 280 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 281 if(docStyles[styleCounter]) {
256 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); 282 //Inseting data from rules array into tag as string
257 //Syncing <style> tags count since it might be mixed with <link> 283 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules);
258 styleCounter++; 284 //Syncing <style> tags count since it might be mixed with <link>
285 styleCounter++;
286 }
259 } 287 }
260 } 288 }
261 } 289 }
@@ -281,9 +309,11 @@ exports.IoMediator = Montage.create(Component, {
281 if (template.css[i].ownerNode.getAttribute) { 309 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 310 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 311 //Inseting data from rules array into <style> as string
284 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); 312 if (docStyles[styleCounter]) {
285 styleCounter++; 313 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules);
286 } else { 314 styleCounter++;
315 }
316 } else if (!template.css[i].ownerNode.getAttribute('data-ninja-template')){
287 //Checking for attributes to be added to tag upon saving 317 //Checking for attributes to be added to tag upon saving
288 for (var k in docLinks) { 318 for (var k in docLinks) {
289 if (docLinks[k].getAttribute) { 319 if (docLinks[k].getAttribute) {
@@ -300,8 +330,29 @@ exports.IoMediator = Montage.create(Component, {
300 } 330 }
301 } 331 }
302 } 332 }
333 var local, regex, fileCouldDirUrl, adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url');
334 //TODO: Assure logic for local directory
335 local = cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-2])[0] || cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0] || cssUrl.split(cssUrl.split('/')[0])[0];
336 //
337 fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+local).replace(/\/\//gi, '/'));
338 //
339 regex = new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi');
340 //
341 if (local.split('/').length > 2) {
342 adjCss = adjCss.replace(regex, '../');
343 } else {
344 adjCss = adjCss.replace(regex, '');
345 }
346
347
348 //console.log(adjCss);
349 //console.log(fileCouldDirUrl);
350 //return;
351
352
353
303 //Saving data from rules array converted to string into <link> file 354 //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)}); 355 var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: adjCss});
305 } 356 }
306 } 357 }
307 } 358 }
@@ -350,7 +401,7 @@ exports.IoMediator = Montage.create(Component, {
350 webgltag.innerHTML = json; 401 webgltag.innerHTML = json;
351 } 402 }
352 // 403 //
353 return this.getPrettyHtml(template.document.content.document.documentElement.outerHTML.replace(this.appTemplatesUrl, '')); 404 return this.getPrettyHtml(template.document.content.document.documentElement.outerHTML.replace(this.getAppTemplatesUrlRegEx(), ''));
354 } 405 }
355 }, 406 },
356 //////////////////////////////////////////////////////////////////// 407 ////////////////////////////////////////////////////////////////////
@@ -368,7 +419,7 @@ exports.IoMediator = Montage.create(Component, {
368 } 419 }
369 } 420 }
370 //Returning the CSS string 421 //Returning the CSS string
371 return this.getPrettyCss(css.replace(this.appTemplatesUrl, '')); 422 return this.getPrettyCss(css.replace(this.getAppTemplatesUrlRegEx(), ''));
372 } 423 }
373 }, 424 },
374 //////////////////////////////////////////////////////////////////// 425 ////////////////////////////////////////////////////////////////////