diff options
author | Armen Kesablyan | 2012-05-15 16:34:46 -0700 |
---|---|---|
committer | Armen Kesablyan | 2012-05-15 16:34:46 -0700 |
commit | c8d61c8e72e0eba266575f9df54325fa77fde73d (patch) | |
tree | 556cafd76ab9b2cf4cc2b4cc3ea17b12ce690b69 /js/mediators/io-mediator.js | |
parent | 15a3aaebb56cb2c9409bfe55c862868726c7fd44 (diff) | |
parent | 46bd3712329cd3c9311e50ed9ee4c2245bd1be5a (diff) | |
download | ninja-c8d61c8e72e0eba266575f9df54325fa77fde73d.tar.gz |
Merge branch 'dom-architecture' of github.com:Motorola-Mobility/ninja-internal into binding
Diffstat (limited to 'js/mediators/io-mediator.js')
-rw-r--r-- | js/mediators/io-mediator.js | 182 |
1 files changed, 135 insertions, 47 deletions
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index c20ae8f7..8346c75e 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js | |||
@@ -10,7 +10,7 @@ var Montage = require("montage/core/core").Montage, | |||
10 | Component = require("montage/ui/component").Component, | 10 | Component = require("montage/ui/component").Component, |
11 | FileIo = require("js/io/system/fileio").FileIo, | 11 | FileIo = require("js/io/system/fileio").FileIo, |
12 | ProjectIo = require("js/io/system/projectio").ProjectIo, | 12 | ProjectIo = require("js/io/system/projectio").ProjectIo, |
13 | TemplateCreator = require("node_modules/tools/template-creator").TemplateCreator; | 13 | TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; |
14 | //////////////////////////////////////////////////////////////////////// | 14 | //////////////////////////////////////////////////////////////////////// |
15 | // | 15 | // |
16 | exports.IoMediator = Montage.create(Component, { | 16 | exports.IoMediator = Montage.create(Component, { |
@@ -53,14 +53,14 @@ exports.IoMediator = Montage.create(Component, { | |||
53 | // | 53 | // |
54 | fileNew: { | 54 | fileNew: { |
55 | enumerable: false, | 55 | enumerable: false, |
56 | value: function (file, template, callback) { | 56 | value: function (file, url, callback, template) { |
57 | //Loading template from template URL | 57 | //Loading template from template URL |
58 | var xhr = new XMLHttpRequest(), result; | 58 | var xhr = new XMLHttpRequest(), result; |
59 | xhr.open("GET", template, false); | 59 | xhr.open("GET", url, false); |
60 | xhr.send(); | 60 | xhr.send(); |
61 | if (xhr.readyState === 4) { | 61 | if (xhr.readyState === 4) { |
62 | //Making call to create file, checking for return code | 62 | //Making call to create file, checking for return code |
63 | switch (this.fio.newFile({ uri: file, contents: xhr.response })) { | 63 | switch (this.fio.newFile({ uri: file, contents: parseTemplate(xhr.response, template) })) { |
64 | case 201: | 64 | case 201: |
65 | result = { status: 201, success: true, uri: file }; | 65 | result = { status: 201, success: true, uri: file }; |
66 | break; | 66 | break; |
@@ -74,6 +74,21 @@ exports.IoMediator = Montage.create(Component, { | |||
74 | result = { status: 500, success: false, uri: file }; | 74 | result = { status: 500, success: false, uri: file }; |
75 | break; | 75 | break; |
76 | } | 76 | } |
77 | //TODO: Improve template data injection | ||
78 | function parseTemplate (content, template) { | ||
79 | // | ||
80 | if (template.name.toLowerCase() === 'banner' || template.name.toLowerCase() === 'animation') { | ||
81 | //Getting dimensions of banner | ||
82 | var dimensions = template.id.split('x'); | ||
83 | dimensions = {width: String(dimensions[0])+'px', height: String(dimensions[1])+'px'}; | ||
84 | // | ||
85 | content = content.replace(/Dimensions@@@/gi, "Dimensions@@@"+template.id); | ||
86 | content = content.replace(/ninja-banner {}/gi, "ninja-banner {overflow: visible; width: "+dimensions.width+"; height: "+dimensions.height+"}"); | ||
87 | content = content.replace(/ninja-content-wrapper {}/gi, "ninja-content-wrapper {overflow: hidden; width: "+dimensions.width+"; height: "+dimensions.height+"}"); | ||
88 | } | ||
89 | // | ||
90 | return content; | ||
91 | } | ||
77 | } else { | 92 | } else { |
78 | result = { status: 500, success: false, uri: file }; | 93 | result = { status: 500, success: false, uri: file }; |
79 | } | 94 | } |
@@ -147,21 +162,25 @@ exports.IoMediator = Montage.create(Component, { | |||
147 | // | 162 | // |
148 | fileSave: { | 163 | fileSave: { |
149 | enumerable: false, | 164 | enumerable: false, |
150 | value: function (file, callback) { | 165 | value: function (doc, callback) { |
151 | // | 166 | // |
152 | var contents, save; | 167 | var contents, save; |
153 | // | 168 | // |
154 | switch (file.mode) { | 169 | switch (doc.mode) { |
155 | case 'html': | 170 | case 'html': |
156 | //Getting content from function to properly handle saving assets (as in external if flagged) | 171 | //Getting content from function to properly handle saving assets (as in external if flagged) |
157 | contents = this.parseNinjaTemplateToHtml(file); | 172 | if (doc.template && (doc.template.type === 'banner' || doc.template.type === 'animation')) { |
173 | contents = this.parseNinjaTemplateToHtml(doc, true); | ||
174 | } else { | ||
175 | contents = this.parseNinjaTemplateToHtml(doc); | ||
176 | } | ||
158 | break; | 177 | break; |
159 | default: | 178 | default: |
160 | contents = file.content; | 179 | contents = doc.content; |
161 | break; | 180 | break; |
162 | } | 181 | } |
163 | //Making call to save file | 182 | //Making call to save file |
164 | save = this.fio.saveFile({ uri: file.document.uri, contents: contents }); | 183 | save = this.fio.saveFile({ uri: doc.file.uri, contents: contents }); |
165 | //Checking for callback | 184 | //Checking for callback |
166 | if (callback) callback(save); | 185 | if (callback) callback(save); |
167 | } | 186 | } |
@@ -192,23 +211,62 @@ exports.IoMediator = Montage.create(Component, { | |||
192 | //Setting content to temp | 211 | //Setting content to temp |
193 | doc.getElementsByTagName('html')[0].innerHTML = html; | 212 | doc.getElementsByTagName('html')[0].innerHTML = html; |
194 | //Creating return object | 213 | //Creating return object |
195 | return { head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc }; | 214 | return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc}; |
196 | } | 215 | } |
197 | }, | 216 | }, |
198 | //////////////////////////////////////////////////////////////////// | 217 | //////////////////////////////////////////////////////////////////// |
199 | //TODO: Expand to allow more templates, clean up variables | 218 | //TODO: Expand to allow more templates, clean up variables |
200 | parseNinjaTemplateToHtml: { | 219 | parseNinjaTemplateToHtml: { |
201 | enumerable: false, | 220 | enumerable: false, |
202 | value: function (template) { | 221 | value: function (template, ninjaWrapper) { |
203 | var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])); | 222 | 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'); | 223 | regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); |
205 | //Injecting head and body into old document | 224 | //Injecting head and body into old document |
206 | template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, ''); | 225 | template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); |
207 | template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, ''); | 226 | template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); |
227 | //Copying attributes to maintain same properties as the <body> | ||
228 | for (var n in template.body.attributes) { | ||
229 | if (template.body.attributes[n].value) { | ||
230 | // | ||
231 | template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value); | ||
232 | } | ||
233 | } | ||
234 | //TODO: Add attribute copying for <HEAD> and <HTML> | ||
235 | |||
236 | /* | ||
237 | //Testing using montage clean up method | ||
238 | var mjscode, mjsTemp = TemplateCreator.create(); | ||
239 | |||
240 | //mjscode = mjsTemp.initWithHeadAndBodyElements(template.head, template.body); | ||
241 | //mjscode = mjsTemp.initWithDocument(template.file.content.document); | ||
242 | mjscode = mjsTemp.initWithDocument(template.document); | ||
243 | |||
244 | console.log(template.head, mjscode._document.head); | ||
245 | template.file.content.document.head.innerHTML = mjscode._document.head.innerHTML.replace(regexRootUrl, ''); | ||
246 | template.file.content.document.body.innerHTML = mjscode._document.body.innerHTML.replace(regexRootUrl, ''); | ||
247 | */ | ||
248 | |||
249 | |||
250 | |||
251 | |||
208 | //Getting all CSS (style or link) tags | 252 | //Getting all CSS (style or link) tags |
209 | var styletags = template.document.content.document.getElementsByTagName('style'), | 253 | var styletags = template.file.content.document.getElementsByTagName('style'), |
210 | linktags = template.document.content.document.getElementsByTagName('link'), | 254 | linktags = template.file.content.document.getElementsByTagName('link'), |
211 | toremovetags = []; | 255 | toremovetags = [], |
256 | njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'); | ||
257 | |||
258 | ////////////////////////////////////////////////// | ||
259 | //TODO: Remove, temp hack, this is to be fixed by Montage | ||
260 | var basetags = template.file.content.document.getElementsByTagName('base'); | ||
261 | for (var g in basetags) { | ||
262 | if (basetags[g].getAttribute) toremovetags.push(basetags[g]); | ||
263 | } | ||
264 | ////////////////////////////////////////////////// | ||
265 | |||
266 | // | ||
267 | for (var f in njtemplatetags) { | ||
268 | if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]); | ||
269 | } | ||
212 | //Getting styles tags to be removed from document | 270 | //Getting styles tags to be removed from document |
213 | if (styletags.length) { | 271 | if (styletags.length) { |
214 | for (var j = 0; j < styletags.length; j++) { | 272 | for (var j = 0; j < styletags.length; j++) { |
@@ -225,15 +283,16 @@ exports.IoMediator = Montage.create(Component, { | |||
225 | for (var h = 0; toremovetags[h]; h++) { | 283 | for (var h = 0; toremovetags[h]; h++) { |
226 | try { | 284 | try { |
227 | //Checking head first | 285 | //Checking head first |
228 | template.document.content.document.head.removeChild(toremovetags[h]); | 286 | template.file.content.document.head.removeChild(toremovetags[h]); |
229 | } catch (e) { | 287 | } catch (e) { |
230 | try { | 288 | |
289 | } | ||
290 | try { | ||
231 | //Checking body if not in head | 291 | //Checking body if not in head |
232 | template.document.content.document.body.removeChild(toremovetags[h]); | 292 | template.file.content.document.body.removeChild(toremovetags[h]); |
233 | } catch (e) { | 293 | } catch (e) { |
234 | //Error, not found! | 294 | //Error, not found! |
235 | } | 295 | } |
236 | } | ||
237 | } | 296 | } |
238 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) | 297 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) |
239 | for (var l in linktags) { | 298 | for (var l in linktags) { |
@@ -251,13 +310,13 @@ exports.IoMediator = Montage.create(Component, { | |||
251 | if (template.styles) { | 310 | if (template.styles) { |
252 | //Getting all style tags | 311 | //Getting all style tags |
253 | var styleCounter = 0, | 312 | var styleCounter = 0, |
254 | docStyles = template.document.content.document.getElementsByTagName('style'); | 313 | docStyles = template.file.content.document.getElementsByTagName('style'); |
255 | //Looping through all style tags | 314 | //Looping through all style tags |
256 | for (var i in template.styles) { | 315 | for (var i in template.styles) { |
257 | if (template.styles[i].ownerNode) { | 316 | if (template.styles[i].ownerNode) { |
258 | if (template.styles[i].ownerNode.getAttribute) { | 317 | if (template.styles[i].ownerNode.getAttribute) { |
259 | //Checking for node not to be loaded from file | 318 | //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')) { | 319 | if (template.styles[i].ownerNode.getAttribute('data-ninja-uri') === nu |