Changeset 33096 for main


Ignore:
Timestamp:
2019-05-17T19:41:19+12:00 (2 years ago)
Author:
wy59
Message:

Some improvements t

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor.js

    r33095 r33096  
    1 
    21
    32function MapEditor(id) {
     
    174173                    return message;
    175174                }
    176             }               
    177         }
    178        
    179     }
    180 
    181     $(function () {
     175            }
     176        }
     177    }
     178
     179    // Having got our unique (set) of descriptions/labels, we're ready to set it up as the source for our autocomplete list
     180    $(function setupAutocompleteLabelsList() {
    182181        // Overrides the default autocomplete filter function to
    183182        // search only from the beginning of the string
     
    200199MapEditor.prototype.addToAutocompleteLabelsList = function (newLabel) {
    201200   
    202     if (!this.autocompleteLabelsList.includes(newLabel)) {
    203         // TODO: add to end with push, add to start with unshift or sort alphabetically?
    204         this.autocompleteLabelsList.unshift(newLabel);
     201    if (newLabel !== "" && !this.autocompleteLabelsList.includes(newLabel)) {
     202        // add to autocomplete list and sort alphabetically
     203        this.autocompleteLabelsList.push(newLabel);
     204        this.autocompleteLabelsList.sort();
    205205    }
    206206}
     
    803803MapEditor.prototype.LOAD = function (json_str, nodeID) {
    804804    this.mapEditorHistory.historyOverlayPush();
    805    
    806    
     805
    807806    // This seems to convert the map_store object into an array and forces array index access, instead of convenient property access using nodeID   
    808807    //Object.values(gsmap_store)[0]; // Always gets top level section's map-editor, not what we want.
     
    817816   
    818817    map_editor.overlays = new_overlays;
    819    
     818
     819    var labels_hashmap = {};
    820820    for (var i=0; i<map_editor.overlays.length; i++) {
    821821        var shape = map_editor.overlays[i];
    822822
    823         // set up the autocomplete list using saved labels/descriptions
    824         map_editor.addToAutocompleteLabelsList(shape.description);
     823        // set up the autocomplete list using saved labels/descriptions
     824        //map_editor.addToAutocompleteLabelsList(shape.description); // inefficient in ensuring uniqueness of values, use (hash)map
     825        if (shape.description !== "") {
     826            labels_hashmap[shape.description] = 1;  // we just want the shape.description added to the map as key, don't care about value
     827                                                    // so that we can maintain a list of unique descriptions
     828        }
    825829
    826830        // make the shapes selectable on load:
     
    834838
    835839    this.mapEditorHistory.presentOverlayPush();
     840   
     841    var keys = Object.keys(labels_hashmap);
     842    // DO NOT assign "this.autocompleteLabelsList = keys(labels_hashmap);"
     843    // Because Javascript is like Java and (in this) like C, not like C++. JavaScript uses Call-By-Sharing for objects:
     844    // when reference addresses that are overwritten inside functions,
     845    // the new address local to the function is not remembered/visible back outside the function
     846    // https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language
     847    // Instead, push.apply=addAll elements of keys to our autocomplete list, see
     848    // https://stackoverflow.com/questions/35658464/what-is-the-equivalent-of-java-collection-addall-for-javascript-arrays/35658514
     849    this.autocompleteLabelsList.push.apply(this.autocompleteLabelsList, keys); // addAll keys to our autocompleteLabelsList
     850    this.autocompleteLabelsList.sort();
    836851}
    837852
Note: See TracChangeset for help on using the changeset viewer.