Changeset 33102 for main/trunk/greenstone3/web/interfaces
- Timestamp:
- 2019-05-22T16:58:53+12:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor.js
r33100 r33102 1 // Global autocomplete labels list: one list for all the map editors' shape labels in a page 2 // We use the global labels hashmap to more efficiently ensure uniqueness of labels in our autocomplete list 1 3 var global_autocompleteLabelsList = []; 2 4 var global_labels_hashmap = {}; … … 183 185 // Ensure only unique labels are added to our autocomplete list 184 186 MapEditor.prototype.addToAutocompleteLabelsList = function (newLabel) { 185 if (newLabel !== "" && !global_autocompleteLabelsList.includes(newLabel)) { 187 188 // We use a hashmap to more efficiently ensure uniqueness of labels in our array 189 // https://stackoverflow.com/questions/11040472/how-to-check-if-object-property-exists-with-a-variable-holding-the-property-name 190 if (newLabel !== "" && !global_labels_hashmap.hasOwnProperty(newLabel)) { // label is not empty and unique, so 191 // add to hashmap now 192 global_labels_hashmap[newLabel] = 1; 186 193 // add to autocomplete list and sort alphabetically 187 194 global_autocompleteLabelsList.push(newLabel); 188 global_autocompleteLabelsList.sort(); 195 global_autocompleteLabelsList.sort(); 189 196 } 190 197 } … … 500 507 } 501 508 } 502 ); // TODO: responseFunction in setMeta call509 ); // responseFunctions are now in the setMeta calls 503 510 } 504 511 … … 801 808 map_editor.overlays = new_overlays; 802 809 803 var local_labels_hashmap = {};804 810 for (var i=0; i<map_editor.overlays.length; i++) { 805 811 var shape = map_editor.overlays[i]; 806 812 807 813 // set up the autocomplete list using saved labels/descriptions 808 //map_editor.addToAutocompleteLabelsList(shape.description); // inefficient in ensuring uniqueness of values, use (hash)map 809 if (shape.description !== "") { 810 local_labels_hashmap[shape.description] = 1; // we just want the shape.description added to the map as key, don't care about value 811 // so that we can maintain a list of unique descriptions 812 } 813 814 map_editor.addToAutocompleteLabelsList(shape.description); // now efficiently ensures uniqueness of values using (hash)map 815 814 816 // make the shapes selectable on load: 815 817 if (ShapesUtil.overlayItemIsShape(shape)) { … … 822 824 823 825 this.mapEditorHistory.presentOverlayPush(); 824 825 // DO NOT assign "this.autocompleteLabelsList = keys(local_labels_hashmap);"826 // Because Javascript is like Java and (in this) like C, not like C++. JavaScript uses Call-By-Sharing for objects:827 // when reference addresses that are overwritten inside functions,828 // the new address local to the function is not remembered/visible back outside the function829 // https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language830 // Instead, push.apply=addAll elements of keys to our autocomplete list, see831 // https://stackoverflow.com/questions/35658464/what-is-the-equivalent-of-java-collection-addall-for-javascript-arrays/35658514832 833 // Combining contents of one JavaScript Object/hashmap (or associative 'array') into another is also described as merging our JavaScript Objects, so see834 // https://stackoverflow.com/questions/929776/merging-objects-associative-arrays835 // We've decided to use the jQuery based suggested solution for merging hashmaps:836 // $.extend(obj1, obj2); means obj1 = obj1 + obj2. Note: it additionally overwrites any existing properties in obj1 with new values in obj2.837 $.extend(global_labels_hashmap, local_labels_hashmap);838 839 // important to empty our global autocomplete labels list array840 // See https://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript841 // and https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/842 global_autocompleteLabelsList.length = 0;843 844 var keys = Object.keys(global_labels_hashmap);845 global_autocompleteLabelsList.push.apply(global_autocompleteLabelsList, keys); // addAll keys to our global_autocompleteLabelsList (turns map's keys into array)846 global_autocompleteLabelsList.sort();847 826 } 848 827 … … 888 867 } 889 868 890 // Having got our unique (set) of descriptions/labels, we're ready to set it up as the source for our autocomplete list869 // Global function that uses jquery to set the autocomplete list's data source to whatever's in our global autocomplete labels array 891 870 $(function setupAutocompleteLabelsList() { 892 871 // Overrides the default autocomplete filter function to
Note:
See TracChangeset
for help on using the changeset viewer.