Ignore:
Timestamp:
03/13/14 22:21:55 (11 years ago)
Author:
hendrikvanantwerpen
Message:
  • Another shot at getting change events right for _ComplexValueMixin. Our previous approach made it impossible to detect if a change was cause during startup. Now we work purely with widget 'change' events. Children are connected during postCreate, addChild. If you add children otherwise you need to call connectChildsChanges yourself. Also to make sure events are generated, use _setValueInternal if you really need to set the value attribute yourself.
  • Removed unused widget.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/client/qed-client/widgets/ListWidget.js

    r511 r513  
    11define([
    2     "dijit/_Container",
     2    "./_ComplexValueMixin",
    33    "dijit/_WidgetBase",
    44    "dijit/registry",
    55    "dojo/_base/array",
    66    "dojo/_base/declare",
     7    "dojo/_base/event",
    78    "dojo/_base/lang",
    89    "dojo/dnd/Source",
    910    "dojo/dnd/common",
    1011    "dojo/dom-construct",
    11     "dojo/_base/event",
    1212    "dojo/on"
    13 ], function(_Container, _WidgetBase, registry, array, declare, lang, Source, dnd, domConstruct, event, on) {
    14     return declare([_WidgetBase,_Container],{
    15         name: "",
    16         value: null,
    17         disabled: false,
    18         readOnly: false,
     13], function(_ComplexValueMixin, _WidgetBase, registry, array, declare, event, lang, Source, dnd, domConstruct, on) {
     14    return declare([_WidgetBase,_ComplexValueMixin],{
    1915        multiple: true,
    2016        type: "text",
     
    6561            this.own(this.source.on('Drop',
    6662                                    lang.hitch(this,'_handleDrop')));
    67             this.own(on(this.domNode,'change',
    68                         lang.hitch(this,'_handleChange')));
    69         },
    70         create: function() {
    71             this.inherited(arguments);
    72             this._onChangeActive = true;
    7363        },
    7464        creator: function(item, hint) {
     
    8373            } else {
    8474                if ( this.createListElement ) {
    85                     nodeOrWidget = this.createListElement(id,item,this._fromDrop);
     75                    nodeOrWidget =
     76                        this.createListElement(id,item,this._fromDrop);
    8677                } else {
    8778                    return this.source.defaultCreator(item, hint);
    8879                }
    8980            }
    90             var node = nodeOrWidget.domNode ? nodeOrWidget.domNode : nodeOrWidget;
     81            if ( nodeOrWidget.on ) {
     82                this.connectChildsChanges(nodeOrWidget);
     83            }
     84            var node = nodeOrWidget.domNode ?
     85                       nodeOrWidget.domNode :
     86                       nodeOrWidget;
    9187            if ( hint !== "avatar" && node.id !== id ) {
    9288                console.warn("Node id '%s' not equal to generated id '%s'. Is this intended?", node.id, id);
     
    10197        createListElement: null, /* function(id,item,fromDrop){},*/
    10298        _getValueAttr: function() {
    103             this.value = array.map(this.source.getAllNodes(),function(node){
    104                 var widget = registry.byNode(node);
    105                 if ( widget && "value" in widget ) {
    106                     return widget.get('value');
    107                 } else {
    108                     return this.source.getItem(node.id).data;
    109                 }
    110             },this);
    111             return this.value;
     99            return array.map(
     100                this._getDescendantFormWidgets(),
     101                function(child) { return child.get('value'); }
     102                ,this);
    112103        },
    113104        _setValueAttr: function(value,priorityChange) {
    114105            this.clear();
    115             this.appendItems(value || []);
     106            this._setValueInternal(value || [], priorityChange);
     107            this.appendItems(this.value);
    116108        },
    117         _setDisabledAttr: function(value) {
    118             this._set("disabled", value);
    119             array.forEach(this.getChildren(), function(child){
    120                 child.set("disabled", value);
    121             });
    122             this._updateContainer();
     109        _setReadOnlyAttr: function() {
     110            this.inherited(arguments);
     111            this._updateSource();
    123112        },
    124         _setReadOnlyAttr: function(value) {
    125             this._set("readOnly", value);
    126             array.forEach(this.getChildren(), function(child){
    127                 child.set("readOnly", value);
    128             });
    129             this._updateContainer();
     113        _setDisabledAttr: function() {
     114            this.inherited(arguments);
     115            this._updateSource();
    130116        },
    131         _updateContainer: function() {
     117        _updateSource: function() {
    132118            if ( this.disabled || this.readOnly ) {
    133119                this.source.accept = [];
     
    139125            }
    140126        },
    141         focus: function() {
    142             var children = this.getChildren();
    143             if ( children.length > 0 ) {
    144                 children[0].focus();
    145             }
     127        getChildren: function() {
     128            return array.filter(
     129                array.map(this.source.getAllNodes(), function(node){
     130                    return registry.byNode(node);
     131                }),
     132                function(widget){ return widget !== null; });
    146133        },
     134        _getDescendantFormWidgets: function() {
     135            return array.filter(
     136                this.getChildren(),
     137                function(child){ return 'value' in child; });
     138        },
     139
    147140        appendItems: function(items,forceEvent) {
    148141            this._fromDrop = false;
    149142            this.source.insertNodes(false,items);
    150             if ( forceEvent ) { this._handleDrop(); }
     143            this._setValueInternal(
     144                this.get('value'),
     145                forceEvent === true ? true : null);
    151146            this._fromDrop = true;
    152147        },
     
    154149            this._fromDrop = false;
    155150            this.source.insertNodes(false,[item]);
    156             if ( forceEvent ) { this._handleDrop(); }
     151            this._setValueInternal(
     152                this.get('value'),
     153                forceEvent === true ? true : null);
    157154            this._fromDrop = true;
    158155        },
    159156        removeItem: function(key,forceEvent) {
    160             array.forEach(array.filter(this.source.getAllNodes(), function(node){
    161                 return node.id === key;
    162             }), lang.hitch(this, "_destroyNodeOrWidget"));
     157            array.forEach(
     158                array.filter(this.source.getAllNodes(), function(node){
     159                    return node.id === key;
     160                }),
     161                lang.hitch(this, "_destroyNodeOrWidget"));
    163162            this.source.delItem(key);
    164             if ( forceEvent ) { this._handleDrop(); }
    165         },
    166         getChildren: function() {
    167             return array.filter(array.map(this.source.getAllNodes(), function(node){
    168                 return registry.byNode(node);
    169             }),function(widget){ return widget !== null; });
     163            this._setValueInternal(
     164                this.get('value'),
     165                forceEvent === true ? true : null);
    170166        },
    171167        clear: function(forceEvent) {
     
    173169                          lang.hitch(this, "_destroyNodeOrWidget"));
    174170            this.source.clearItems();
    175             if ( forceEvent ) { this._handleDrop(); }
     171            this._setValueInternal(
     172                this.get('value'),
     173                forceEvent === true ? true : null);
    176174        },
    177175        _destroyNodeOrWidget: function(node) {
     
    183181            }
    184182        },
    185         validate: function() {
    186             return array.every(array.map(this.source.getAllNodes(),function(node){
    187                 var widget = registry.byNode(node);
    188                 return !widget || widget.disabled || !widget.validate || widget.validate();
    189             }), function(item){ return item; });
    190         },
    191183        destroy: function() {
    192184            this.source.destroy();
    193185            this.inherited(arguments);
    194186        },
    195         _handleChange: function(evt) {
    196             if ( evt.target !== this.domNode ) {
    197                 this._onChange();
    198                 if ( evt ) { event.stop(evt); }
    199                 return false;
    200             } else {
    201                 return evt;
    202             }
    203         },
    204         _handleDrop: function(evt) {
    205             this._onChange();
    206         },
    207         _onChange: function() {
    208             if ( this._onChangeActive &&
    209                  !(this.readOnly || this.disabled) ) {
    210                 if ( this._onChangeHandle ) {
    211                     this._onChangeHandle.cancel();
    212                 }
    213                 this._onChangeHandle = this.defer(function(){
    214                     this._onChangeHandle = null;
    215                     on.emit(this.domNode,'change',{
    216                         target: this.domNode,
    217                         value: this.get('value'),
    218                         bubbles: true,
    219                         cancellable: true
    220                     });
    221                 });
    222             }
     187        _handleDrop: function() {
     188            this._setValueInternal(this.get('value'));
    223189        }
    224190    });
Note: See TracChangeset for help on using the changeset viewer.