aboutsummaryrefslogtreecommitdiff
path: root/js/io/document/html-document.js
blob: c44dfe75ef9cfac115e5acf1c3b0a2965e843f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Montage = require("montage/core/core").Montage,
    baseDocumentModule = require("js/io/document/base-document"),
    NJUtils = require("js/lib/NJUtils").NJUtils;

var HTMLDocument = exports.HTMLDocument = Montage.create(baseDocumentModule.BaseDocument, {
    // PRIVATE MEMBERS
    _selectionExclude: { value: null, enumerable: false },
    _cloudTemplateUri: { value: "user-document-templates/montage-application-cloud/index.html", enumerable: false},
    _iframe: { value: null, enumerable: false },
    _server: { value: null, enumerable: false },
    _selectionModel: { value: [], enumerable: false },
    _undoModel: { value: { "queue" : [], "position" : 0 }, enumerable: false},

    _document: { value: null, enumerable: false },
    _documentRoot: { value: null, enumerable: false },
    _stageBG: { value: null, enumerable: false },
    _window: { value: null, enumerable: false },
    _styles: { value: null, enumerable: false },
    _stylesheets: { value: null, enumerable: false },
    _stageStyleSheetId : { value: 'nj-stage-stylesheet', enumerable: false },
    _initialUserDocument: { value: null, enumerable: false },
    _htmlSource: {value: "<html></html>", enumerable: false},
    _glData: {value: null, enumerable: false },

    _elementCounter: { value: 1, enumerable: false },
    _snapping : { value: true, enumerable: false },
    _layoutMode: { value: "all", enumerable: false },
    _draw3DGrid: { value: false, writable: true },
    _swfObject: { value: false, enumerable: false },

    _zoomFactor: { value: 100, enumerable: false },

    // PUBLIC MEMBERS
    cssLoadInterval: { value: null, enumerable: false },

    /*
     * PUBLIC API
     */

    // GETTERS / SETTERS
    selectionExclude: {
        get: function() { return this._selectionExclude; },
        set: function(value) { this._selectionExclude = value; }
    },

    iframe: {
        get: function() { return this._iframe; },
        set: function(value) { this._iframe = value; }
    },

    server: {
        get: function() { return this._server; },
        set: function(value) { this._server = value; }
    },

    selectionModel: {
        get: function() { return this._selectionModel; },
        set: function(value) { this._selectionModel = value; }
    },

    undoModel: {
        get: function() { return this._undoModel; },
        set: function(value) { this._undoModel.queue = value.queue; this._undoModel.position = value.position; }
    },

    documentRoot: {
        get: function() { return this._documentRoot; },
        set: function(value) { this._documentRoot = value; }
    },

    stageBG: {
        get: function() { return this._stageBG; },
        set: function(value) { this._stageBG = value; }
    },

    elementCounter: {
        set: function(value) { this._elementCounter = value; },
        get: function() { return this._elementCounter; }
    },

    snapping: {
        get: function() { return this._snapping; },
        set: function(value) {
            if(this._snapping !== value) {
                this._snapping = value;
            }
        }
    },

    // TODO SEND THE EVENT --> Redraw the desired layout
    layoutMode: {
        get: function() { return this._layoutMode; },
        set: function(mode) { this._layoutMode = mode; }
    },

    draw3DGrid: {
        get: function() { return this._draw3DGrid; },
        set: function(value) {
            if(this._draw3DGrid !== value) {
                this._draw3DGrid = value;
            }
        }
    },

    _userComponentSet: {
        value: {},
        writable: true,
        enumerable:true
    },

//    userComponentSet:{
//        enumerable: true,
//        get: function() {
//            return this._userComponentSet;
//        },
//        set: function(value) {
//            this._userComponentSet = value;
//            this._drawUserComponentsOnOpen();
//        }
//    },
//
//    _drawUserComponentsOnOpen:{
//        value:function(){
//            for(var i in this._userComponentSet){
//                console.log(this._userComponentSet[i].control)
//                this._userComponentSet[i].control.needsDraw = true;
//            }
//        }
//    },
    
    glData: {
        get: function()
		{
			var elt = this.iframe;
			var elt = this.iframe.contentWindow.document.getElementById("UserContent");
			this._glData = null;
			if (elt)
			{
				this._glData = new Array();
				this.collectGLData( elt,  this._glData );
			}
				
			return this._glData
		},

        set: function(value)
		{
			var nWorlds = value.length;
			for (var i=0;  i<nWorlds;  i++)
			{
				var importStr = value[i];
				var startIndex = importStr.indexOf( "id: " );
				if (startIndex >= 0)
				{
					var endIndex = importStr.indexOf( "\n", startIndex );
					if (endIndex > 0)
					{
						var id = importStr.substring( startIndex+4, endIndex );
						var canvas = this.iframe.contentWindow.document.getElementById( id );
						if (canvas)
						{
							if (!canvas.elementModel)
							{
								NJUtils.makeElementModel(canvas, "Canvas", "shape", true);
							}
								
							if (canvas.elementModel)
							{
								if (canvas.elementModel.shapeModel.GLWorld)
                                    canvas.elementModel.shapeModel.GLWorld.clearTree();

								var world = new GLWorld( canvas );
								canvas.elementModel.shapeModel.GLWorld = world;
								world.import( importStr );
							}
						}
					}
				}
			}
		}
    },

    zoomFactor: {
        get: function() { return this._zoomFactor; },
        set: function(value) { this._zoomFactor = value; }
    },

    //****************************************//
    // PUBLIC METHODS
    initialize: {
        value: function(doc, uuid, iframe, callback) {
            // Shell mode is not used anymore
            //if(!window.IsInShellMode()) {

                this.init("index-cloud", this._cloudTemplateUri, doc.type, iframe, uuid, callback);
            /*
            } else {
                var tmpurl = doc.uri.split('\\');
                var fileUrl =  doc.server.url + "/" + tmpurl[tmpurl.length -1] + "?fileio=true&template=/user-document-templates/montage-application/index.html";
                this.init(name, fileUrl, doc.type, iframe, uuid, callback);
                this.server = doc.server;
                this._initialUserDocument = doc;
            }
            */
            this.iframe = iframe;
            this.selectionExclude = ["HTML", "BODY", "Viewport", "UserContent", "stageBG"];
            this.currentView = "design";

            this._loadDocument(this.uri);
        }
    },

	collectGLData: {
		value: function( elt,  dataArray )
		{
			if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
			{
				var data = elt.elementModel.shapeModel.GLWorld.export();
				dataArray.push( data );
			}

			if (elt.children)
			{
				var nKids = elt.children.length;
				for (var i=0;  i<nKids;  i++)
				{
					var child = elt.children[i];
					this.collectGLData( child, dataArray );
				}
			}
		}
	},


    // OLD

    inExclusion: {
        value: function(element) {
            if(this._selectionExclude.indexOf(element.id) === -1) {
                if(this._selectionExclude.indexOf(element.nodeName) === -1) {
                    return -1;
                }
            } else if (this._selectionExclude.indexOf(element.id) === -1) {
                return -1;
            } else {
                return 1;
            }
        }
    },



    AppendElement: {
        value: function(element, parent) {
            this.dirtyFlag = true;
        }
    },


    /**
     * Return the specified inline attribute from the element.
     */
    GetElementAttribute: {
        value: function(element, attribute) {

            var value;

            if(attribute === "src") {
                return element[attribute].replace(window.location.href, '');
            }

            value = element[attribute];

            if(value !== undefined) return value;
//            if(value || value === false) return [value, "inline"];

            // 3.
            //value = this._document.defaultView.getComputedStyle(element,null).getPropertyValue(attribute);
            //if(value) return value;

            return null;
        }
    },

    GetElementStyle: {
        value: function(element, style) {
//            return this._queryStylesheets(element, style);
        }
    },

    SetStyle: {
        value: function(type, selector, style, value) {
            try {
                for(var j=0; j<this._stylesheets.length;j++){
                    for(var i=0; i<this._stylesheets[j].cssRules.length;i++) {
                        if(this._stylesheets[j].cssRules[i].selectorText === type + selector) {
                            this._stylesheets[j].cssRules[i].style[style] = value;

                            return true;
                        }
                    }
                }
            } catch(err) {
                console.log("Cannot change the style of selector: " + selector + " " + err);
            }
        }
    },

    GetElementFromPoint: {
        value: function(x, y) {
            return this._window.getElement(x,y);
        }
    },

    // Private
    _loadDocument: {
        value: function(uri) {
            // Load the document into the Iframe
            this.iframe.src = uri;
            this.iframe.addEventListener("load", this, true);
        }
    },

    handleEvent: {
        value: function(event){
            this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent");
            this.stageBG = this.iframe.contentWindow.document.getElementById("stageBG");
            this.stageBG.onclick = null;
            this._document = this.iframe.contentWindow.document;
            this._window = this.iframe.contentWindow;
            if(!this.documentRoot.Ninja)
            {
                this.documentRoot.Ninja = {};
            }

            if(this._initialUserDocument) {
                // Now load the user content
                this.documentRoot.innerHTML = this._initialUserDocument.body;
                this.iframe.contentWindow.document.getElementById("userHead").innerHTML = this._initialUserDocument.head;

                this.cssLoadInterval = setInterval(function() {
                    if(this._document.styleSheets.length > 1) {
                        clearInterval(this.cssLoadInterval);
                        this._styles = this._document.styleSheets[this._document.styleSheets.length - 1];
                        this._stylesheets = this._document.styleSheets; // Entire stlyesheets array

                        this.callback(this);
                    }
                }.bind(this), 50);
                
                // TODO - Not sure where this goes
                this._userComponentSet = {};
            } else {
                this._styles = this._document.styleSheets[this._document.styleSheets.length - 1];
                this._stylesheets = this._document.styleSheets; // Entire stlyesheets array

                /* TODO Finish this implementation once we start caching Core Elements */
                // Assign a model to the UserContent and add the ViewPort reference to it.
                NJUtils.makeElementModel(this.documentRoot, "Stage", "stage");
                //this.documentRoot.elementModel.viewPort = this.iframe.contentWindow.document.getElementById("Viewport");
                NJUtils.makeElementModel(this.stageBG, "Stage", "stage");
                NJUtils.makeElementModel(this.iframe.contentWindow.document.getElementById("Viewport"), "Stage", "stage");

                for(i = 0; i < this._stylesheets.length; i++) {
                    if(this._stylesheets[i].ownerNode.id === this._stageStyleSheetId) {
                        this.documentRoot.elementModel.defaultRule = this._stylesheets[i];
                        break;
                    }
                }

                // Temporary create properties for each rule we need to save the index of the rule.
                var len = this.documentRoot.elementModel.defaultRule.cssRules.length;
                for(var j = 0; j < len; j++) {
//                    console.log(this.documentRoot.elementModel.defaultRule.cssRules[j].selectorText);
                    if(this.documentRoot.elementModel.defaultRule.cssRules[j].selectorText === "*") {

                        this.documentRoot.elementModel.transitionStopRule = this.documentRoot.elementModel.defaultRule.cssRules[j];

                    } else if(this.documentRoot.elementModel.defaultRule.cssRules[j].selectorText === "body") {

                        this.documentRoot.elementModel.body = this.documentRoot.elementModel.defaultRule.cssRules[j];

                    } else if(this.documentRoot.elementModel.defaultRule.cssRules[j].selectorText === "#Viewport") {

                        this.documentRoot.elementModel.viewPort = this.documentRoot.elementModel.defaultRule.cssRules[j];

                    } else if(this.documentRoot.elementModel.defaultRule.cssRules[j].selectorText === ".stageDimension") {

                        this.documentRoot.elementModel.stageDimension = this.documentRoot.elementModel.defaultRule.cssRules[j];

                    } else if(this.documentRoot.elementModel.defaultRule.cssRules[j].selectorText === ".stageView") {

                        this.documentRoot.elementModel.stageView = this.documentRoot.elementModel.defaultRule.cssRules[j];

                    }
                }

                this.callback(this);
            }
        }
    },

    _setSWFObjectScript: {
        value: function() {
            if(!this._swfObject) {
                /*
                var swfObj = document.createElement("script");
                swfObj.type = "text/javascript";
                swfObj.src = "../../user-document-templates/external-libs/swf-object/swfobject.js";
                swfObj.id = "swfObject";
                var head= this._document.getElementsByTagName('head')[0];
                head.appendChild(swfObj);
                this._swfObject = true;
                */
            }
        }
    }
});