diff options
Diffstat (limited to 'js/mediators')
-rw-r--r-- | js/mediators/io-mediator.js | 92 |
1 files changed, 62 insertions, 30 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index e763c67c..cc97db5e 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,8 +309,10 @@ 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); |
314 | styleCounter++; | ||
315 | } | ||
286 | } else { | 316 | } else { |
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) { |
@@ -300,8 +330,10 @@ exports.IoMediator = Montage.create(Component, { | |||
300 | } | 330 | } |
301 | } | 331 | } |
302 | } | 332 | } |
333 | var adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-uri'); | ||
334 | //console.log((template.css[i].ownerNode.getAttribute('data-ninja-uri')));//cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0] | ||
303 | //Saving data from rules array converted to string into <link> file | 335 | //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)}); | 336 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: adjCss}); |
305 | } | 337 | } |
306 | } | 338 | } |
307 | } | 339 | } |
@@ -350,7 +382,7 @@ exports.IoMediator = Montage.create(Component, { | |||
350 | webgltag.innerHTML = json; | 382 | webgltag.innerHTML = json; |
351 | } | 383 | } |
352 | // | 384 | // |
353 | return this.getPrettyHtml(template.document.content.document.documentElement.outerHTML.replace(this.appTemplatesUrl, '')); | 385 | return this.getPrettyHtml(template.document.content.document.documentElement.outerHTML.replace(this.getAppTemplatesUrlRegEx(), '')); |
354 | } | 386 | } |
355 | }, | 387 | }, |
356 | //////////////////////////////////////////////////////////////////// | 388 | //////////////////////////////////////////////////////////////////// |
@@ -368,7 +400,7 @@ exports.IoMediator = Montage.create(Component, { | |||
368 | } | 400 | } |
369 | } | 401 | } |
370 | //Returning the CSS string | 402 | //Returning the CSS string |
371 | return this.getPrettyCss(css.replace(this.appTemplatesUrl, '')); | 403 | return this.getPrettyCss(css.replace(this.getAppTemplatesUrlRegEx(), '')); |
372 | } | 404 | } |
373 | }, | 405 | }, |
374 | //////////////////////////////////////////////////////////////////// | 406 | //////////////////////////////////////////////////////////////////// |