storkCore

Check-in [52a362b577]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added support in ListOfListItem for only rendering if sublist has content This means that e.g. sections with empty sublists can be set to not appear.
Timelines: family | ancestors | descendants | both | emptySublist
Files: files | file ages | folders
SHA1:52a362b577024ab4534c6adb2a1716e42babe006
User & Date: setok 2018-02-06 09:30:47
Context
2018-02-06
18:22
(no comment) check-in: 953053e8fb user: setok tags: emptySublist
09:30
Added support in ListOfListItem for only rendering if sublist has content This means that e.g. sections with empty sublists can be set to not appear. check-in: 52a362b577 user: setok tags: emptySublist
09:28
Create new branch named "emptySublist" check-in: 2e93e6d513 user: setok tags: emptySublist
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to list.js.

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
...
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
...
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
...
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
...
696
697
698
699
700
701
702
703

704
705
706

707
708
709

710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
...
787
788
789
790
791
792
793



794
795
796
797
798
799
800











801
802
803
804
805
806
807
...
824
825
826
827
828
829
830




















831
832
833
834
835
836
837
838
839
840
841
842

843
844
845
846
847
848

849
850





















851
852
853
854
855
856
857
858
859
860
861

862
863
864
865
866
867
868
...
906
907
908
909
910
911
912


913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
....
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
....
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
        //itemController.init();
        //console.log("Model for item " + i + ":", listModel.getItemModel(i));
        itemController.setModel(listModel.getItemModel(i));
        itemController.setListID(this.listID);
        itemController.setListItemID(this.nextItemID);
        itemController.setListModel(listModel);
        this.nextItemID++;
        itemController.createElement(document, this.containerElement);
        itemController.drawed();
        this.itemControllers[i] = itemController;
        this.informListeners('listItemControllerAdded',[this,
                                                        itemController,
                                                        i]);
    }

