diff options
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r-- | js/mediators/io-mediator.js | 121 |
1 files changed, 113 insertions, 8 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index 56869839..e5eb1fc6 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -195,18 +195,121 @@ exports.IoMediator = Montage.create(Component, { | |||
195 | } | 195 | } |
196 | }, | 196 | }, |
197 | //////////////////////////////////////////////////////////////////// | 197 | //////////////////////////////////////////////////////////////////// |
198 | //TODO: Expand to allow more templates | 198 | //TODO: Expand to allow more templates, clean up variables |
199 | parseNinjaTemplateToHtml: { | 199 | parseNinjaTemplateToHtml: { |
200 | enumerable: false, | 200 | enumerable: false, |
201 | value: function (template) { | 201 | value: function (template) { |
202 | // | 202 | //Injecting head and body into old document |
203 | template.document.content.document.body.innerHTML = template.body; | 203 | template.document.content.document.body.innerHTML = template.body; |
204 | template.document.content.document.head.innerHTML = template.head; | 204 | template.document.content.document.head.innerHTML = template.head; |
205 | //TODO: Remove temp fix for styles | 205 | //Getting all CSS (style or link) tags |
206 | if (template.style) { | 206 | var styletags = template.document.content.document.getElementsByTagName('style'), |
207 | template.document.content.document.head.getElementsByTagName('style')[0].innerHTML = this.getCssFromRules(template.style.cssRules); | 207 | linktags = template.document.content.document.getElementsByTagName('link'), |
208 | url = new RegExp(window.location.protocol+'//'+window.location.host+'/js/document/templates/montage-html/', 'gi'); | ||
209 | //Looping through link tags and removing file recreated elements | ||
210 | for (var j in styletags) { | ||
211 | if (styletags[j].getAttribute) { | ||
212 | if(styletags[j].getAttribute('ninjauri') !== null) {//TODO: Use querySelectorAll | ||
213 | try { | ||
214 | //Checking head first | ||
215 | template.document.content.document.head.removeChild(styletags[j]); | ||
216 | } catch (e) { | ||
217 | try { | ||
218 | //Checking body if not in head | ||
219 | template.document.content.document.body.removeChild(styletags[j]); | ||
220 | } catch (e) { | ||
221 | //Error, not found! | ||
222 | } | ||
223 | } | ||
224 | |||
225 | } | ||
226 | } | ||
227 | } | ||
228 | //TODO: Add logic to only enble tags we disabled | ||
229 | for (var l in linktags) { | ||
230 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll | ||
231 | linktags[l].removeAttribute('disabled'); | ||
232 | } | ||
233 | } | ||
234 | //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) | ||
235 | if (template.styles) { | ||
236 | //Getting all style tags | ||
237 | var styleCounter = 0, | ||
238 | docStyles = template.document.content.document.getElementsByTagName('style'); | ||
239 | //Looping through all style tags | ||
240 | for(var i in template.styles) { | ||
241 | if (template.styles[i].ownerNode) { | ||
242 | if (template.styles[i].ownerNode.getAttribute) { | ||
243 | //Checking for node not to be loaded from file | ||
244 | if (template.styles[i].ownerNode.getAttribute('ninjauri') === null) { | ||
245 | //Inseting data from rules array into tag as string | ||
246 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); | ||
247 | //Syncing <style> tags count since it might be mixed with <link> | ||
248 | styleCounter++; | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | } | ||
253 | } else if (template.css) { | ||
254 | //Getting all style and link tags | ||
255 | var styleCounter = 0, | ||
256 | docStyles = template.document.content.document.getElementsByTagName('style'), | ||
257 | docLinks = template.document.content.document.getElementsByTagName('link'); | ||
258 | for(var i in template.css) { | ||
259 | if (template.css[i].ownerNode) { | ||
260 | if (template.css[i].ownerNode.getAttribute) { | ||
261 | if (template.css[i].ownerNode.getAttribute('ninjauri') === null) {//TODO: Use querySelectorAll | ||
262 | //Inseting data from rules array into <style> as string | ||
263 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); | ||
264 | styleCounter++; | ||
265 | } else { | ||
266 | //Saving data from rules array converted to string into <link> file | ||
267 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('ninjauri'), contents: this.getCssFromRules(template.css[i].cssRules)}); | ||
268 | } | ||
269 | } | ||
270 | } | ||
271 | } | ||
208 | } | 272 | } |
209 | return template.document.content.document.documentElement.outerHTML; | 273 | //Checking for webGL elements in document |
274 | if (template.webgl.length) { | ||
275 | // | ||
276 | var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'); | ||
277 | // | ||
278 | for (var i in scripts) { | ||
279 | if (scripts[i].getAttribute) { | ||
280 | if (scripts[i].getAttribute('data-ninja-webgl') !== null) {//TODO: Use querySelectorAll | ||
281 | matchingtags.push(scripts[i]); | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | // | ||
286 | if (matchingtags.length) { | ||
287 | if (matchingtags.length === 1) { | ||
288 | webgltag = matchingtags[0]; | ||
289 | } else { | ||
290 | //TODO: Add logic to handle multiple tags, perhaps combine to one | ||
291 | webgltag = matchingtags[matchingtags.length-1]; //Saving all data to last one... | ||
292 | } | ||
293 | } | ||
294 | // | ||
295 | if (!webgltag) { | ||
296 | webgltag = template.document.content.document.createElement('script'); | ||
297 | webgltag.setAttribute('data-ninja-webgl', 'true'); | ||
298 | template.document.content.document.head.appendChild(webgltag); | ||
299 | } | ||
300 | // | ||
301 | json = '\n\t\t({\n\t\t\t"version": "X.X.X.X",\n\t\t\t"data": ['; | ||
302 | // | ||
303 | for (var j=0; template.webgl[j]; j++) { | ||
304 | json += '\n\t\t\t\t"'+escape(template.webgl[j])+'"'; | ||
305 | } | ||
306 | // | ||
307 | json += ']\n\t\t})\n\t'; | ||
308 | // | ||
309 | webgltag.innerHTML = json; | ||
310 | } | ||
311 | // | ||
312 | return template.document.content.document.documentElement.outerHTML.replace(url, ''); | ||
210 | } | 313 | } |
211 | }, | 314 | }, |
212 | //////////////////////////////////////////////////////////////////// | 315 | //////////////////////////////////////////////////////////////////// |
@@ -215,7 +318,7 @@ exports.IoMediator = Montage.create(Component, { | |||
215 | enumerable: false, | 318 | enumerable: false, |
216 | value: function (list) { | 319 | value: function (list) { |
217 | //Variable to store CSS definitions | 320 | //Variable to store CSS definitions |
218 | var i, str, css = ''; | 321 | var i, str, url, css = ''; |
219 | //Looping through list | 322 | //Looping through list |
220 | if (list && list.length > 0) { | 323 | if (list && list.length > 0) { |
221 | //Adding each list item to string and also adding breaks | 324 | //Adding each list item to string and also adding breaks |
@@ -227,8 +330,10 @@ exports.IoMediator = Montage.create(Component, { | |||
227 | css += '\n'+str; | 330 | css += '\n'+str; |
228 | } | 331 | } |
229 | } | 332 | } |
333 | //TODO: Add better logic for creating this string | ||
334 | url = new RegExp(window.location.protocol+'//'+window.location.host+'/js/document/templates/montage-html/', 'gi'); | ||
230 | //Returning the CSS string | 335 | //Returning the CSS string |
231 | return css; | 336 | return css.replace(url, ''); |
232 | } | 337 | } |
233 | } | 338 | } |
234 | //////////////////////////////////////////////////////////////////// | 339 | //////////////////////////////////////////////////////////////////// |