UNPKG

@qooxdoo/framework

Version:

The JS Framework for Coders

318 lines (262 loc) 8.53 kB
/* ************************************************************************ qooxdoo - the new era of web development http://qooxdoo.org Copyright: 2004-2011 1&1 Internet AG, Germany, http://www.1und1.de License: MIT: https://opensource.org/licenses/MIT See the LICENSE file in the project's top-level directory for details. Authors: * Christian Hagendorn (chris_schmidt) * Martin Wittemann (martinwittemann) ************************************************************************ */ /** * The mixin controls the binding between model and item. * * @internal */ qx.Mixin.define("qx.ui.tree.core.MWidgetController", { construct : function() { this.__boundItems = []; }, properties : { /** * The name of the property, where the value for the tree node/leaf label * is stored in the model classes. */ labelPath : { check: "String", nullable: true }, /** * The path to the property which holds the information that should be * shown as an icon. */ iconPath : { check: "String", nullable: true }, /** * A map containing the options for the label binding. The possible keys * can be found in the {@link qx.data.SingleValueBinding} documentation. */ labelOptions : { nullable: true }, /** * A map containing the options for the icon binding. The possible keys * can be found in the {@link qx.data.SingleValueBinding} documentation. */ iconOptions : { nullable: true }, /** * The name of the property, where the children are stored in the model. * Instead of the {@link #labelPath} must the child property a direct * property form the model instance. */ childProperty : { check: "String", nullable: true }, /** * Delegation object, which can have one or more functions defined by the * {@link qx.ui.tree.core.IVirtualTreeDelegate} interface. */ delegate : { event: "changeDelegate", init: null, nullable: true } }, members : { /** @type {Array} which contains the bounded items */ __boundItems : null, /** * Helper-Method for binding the default properties from the model to the * target widget. The used default properties depends on the passed item. * * This method should only be called in the {@link IVirtualTreeDelegate#bindItem} * function implemented by the {@link #delegate} property. * * @param item {qx.ui.core.Widget} The internally created and used node or * leaf. * @param index {Integer} The index of the item (node or leaf). */ bindDefaultProperties : function(item, index) { // bind model first this.bindProperty("", "model", null, item, index); this.bindProperty( this.getLabelPath(), "label", this.getLabelOptions(), item, index ); var bindPath = this.__getBindPath(index); var bindTarget = this._tree.getLookupTable(); bindTarget = qx.data.SingleValueBinding.resolvePropertyChain(bindTarget, bindPath); if (qx.util.OOUtil.hasProperty(bindTarget.constructor, this.getChildProperty())) { this.bindProperty( this.getChildProperty() + ".length", "appearance", { converter : function() { return "virtual-tree-folder"; } }, item, index ); } else { item.setAppearance("virtual-tree-file"); } if (this.getIconPath() != null) { this.bindProperty( this.getIconPath(), "icon", this.getIconOptions(), item, index ); } }, /** * Helper-Method for binding a given property from the model to the target * widget. * * This method should only be called in the {@link IVirtualTreeDelegate#bindItem} * function implemented by the {@link #delegate} property. * * @param sourcePath {String | null} The path to the property in the model. * If you use an empty string, the whole model item will be bound. * @param targetProperty {String} The name of the property in the target widget. * @param options {Map | null} The options to use for the binding. * @param targetWidget {qx.ui.core.Widget} The target widget. * @param index {Integer} The index of the current binding. */ bindProperty : function(sourcePath, targetProperty, options, targetWidget, index) { var bindPath = this.__getBindPath(index, sourcePath); var bindTarget = this._tree.getLookupTable(); var id = bindTarget.bind(bindPath, targetWidget, targetProperty, options); this.__addBinding(targetWidget, id); }, /** * Helper-Method for binding a given property from the target widget to * the model. * This method should only be called in the * {@link qx.ui.tree.core.IVirtualTreeDelegate#bindItem} function implemented by the * {@link #delegate} property. * * @param targetPath {String | null} The path to the property in the model. * @param sourceProperty {String} The name of the property in the target. * @param options {Map | null} The options to use for the binding. * @param sourceWidget {qx.ui.core.Widget} The source widget. * @param index {Integer} The index of the current binding. */ bindPropertyReverse : function(targetPath, sourceProperty, options, sourceWidget, index) { var bindPath = this.__getBindPath(index, targetPath); var bindTarget = this._tree.getLookupTable(); var id = sourceWidget.bind(sourceProperty, bindTarget, bindPath, options); this.__addBinding(sourceWidget, id); }, /** * Remove all bindings from all bounded items. */ removeBindings : function() { while(this.__boundItems.length > 0) { var item = this.__boundItems.pop(); this._removeBindingsFrom(item); } }, /** * Sets up the binding for the given item and index. * * @param item {qx.ui.core.Widget} The internally created and used item. * @param index {Integer} The index of the item. */ _bindItem : function(item, index) { var bindItem = qx.util.Delegate.getMethod(this.getDelegate(), "bindItem"); if (bindItem != null) { bindItem(this, item, index); } else { this.bindDefaultProperties(item, index); } }, /** * Removes the binding of the given item. * * @param item {qx.ui.core.Widget} The item which the binding should be * removed. */ _removeBindingsFrom : function(item) { var bindings = this.__getBindings(item); while (bindings.length > 0) { var id = bindings.pop(); try { this._tree.getLookupTable().removeBinding(id); } catch(e) { item.removeBinding(id); } } if (this.__boundItems.includes(item)) { qx.lang.Array.remove(this.__boundItems, item); } }, /** * Helper method to create the path for binding. * * @param index {Integer} The index of the item. * @param path {String|null} The path to the property. * @return {String} The binding path */ __getBindPath : function(index, path) { var bindPath = "[" + index + "]"; if (path != null && path != "") { bindPath += "." + path; } return bindPath; }, /** * Helper method to save the binding for the widget. * * @param widget {qx.ui.core.Widget} widget to save binding. * @param id {var} the id from the binding. */ __addBinding : function(widget, id) { var bindings = this.__getBindings(widget); if (!bindings.includes(id)) { bindings.push(id); } if (!this.__boundItems.includes(widget)) { this.__boundItems.push(widget); } }, /** * Helper method which returns all bound id from the widget. * * @param widget {qx.ui.core.Widget} widget to get all binding. * @return {Array} all bound id's. */ __getBindings : function(widget) { var bindings = widget.getUserData("BindingIds"); if (bindings == null) { bindings = []; widget.setUserData("BindingIds", bindings); } return bindings; } }, destruct : function() { this.__boundItems = null; } });