................................................................................
    itemController.setListItemID(this.nextItemID);
    this.nextItemID++;
    var nextController = this.itemControllers[index];
    if(nextController) {
        var nextElement = nextController.getElement();
        /*console.log("insertBeforeEvent - nextController.getElement:");
         console.log(nextElement);*/
        itemController.createElement(document, this.containerElement, nextElement);
        this.itemControllers.splice(index, 0, itemController);
    }
    else {
        /*console.log(this.containerElement);*/
        itemController.createElement(document, this.containerElement);
        this.itemControllers.push(itemController);
    }

    itemController.setListModel(this.model);
    itemController.drawed();

    this.informListeners('listItemControllerAdded',[this,
................................................................................


/**
 * @private
 */
ListItemController._refreshTextProperties = function() {
    for (var key in this.textProperties) {
        //console.log("createElement, key: " + key);
        if (this.textProperties[key] != undefined) {
            //console.log("found property: " + key);
            //var elementID = this.textProperties[key] + "_#n";
            var elementID = this.generateElementID(this.textProperties[key]);
            //console.log("createElement, elementID:");
            //console.log(elementID);
            var textContainer = document.getElementById(elementID);
            if (textContainer != null) {
                emptyElement(textContainer);
                var text = this.model.getProperty(key);
                //console.log("createElement - text: " + text);
                textContainer.appendChild(document.createTextNode(text));
            }
        }
    }
};

/**
................................................................................

/**
 * @private
 */
ListItemController._refreshAttributesProperties = function() {
    for (var key in this.attrProperties) {
        if (this.attrProperties[key] != undefined) {
            //console.log("createElement: found attrProperty: " + key);
            var data = this.attrProperties[key];
            //console.log("createElement: data");
            //console.log(data);
            var elementID = this.generateElementID(data["idPrefix"]);
            //console.log("createElement: elementID: " + elementID);
            var element = document.getElementById(elementID);
            if (element != null) {
                var value = this.model.getProperty(key);
                element.setAttribute(data["attribute"], value);
            }
        }
    }
................................................................................
 * @private
 */
ListItemController._refreshMapAttributes = function() {
    for (var key in this.attrMaps) {
        var data = this.attrMaps[key];
        if (this.model.hasProperty(key)) {
            var elementID = this.generateElementID(data["idPrefix"]);
            //console.log("createElement: elementID: " + elementID);
            var element = document.getElementById(elementID);

            if (element != undefined) {
                var map = data["propertyMap"];
                var value = this.model.getProperty(key);
                var mappedValue = map[value];
                if (mappedValue != undefined) {
................................................................................
        this.refresh();
    }
};


/**
 * Create the actual HTML element that will match this list item, including
 * the content from the model set up earlier.

 *
 * 'document' is the document to enter the newly created element into, and
 * 'containerElement' is the element that it will be appended to.

 * If 'beforeElement' is defined, the new element should be created right
 * before it, but as a child of 'containerElement'.
 */

ListItemController.createElement = function(document,
                                            containerElement,
                                            beforeElement) {
    //console.log("createElement - listID: " + this.listID);
    this.IDPrefix = "ListItemController" + this.getListID() + ".";
    var newNode = cloneNodeSetIDs(this.itemTemplate,
                                  this.IDPrefix,
                                  this.getListItemID());

    //console.log("createElement - newNode:", newNode, containerElement);
    //console.debug("container in DOM", document.getElementById(containerElement.id) ? true: false);

    if (beforeElement == undefined) {
        containerElement.appendChild(newNode);
    } else {
        containerElement.insertBefore(newNode, beforeElement);
    }

    this.myElement = newNode;

    if (this.model !== undefined) {
        this.refresh();
    }
    /*
     var nodeID = newNode.id;
     nodeID = nodeID.replace("#n", "#" + this.listID);

     newNode.id = nodeID;
     */

    //console.log("createNewElement done");
};


/**
 * Return the element from within the containing element of this list item
................................................................................
 * (sectioned lists).
 *
 * @class StorkUtil.ListOfListsItem
 * @extends StorkUtil.ListItemController
 * @singleton
 *****************************************************************************/
var ListOfListsItem = clone(ListItemController);




/**
 * @property subListController
 */
ListOfListsItem.clonedProperty("subListController", ListController);













/**
 * Get the ListController for the sub-list within this item.
 * This ListController is automatically created when a ListOfListItem is
 * cloned.
 */
ListOfListsItem.getSubListController = function() {
    return this.subListController;
................................................................................
 * ListModel object as its value, and which will be used to populate the
 * sub-list
 */
ListOfListsItem.setSubListModelProperty = function(propertyName) {
    this.subListModelProperty = propertyName;
};






















/**
 * Override the normal ListItemController createElement() with one that
 * builds the sub-list.
 */
ListOfListsItem.createElement = function(document, containerElement,
                                         beforeElement) {
    var subListModelProperty;
    var listContainer;

    //console.debug("ListOfListsItem - createElement");
    this.superMethod(ListOfListsItem.createElement, "createElement", document, containerElement, beforeElement);


    listContainer = this.getContainedElement(this.subListContainer);
    this.subListController.setListContainer(listContainer);

    subListModelProperty = this.model.getProperty(this.subListModelProperty);
    this.subListController.setModel(subListModelProperty);

};























//***********************************************************
// * Model for lists
//**********************************************************/

/**
 * Model for lists
 *
 * @class StorkUtil.ListModel
 * @extends StorkCore.StorkModel
 * @singleton

 */
var ListModel = clone(StorkModel);

/**
 * @property {Array} items
 */
ListModel.copiedProperty("items", []);
................................................................................
 * ListModel, the item deleted, and the index of the item deleted.
 */
ListModel.deleteIndex = function(index) {
    var i, item;

    item = this.items[index];
    this.items.splice(index, 1);


    for (i=0; i < this.listeners.length; i++) {
        if (this.listeners[i].deleteItemEvent != undefined) {
            this.listeners[i].deleteItemEvent(this, item, index);
        }
    }
};


/**
 * Insert a list item model object, based on 'object',
 * before the one specified in 'index'.
 *
 * @param {Object} object is any Javascript object. Each property of it will be made
 *      into a StorkModel property.
 *
 *      Calls insertBeforeEvent(sourceModel, model, index) for each listener,
 *      where 'sourceModel' is this ListModel, 'model' is the added model and
 *
 * @param {Number} index is the index where it now resides.
 *
 * @return {StorkCore.StorkModel}
 */

ListModel.insertObjectBefore = function(object, index) {
    var model = StorkModel.createFromObject(object);
    this.items.splice(index, 0, model);
................................................................................
 *          listener, where 'sourceModel' is this ListModel, 'model' is the
 *          added model
 */

ListModel.push = function(storkModel) {
    this.items.push(storkModel);
    this.informListeners("insertBeforeEvent",
                         [this, storkModel, this.items.length-1]);
    return;
};


/**
 * Insert a list item model object, based on 'object' to end of list.
 *
................................................................................
    }

    return items;
};


/**
 * Get the item model for the given 'itemNumber' (an index into the item list).
 */
ListModel.getItemModel = function(itemNumber) {
    return this.items[itemNumber];
};

/** ***********************************************************************
 * implements a list with filtering of its items
 **************************************************************************/
var FilteredListModel = clone(ListModel);

................................................................................
    return model;
};

//override
FilteredListModel._pushObject = function(model) {
    this.items.push(model);
    this.informListeners("insertBeforeEvent",
                         [this, model, this.items.length-1]);
    return model;
};

FilteredListModel._deleteIndex = FilteredListModel.deleteIndex;

FilteredListModel.throwReadOnlyError = function(){
    throw Error("Method not available in read only FilteredListModel instance");







|







 







|




|







 







<




<





<







 







<

<


<







 







<







 







|
>

<
|
>



>
|
<

<





<
<
<











<
<
<
<
<
<







 







>
>
>







>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





|

|



|
>




|
|
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|
|
|
>







 







>
>




|







|
|



<
|







 







|







 







|

|
|







 







|







123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
...
544
545
546
547
548
549
550

551
552
553
554

555
556
557
558
559

560
561
562
563
564
565
566
...
582
583
584
585
586
587
588

589

590
591

592
593
594
595
596
597
598
...
602
603
604
605
606
607
608

609
610
611
612
613
614
615
...
689
690
691
692
693
694
695
696
697
698

699
700
701
702
703
704
705

706

707
708
709
710
711



712
713
714
715
716
717
718
719
720
721
722






723
724
725
726
727
728
729
...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
...
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973

974
975
976
977
978
979
980
981
...
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
....
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
....
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
        //itemController.init();
        //console.log("Model for item " + i + ":", listModel.getItemModel(i));
        itemController.setModel(listModel.getItemModel(i));
        itemController.setListID(this.listID);
        itemController.setListItemID(this.nextItemID);
        itemController.setListModel(listModel);
        this.nextItemID++;
        itemController.createElement(this.containerElement);
        itemController.drawed();
        this.itemControllers[i] = itemController;
        this.informListeners('listItemControllerAdded',[this,
                                                        itemController,
                                                        i]);
    }

................................................................................
    itemController.setListItemID(this.nextItemID);
    this.nextItemID++;
    var nextController = this.itemControllers[index];
    if(nextController) {
        var nextElement = nextController.getElement();
        /*console.log("insertBeforeEvent - nextController.getElement:");
         console.log(nextElement);*/
        itemController.createElement(this.containerElement, nextElement);
        this.itemControllers.splice(index, 0, itemController);
    }
    else {
        /*console.log(this.containerElement);*/
        itemController.createElement(this.containerElement);
        this.itemControllers.push(itemController);
    }

    itemController.setListModel(this.model);
    itemController.drawed();

    this.informListeners('listItemControllerAdded',[this,
................................................................................


/**
 * @private
 */
ListItemController._refreshTextProperties = function() {
    for (var key in this.textProperties) {

        if (this.textProperties[key] != undefined) {
            //console.log("found property: " + key);
            //var elementID = this.textProperties[key] + "_#n";
            var elementID = this.generateElementID(this.textProperties[key]);

            //console.log(elementID);
            var textContainer = document.getElementById(elementID);
            if (textContainer != null) {
                emptyElement(textContainer);
                var text = this.model.getProperty(key);

                textContainer.appendChild(document.createTextNode(text));
            }
        }
    }
};

/**
................................................................................

/**
 * @private
 */
ListItemController._refreshAttributesProperties = function() {
    for (var key in this.attrProperties) {
        if (this.attrProperties[key] != undefined) {

            var data = this.attrProperties[key];

            //console.log(data);
            var elementID = this.generateElementID(data["idPrefix"]);

            var element = document.getElementById(elementID);
            if (element != null) {
                var value = this.model.getProperty(key);
                element.setAttribute(data["attribute"], value);
            }
        }
    }
................................................................................
 * @private
 */
ListItemController._refreshMapAttributes = function() {
    for (var key in this.attrMaps) {
        var data = this.attrMaps[key];
        if (this.model.hasProperty(key)) {
            var elementID = this.generateElementID(data["idPrefix"]);

            var element = document.getElementById(elementID);

            if (element != undefined) {
                var map = data["propertyMap"];
                var value = this.model.getProperty(key);
                var mappedValue = map[value];
                if (mappedValue != undefined) {
................................................................................
        this.refresh();
    }
};


/**
 * Create the actual HTML element that will match this list item, including
 * the content from the model set up earlier. This is mostly intended to be
 * called by a ListController.
 *

 * 'containerElement' is the element that the newly created element will be 
 *                    appended to.
 * If 'beforeElement' is defined, the new element should be created right
 * before it, but as a child of 'containerElement'.
 */

ListItemController.createElement = function(containerElement,

                                            beforeElement) {

    this.IDPrefix = "ListItemController" + this.getListID() + ".";
    var newNode = cloneNodeSetIDs(this.itemTemplate,
                                  this.IDPrefix,
                                  this.getListItemID());




    if (beforeElement == undefined) {
        containerElement.appendChild(newNode);
    } else {
        containerElement.insertBefore(newNode, beforeElement);
    }

    this.myElement = newNode;

    if (this.model !== undefined) {
        this.refresh();
    }







    //console.log("createNewElement done");
};


/**
 * Return the element from within the containing element of this list item
................................................................................
 * (sectioned lists).
 *
 * @class StorkUtil.ListOfListsItem
 * @extends StorkUtil.ListItemController
 * @singleton
 *****************************************************************************/
var ListOfListsItem = clone(ListItemController);

ListOfListsItem.renderIfEmpty = true;


/**
 * @property subListController
 */
ListOfListsItem.clonedProperty("subListController", ListController);


ListOfListsItem.deleted = function() {
    this.superMethod(ListOfListsItem.deleted, "deleted");

    if (this.model && this.sublistModelProperty) {
        subListModel = this.model.getProperty(this.subListModelProperty);
        sublistModel.removeListener(this);
    }
    return;
};

    
/**
 * Get the ListController for the sub-list within this item.
 * This ListController is automatically created when a ListOfListItem is
 * cloned.
 */
ListOfListsItem.getSubListController = function() {
    return this.subListController;
................................................................................
 * ListModel object as its value, and which will be used to populate the
 * sub-list
 */
ListOfListsItem.setSubListModelProperty = function(propertyName) {
    this.subListModelProperty = propertyName;
};


/**
 * Override ListItemController refresh() to implement functionality of
 * renderIfEmpty().
 */

ListOfListsItem.refresh = function() {    
    if (! this.renderIfEmpty) {
        var sublistModel = this.getSubListController().getModel();

        if (!sublistModel || (sublistModel.getLength() == 0)) {
            hideElement(this.myElement);
        } else {
            showElement(this.myElement);
        }                
    }
        
    this.superMethod(ListOfListsItem.refresh, "refresh");
};


/**
 * Override the normal ListItemController createElement() with one that
 * builds the sub-list.
 */
ListOfListsItem.createElement = function(containerElement,
                                         beforeElement) {
    var subListModel;
    var listContainer;

    //console.debug("ListOfListsItem - createElement");
    this.superMethod(ListOfListsItem.createElement, "createElement", 
                     containerElement, beforeElement);

    listContainer = this.getContainedElement(this.subListContainer);
    this.subListController.setListContainer(listContainer);

    subListModel = this.model.getProperty(this.subListModelProperty);
    this.subListController.setModel(subListModel);
    subListModel.addListener(this);
};


ListOfListsItem.renderIfEmpty = function(render) {
    this.renderIfEmpty = render;
    return;
};


/* If the underlining list changes, refresh this item. The item may need to 
   change its appearance based on changes in the sublist */

ListOfListsItem.insertBeforeEvent = function(list, item, index) {
    this.refresh();
};

ListOfListsItem.deleteItemEvent = function(list, item, index) {
    this.refresh();
};

ListOfListsItem.resetEvent = function(list) {
    this.refresh();
};

//***********************************************************
// * Model for lists
//**********************************************************/

/**
 * Model for lists
 *
 * Events thrown: 
 * insertBeforeEvent  (newItem, index)
 * deleteItemEvent (item, index)
 * resetEvent
 */
var ListModel = clone(StorkModel);

/**
 * @property {Array} items
 */
ListModel.copiedProperty("items", []);
................................................................................
 * ListModel, the item deleted, and the index of the item deleted.
 */
ListModel.deleteIndex = function(index) {
    var i, item;

    item = this.items[index];
    this.items.splice(index, 1);
    this.informListeners("deleteItemEvent", [this, item, index]);
    /*
    for (i=0; i < this.listeners.length; i++) {
        if (this.listeners[i].deleteItemEvent != undefined) {
            this.listeners[i].deleteItemEvent(this, item, index);
        }
    }*/
};


/**
 * Insert a list item model object, based on 'object',
 * before the one specified in 'index'.
 *
 * @param {Object} object is any Javascript object. Each property of it will 
 *      be made into a StorkModel property.
 *
 *      Calls insertBeforeEvent(sourceModel, model, index) for each listener,
 *      where 'sourceModel' is this ListModel, 'model' is the added model and

 *      'index' is the index where it now resides.
 *
 * @return {StorkCore.StorkModel}
 */

ListModel.insertObjectBefore = function(object, index) {
    var model = StorkModel.createFromObject(object);
    this.items.splice(index, 0, model);
................................................................................
 *          listener, where 'sourceModel' is this ListModel, 'model' is the
 *          added model
 */

ListModel.push = function(storkModel) {
    this.items.push(storkModel);
    this.informListeners("insertBeforeEvent",
                         [this, storkModel, this.items.length]);
    return;
};


/**
 * Insert a list item model object, based on 'object' to end of list.
 *
................................................................................
    }

    return items;
};


/**
 * Get the item model for the given 'index' (an index into the item list).
 */
ListModel.getItemModel = function(index) {
    return this.items[index];
};

/** ***********************************************************************
 * implements a list with filtering of its items
 **************************************************************************/
var FilteredListModel = clone(ListModel);

................................................................................
    return model;
};

//override
FilteredListModel._pushObject = function(model) {
    this.items.push(model);
    this.informListeners("insertBeforeEvent",
                         [this, model, this.items.length]);
    return model;
};

FilteredListModel._deleteIndex = FilteredListModel.deleteIndex;

FilteredListModel.throwReadOnlyError = function(){
    throw Error("Method not available in read only FilteredListModel instance");

Changes to storkCore.js.

529
530
531
532
533
534
535

















536
537
538
539
540
541
542

    for (var key in source) {
        ob[key] = source[key];
    }

    return ob;
}


















function emptyElement(element) {
    //    children = element.childNodes;
    if(isString(element)) {
        element = elementByID(element);
    }
    if(!element || !element.hasChildNodes()) {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

    for (var key in source) {
        ob[key] = source[key];
    }

    return ob;
}


function hideElement(element) {
    element.setAttribute("hidden", "");
    // Can be used in CSS for those browsers that don't support the 'hidden'
    // attribute
    addClass(element, "hidden");
    return;
};


function showElement(element) {
    element.removeAttribute("hidden", "");
    removeClass(element, "hidden");
    return;
};


function emptyElement(element) {
    //    children = element.childNodes;
    if(isString(element)) {
        element = elementByID(element);
    }
    if(!element || !element.hasChildNodes()) {