PageSourceSearch

https://www.daltile.com/-/media/Base-Themes/SearchTheme/Scripts/component-search-sort.js

js daltile.com collected 2026-09-24 19:03:25 UTC 7,543 bytes, 198 lines download raw bytes

1/**
2 * Search sorting component functionality
3 * @module searchSort
4 * @param  {jQuery} $ Instance of jQuery
5 * @param  {Document} document dom document object
6 * @return {Object} list of methods for working with search sort component
7*/
8XA.component.search.sort = (function ($, document) {
9    /**
10    * This object stores all public api methods
11    * @type {Object.<Methods>}
12    * @memberOf module:searchSort
13    */
14    var api = {},
15        queryModel,
16        queryParameters,
17        getSignature;
18    /**
19    * @name module:searchSort.SortModel
20    * @constructor
21    * @augments Backbone.Model
22    */
23    var SortModel = Backbone.Model.extend(
24        /** @lends module:searchSort.SortModel.prototype **/
25        {
26            /**
27            * Default model options
28            * @default
29            */
30            defaults: {
31                updateOrder: false,
32                sig: []
33            }
34        });
35    /**
36    * @name module:searchSort.SortView
37    * @constructor
38    * @augments Backbone.View
39    */
40    var SortView = XA.component.search.baseView.extend(
41        /** @lends module:searchSort.SortView.prototype **/
42        {
43            /**
44            * Initially sets data to model and watches events on which
45            * view should be updated
46            * @listens module:XA.component.search.vent~event:hashChanged
47            * @memberof module:searchSort.SortView
48            * @alias module:searchSort.SortView#initialize
49            */
50            initialize: function () {
51                var dataProperties = this.$el.data();
52
53                if (this.model) {
54                    this.model.set("sig", this.translateSignatures(dataProperties.properties.sig, "o"));
55                }
56
57                XA.component.search.vent.on("hashChanged", this.updateComponent.bind(this));
58            },
59            /**
60             * list of events for Backbone View
61            * @memberof module:searchSort.SortView
62            * @alias module:searchSort.SortView#events
63             */
64            events: {
65                'click .sort-results-group a': 'sortSearchResultsLink',
66                'change select': 'sortSearchResultsSelect'
67            },
68            /**
69             * prevents default event and calls
70             * ['sortSearchResults']{@link module:searchSort.SortView.sortSearchResults}
71             * method
72             * @param {Event} event
73             * @memberof module:searchSort.SortView
74            * @alias module:searchSort.SortView#sortSearchResultsLink
75             */
76            sortSearchResultsLink: function (event) {
77                event.preventDefault();
78                this.sortSearchResults($(event.currentTarget).parent());
79            },
80            /**
81             * call 
82             * ['sortSearchResults']{@link module:searchSort.SortView.sortSearchResults}
83             * method
84             * @param {Event} event
85             * @memberof module:searchSort.SortView
86            * @alias module:searchSort.SortView#sortSearchResultsSelect
87             */
88            sortSearchResultsSelect: function (event) {
89                this.sortSearchResults($(event.currentTarget[event.currentTarget.options.selectedIndex]));
90            },
91            /**
92            * Updates hash with sorting value
93            * @param {DomElement} element Element that contains data that is needed for sorting
94            * @memberof module:searchSort.SortView
95            * @alias module:searchSort.SortView#sortSearchResults
96            */
97            sortSearchResults: function (element) {
98                var attributes = element.data(),
99                    sig = this.model.get("sig"),
100                    attrString = attributes.facet + ',' + attributes.direction;
101
102                if (attributes.direction !== "") {
103                    queryParameters.updateHash(this.updateSignaturesHash(sig, attrString, {}));
104                } else {
105                    queryParameters.updateHash(this.updateSignaturesHash(sig, "", {}));
106                }
107            },
108            /**
109            * Updates component selected value according to hash value
110            * @param {Object} hash Hash value stores as an object
111            * @memberof module:searchSort.SortView
112            * @alias module:searchSort.SortView#updateComponent
113            */
114            updateComponent: function (hash) {
115                var optionToSelect, sortData,
116                    sig = this.model.get("sig"),
117                    i;
118
119                for (i = 0; i < sig.length; i++) {
120                    if (hash.hasOwnProperty(sig[i])) {
121                        sortData = hash[sig[i]].split(',');
122                        optionToSelect = this.$el.find("[data-facet='" + sortData[0] + "'][data-direction='" + sortData[1] + "']");
123                    } else {
124                        optionToSelect = this.$el.find("[data-facet][data-direction]:first");
125                    }
126                }
127
128                this.$el.find("[data-facet][data-direction]").removeAttr("selected");
129                optionToSelect.attr("selected", "selected");
130            }
131        });
132    /**
133        * For each search sorting component on a page creates instance of 
134        * ["SortModel"]{@link module:searchSort.SortModel} and 
135        * ["SortView"]{@link module:searchSort.SortView} 
136        * @memberOf module:searchSort
137        * @alias module:searchSort.init
138        */
139    api.init = function () {
140        if ($("body").hasClass("on-page-editor")) {
141            return;
142        }
143
144        queryModel = XA.component.search.query;
145        queryParameters = XA.component.search.parameters;
146
147        var sort = $(".sort-results:not(.initialized)");
148        
149        _.each(sort, function (elem) {
150            var $el = $(elem),
151                sortModel = new SortModel(),
152                view = new SortView({ el: $el, model: sortModel });
153            
154            $el.addClass("initialized");
155        });
156    };
157    /**
158    * Returns direction of sorting
159    * @param {String} signature component signature
160    * @memberOf module:searchSort
161    * @alias module:searchSort.getFirstSortingOption
162    * @retruns {String|Number} return string "facetName,direction" or -1
163    */
164    api.getFirstSortingOption = function (signature) {
165        var sortResults = $(".sort-results"),
166            firstSort,
167            attributes,
168            attrString,
169            thisSignatures,
170            data,
171            i, j;
172
173        for (i = 0; i < sortResults.length; i++) {
174            if (typeof (signature) !== "undefined") {
175                data = $(sortResults[i]).data();
176                thisSignatures = data.properties.sig.split(',');
177
178                for (j = 0; j < thisSignatures.length; j++) {
179                    if (thisSignatures[j] === signature) {
180                        firstSort = $(sortResults[i]).find("[data-facet][data-direction]");
181                        attributes = firstSort.data();
182                        if (attributes.direction !== "") {
183                            attrString = attributes.facet + ',' + attributes.direction;
184                            return attrString;
185                        }
186                    }
187                }
188            }
189        }
190
191        return -1;
192    };
193
194    return api;
195
196}(jQuery, document));
197
198XA.register('searchSort', XA.component.search.sort);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.