aboutsummaryrefslogtreecommitdiff
path: root/js/mediators
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-05-08 17:33:13 -0700
committerJose Antonio Marquez2012-05-08 17:33:13 -0700
commita1e8540f5656e62db6a89f3af7829be6b259b7ed (patch)
tree1b8a445cf5c145a9622813da88555596640e7e37 /js/mediators
parent4504972c1f3b9bf1d02a4484a07a8a85cf9ccee2 (diff)
downloadninja-a1e8540f5656e62db6a89f3af7829be6b259b7ed.tar.gz
Adding SAVE for I/O
Adding save functionality to new template. Need to implement user UI for prompts and also clean up...
Diffstat (limited to 'js/mediators')
-rw-r--r--js/mediators/io-mediator.js89
1 files changed, 52 insertions, 37 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index c20ae8f7..6d146c97 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -147,21 +147,21 @@ exports.IoMediator = Montage.create(Component, {
147 // 147 //
148 fileSave: { 148 fileSave: {
149 enumerable: false, 149 enumerable: false,
150 value: function (file, callback) { 150 value: function (doc, callback) {
151 // 151 //
152 var contents, save; 152 var contents, save;
153 // 153 //
154 switch (file.mode) { 154 switch (doc.mode) {
155 case 'html': 155 case 'html':
156 //Getting content from function to properly handle saving assets (as in external if flagged) 156 //Getting content from function to properly handle saving assets (as in external if flagged)
157 contents = this.parseNinjaTemplateToHtml(file); 157 contents = this.parseNinjaTemplateToHtml(doc);
158 break; 158 break;
159 default: 159 default:
160 contents = file.content; 160 contents = doc.content;
161 break; 161 break;
162 } 162 }
163 //Making call to save file 163 //Making call to save file
164 save = this.fio.saveFile({ uri: file.document.uri, contents: contents }); 164 save = this.fio.saveFile({ uri: doc.file.uri, contents: contents });
165 //Checking for callback 165 //Checking for callback
166 if (callback) callback(save); 166 if (callback) callback(save);
167 } 167 }
@@ -192,7 +192,7 @@ exports.IoMediator = Montage.create(Component, {
192 //Setting content to temp 192 //Setting content to temp
193 doc.getElementsByTagName('html')[0].innerHTML = html; 193 doc.getElementsByTagName('html')[0].innerHTML = html;
194 //Creating return object 194 //Creating return object
195 return { head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc }; 195 return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc};
196 } 196 }
197 }, 197 },
198 //////////////////////////////////////////////////////////////////// 198 ////////////////////////////////////////////////////////////////////
@@ -203,12 +203,26 @@ exports.IoMediator = Montage.create(Component, {
203 var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])); 203 var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
204 regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); 204 regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi');
205 //Injecting head and body into old document 205 //Injecting head and body into old document
206 template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, ''); 206 template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, '');
207 template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, ''); 207 template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, '');
208 //Getting all CSS (style or link) tags 208 //Getting all CSS (style or link) tags
209 var styletags = template.document.content.document.getElementsByTagName('style'), 209 var styletags = template.file.content.document.getElementsByTagName('style'),
210 linktags = template.document.content.document.getElementsByTagName('link'), 210 linktags = template.file.content.document.getElementsByTagName('link'),
211 toremovetags = []; 211 toremovetags = [],
212 njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'),
213 basetags = template.file.content.document.getElementsByTagName('base');
214
215 //////////////////////////////////////////////////
216 //TODO: Remove, temp hack to avoid montage
217 for (var g in basetags) {
218 if (basetags[g].getAttribute) toremovetags.push(basetags[g]);
219 }
220 //////////////////////////////////////////////////
221
222 //
223 for (var f in njtemplatetags) {
224 if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]);
225 }
212 //Getting styles tags to be removed from document 226 //Getting styles tags to be removed from document
213 if (styletags.length) { 227 if (styletags.length) {
214 for (var j = 0; j < styletags.length; j++) { 228 for (var j = 0; j < styletags.length; j++) {
@@ -225,15 +239,16 @@ exports.IoMediator = Montage.create(Component, {
225 for (var h = 0; toremovetags[h]; h++) { 239 for (var h = 0; toremovetags[h]; h++) {
226 try { 240 try {
227 //Checking head first 241 //Checking head first
228 template.document.content.document.head.removeChild(toremovetags[h]); 242 template.file.content.document.head.removeChild(toremovetags[h]);
229 } catch (e) { 243 } catch (e) {
230 try { 244
245 }
246 try {
231 //Checking body if not in head 247 //Checking body if not in head
232 template.document.content.document.body.removeChild(toremovetags[h]); 248 template.file.content.document.body.removeChild(toremovetags[h]);
233 } catch (e) { 249 } catch (e) {
234 //Error, not found! 250 //Error, not found!
235 } 251 }
236 }
237 } 252 }
238 //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) 253 //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all)
239 for (var l in linktags) { 254 for (var l in linktags) {
@@ -251,13 +266,13 @@ exports.IoMediator = Montage.create(Component, {
251 if (template.styles) { 266 if (template.styles) {
252 //Getting all style tags 267 //Getting all style tags
253 var styleCounter = 0, 268 var styleCounter = 0,
254 docStyles = template.document.content.document.getElementsByTagName('style'); 269 docStyles = template.file.content.document.getElementsByTagName('style');
255 //Looping through all style tags 270 //Looping through all style tags
256 for (var i in template.styles) { 271 for (var i in template.styles) {
257 if (template.styles[i].ownerNode) { 272 if (template.styles[i].ownerNode) {
258 if (template.styles[i].ownerNode.getAttribute) { 273 if (template.styles[i].ownerNode.getAttribute) {
259 //Checking for node not to be loaded from file 274 //Checking for node not to be loaded from file
260 if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.styles[i].ownerNode.getAttribute('data-ninja-template')) { 275 if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === null && !template.styles[i].ownerNode.getAttribute('data-ninja-template') && !template.styles[i].ownerNode.getAttribute('data-ninja-external-url')) {
261 if (docStyles[styleCounter]) { 276 if (docStyles[styleCounter]) {
262 //Inseting data from rules array into tag as string 277 //Inseting data from rules array into tag as string
263 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules); 278 docStyles[styleCounter].innerHTML = this.getCssFromRules(template.styles[i].cssRules);
@@ -271,8 +286,8 @@ exports.IoMediator = Montage.create(Component, {
271 } else if (template.css) { 286 } else if (template.css) {
272 //Getting all style and link tags 287 //Getting all style and link tags
273 var styleCounter = 0, 288 var styleCounter = 0,
274 docStyles = template.document.content.document.getElementsByTagName('style'), 289 docStyles = template.file.content.document.getElementsByTagName('style'),
275 docLinks = template.document.content.document.getElementsByTagName('link'); 290 docLinks = template.file.content.document.getElementsByTagName('link');
276 //Removing Ninja Data Attributes 291 //Removing Ninja Data Attributes
277 for (var n in docLinks) { 292 for (var n in docLinks) {
278 if (docLinks[n].attributes) { 293 if (docLinks[n].attributes) {
@@ -352,13 +367,13 @@ exports.IoMediator = Montage.create(Component, {
352 if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') { 367 if (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name === 'RDGE') {
353 rdgeDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name + this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase(); 368 rdgeDirName = (this.application.ninja.coreIoApi.ninjaLibrary.libs[i].name + this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version).toLowerCase();
354 rdgeVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version; 369 rdgeVersion = this.application.ninja.coreIoApi.ninjaLibrary.libs[i].version;
355 this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.document.root, rdgeDirName); 370 this.application.ninja.coreIoApi.ninjaLibrary.copyLibToCloud(template.file.root, rdgeDirName);
356 } else { 371 } else {
357 //TODO: Error handle no available library to copy 372 //TODO: Error handle no available library to copy
358 } 373 }
359 } 374 }
360 // 375 //
361 var json, matchingtags = [], webgltag, scripts = template.document.content.document.getElementsByTagName('script'), webgljstag, webgllibtag, webglrdgetag, mjstag, mjslibtag; 376 var json, matchingtags = [], webgltag, scripts = template.file.content.document.getElementsByTagName('script'), webgljstag, webgllibtag, webglrdgetag, mjstag, mjslibtag;
362 // 377 //
363 for (var i in scripts) { 378 for (var i in scripts) {
364 if (scripts[i].getAttribute) { 379 if (scripts[i].getAttribute) {
@@ -393,32 +408,32 @@ exports.IoMediator = Montage.create(Component, {
393 } 408 }
394 // 409 //
395 if (!webglrdgetag) { 410 if (!webglrdgetag) {
396 webglrdgetag = template.document.content.document.createElement('script'); 411 webglrdgetag = template.file.content.document.createElement('script');
397 webglrdgetag.setAttribute('type', 'text/javascript'); 412 webglrdgetag.setAttribute('type', 'text/javascript');
398 webglrdgetag.setAttribute('src', rdgeDirName + '/rdge-compiled.js'); 413 webglrdgetag.setAttribute('src', rdgeDirName + '/rdge-compiled.js');
399 webglrdgetag.setAttribute('data-ninja-webgl-rdge', 'true'); 414 webglrdgetag.setAttribute('data-ninja-webgl-rdge', 'true');
400 template.document.content.document.head.appendChild(webglrdgetag); 415 template.file.content.document.head.appendChild(webglrdgetag);
401 } 416 }
402 // 417 //
403 if (!webgllibtag) { 418 if (!webgllibtag) {
404 webgllibtag = template.document.content.document.createElement('script'); 419 webgllibtag = template.file.content.document.createElement('script');
405 webgllibtag.setAttribute('type', 'text/javascript'); 420 webgllibtag.setAttribute('type', 'text/javascript');
406 webgllibtag.setAttribute('src', rdgeDirName + '/canvas-runtime.js'); 421 webgllibtag.setAttribute('src', rdgeDirName + '/canvas-runtime.js');
407 webgllibtag.setAttribute('data-ninja-webgl-lib', 'true'); 422 webgllibtag.setAttribute('data-ninja-webgl-lib', 'true');
408 template.document.content.document.head.appendChild(webgllibtag); 423 template.file.content.document.head.appendChild(webgllibtag);
409 } 424 }
410 // 425 //
411 if (!webgltag) { 426 if (!webgltag) {
412 webgltag = template.document.content.document.createElement('script'); 427 webgltag = template.file.content.document.createElement('script');
413 webgltag.setAttribute('data-ninja-webgl', 'true'); 428 webgltag.setAttribute('data-ninja-webgl', 'true');
414 template.document.content.document.head.appendChild(webgltag); 429 template.file.content.document.head.appendChild(webgltag);
415 } 430 }
416 //TODO: Remove this tag and place inside JS file 431 //TODO: Remove this tag and place inside JS file
417 if (!webgljstag) { 432 if (!webgljstag) {
418 webgljstag = template.document.content.document.createElement('script'); 433 webgljstag = template.file.content.document.createElement('script');
419 webgljstag.setAttribute('type', 'text/javascript'); 434 webgljstag.setAttribute('type', 'text/javascript');
420 webgljstag.setAttribute('data-ninja-webgl-js', 'true'); 435 webgljstag.setAttribute('data-ninja-webgl-js', 'true');
421 template.document.content.document.head.appendChild(webgljstag); 436 template.file.content.document.head.appendChild(webgljstag);
422 } 437 }
423 //TODO: Decide if this should be over-writter or only written on creation 438 //TODO: Decide if this should be over-writter or only written on creation
424 var rootElement = 'document.body'; //TODO: Set actual root element