From 563935933c191093b8ccd88fd1377775c5641b0d Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Mon, 4 Jun 2012 23:33:32 -0700 Subject: components fix: adding a first draw event to components Adding a first draw allows the component to have the right element so that ninja can use it's controller to get the component instance Signed-off-by: Valerio Virgillito --- js/controllers/elements/component-controller.js | 4 +- js/document/templates/app/main.js | 52 ++++++++++++++++---- .../components-panel.reel/components-panel.js | 56 ++++++++++++++-------- 3 files changed, 80 insertions(+), 32 deletions(-) (limited to 'js') diff --git a/js/controllers/elements/component-controller.js b/js/controllers/elements/component-controller.js index 5b0aaeac..dd0766df 100755 --- a/js/controllers/elements/component-controller.js +++ b/js/controllers/elements/component-controller.js @@ -11,7 +11,7 @@ exports.ComponentController = Montage.create(ElementController, { getProperty: { value: function(el, prop) { - var component = el.controller || this.application.ninja.currentDocument.model.getComponentFromElement(el); + var component = el.controller; switch(prop) { case "id": @@ -34,7 +34,7 @@ exports.ComponentController = Montage.create(ElementController, { setProperty: { value: function(el, p, value) { - var component = el.controller || this.application.ninja.currentDocument.model.getComponentFromElement(el); + var component = el.controller; switch(p) { case "id": diff --git a/js/document/templates/app/main.js b/js/document/templates/app/main.js index a406abdb..fbd2e138 100644 --- a/js/document/templates/app/main.js +++ b/js/document/templates/app/main.js @@ -8,12 +8,38 @@ var Montage = require("montage/core/core").Montage, Template = require("montage/ui/template").Template, TemplateCreator = require("tools/template/template-creator").TemplateCreator; + +//var njmodelGet = function njmodelGet() { +// return (this.hasOwnProperty("_model") ? this._model: document.modelGenerator.call(this)); +//}; +// +//Object.defineProperty(Object.prototype, "_model", { +// enumerable: false, +// value: null, +// writable: true +//}); +// +//Object.defineProperty(Object.prototype, "elementModel", { +// configurable: true, +// get: njmodelGet, +// set: function() { +// } +//}); + exports.Main = Montage.create(Component, { hasTemplate: { value: false }, + componentToInsert: { + value: null + }, + + firstDrawCallback: { + value: null + }, + /** * Adding window hooks to callback into this object from Ninja. */ @@ -24,6 +50,12 @@ exports.Main = Montage.create(Component, { window.addComponent = function(element, data, callback) { var component; + if(!self.firstDrawCallback) { + self.firstDrawCallback = {}; + self.firstDrawCallback.callback = data.firstDraw.cb; + self.firstDrawCallback.context = data.firstDraw.ctx; + } + component = require.async(data.path) .then(function(component) { var componentRequire = component[data.name]; @@ -34,6 +66,9 @@ exports.Main = Montage.create(Component, { componentInstance.needsDraw = true; componentInstance.ownerComponent = self; + self.componentToInsert = componentInstance; + componentInstance.addEventListener("firstDraw", self, false); + callback(componentInstance, element); }) .end(); @@ -46,16 +81,15 @@ exports.Main = Montage.create(Component, { // var templateEvent = document.createEvent("CustomEvent"); templateEvent.initCustomEvent("mjsTemplateReady", false, true); - document.body.dispatchEvent(templateEvent); - - - // Dispatch event when this template has loaded. - /* - var newEvent = document.createEvent( "CustomEvent" ); - newEvent.initCustomEvent( "userTemplateDidLoad", false, true ); - document.body.dispatchEvent( newEvent ); - */ + } + }, + + handleFirstDraw: { + value: function() { + this.firstDrawCallback.callback.call(this.firstDrawCallback.context, this.componentToInsert); + this.componentToInsert.removeEventListener("firstDraw", this, false); + this.componentToInsert = null; } } }); \ No newline at end of file diff --git a/js/panels/components-panel.reel/components-panel.js b/js/panels/components-panel.reel/components-panel.js index 5a9d2ca5..0b453c4a 100755 --- a/js/panels/components-panel.reel/components-panel.js +++ b/js/panels/components-panel.reel/components-panel.js @@ -4,11 +4,11 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. */ -var Montage = require("montage/core/core").Montage, - Component = require("montage/ui/component").Component, - NJUtils = require("js/lib/NJUtils").NJUtils; - ClassUUID = require("js/components/core/class-uuid").ClassUuid, - PIData = require("js/data/pi/pi-data").PiData; +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component, + ElementController = require("js/controllers/elements/element-controller").ElementController, + ClassUUID = require("js/components/core/class-uuid").ClassUuid, + PIData = require("js/data/pi/pi-data").PiData; String.prototype.capitalizeFirstChar = function() { return this.charAt(0).toUpperCase() + this.slice(1); @@ -273,28 +273,42 @@ exports.ComponentsPanel = Montage.create(Component, { that = this; element = this.makeComponent(component.component); - this.application.ninja.currentDocument.model.views.design.iframe.contentWindow.addComponent(element, {name: component.name, path: component.module}, function(instance, element) { + this.application.ninja.currentDocument.model.views.design.iframe.contentWindow.addComponent(element, + {name: component.name, path: component.module, firstDraw: {cb: this.componentInstanceOnFirstDraw, ctx: this}}, + function(instance, element) { - //var pos = that.getStageCenter(); + //var pos = that.getStageCenter(); - var styles = { - 'position': 'absolute', - 'left' : that.dragPosition[0] + 'px', - 'top' : that.dragPosition[1] + 'px' - }; + var styles = { + 'position': 'absolute', + 'left' : that.dragPosition[0] + 'px', + 'top' : that.dragPosition[1] + 'px' + }; - var defaultStyles = component.defaultStyles; - if(defaultStyles) { - for(var n in defaultStyles) { - styles[n] = defaultStyles[n]; + var defaultStyles = component.defaultStyles; + if(defaultStyles) { + for(var n in defaultStyles) { + styles[n] = defaultStyles[n]; + } } - } - - that.application.ninja.currentDocument.model.setComponentInstance(instance, element); - that.application.ninja.elementMediator.addElements(element, styles); - }); +// that.application.ninja.currentDocument.model.setComponentInstance(instance, element); + //that.application.ninja.elementMediator.addElements(element, styles); + ElementController.addElement(element, styles); + }); + + } + }, + + componentInstanceOnFirstDraw: { + value: function(instance) { + // Temporary hack until the element model rework goes into place + // TODO: Remove this once we have the element model define property code in place. + if(!instance.element.elementModel) { + this.application.njUtils.makeModelFromElement(instance.element); + } + this.application.ninja.elementMediator.addElements(instance.element); } }, -- cgit v1.2.3