PageSourceSearch

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

js daltile.com collected 2026-09-24 19:03:20 UTC 5,343 bytes, 146 lines download raw bytes

1/**
2 * Provides api for manipulating query model
3 * @module SearchQuery
4 * @param  {jQuery} $ Instance of jQuery
5 * @param  {Document} document dom document object
6 * @return {Object} list of methods for work with query model
7*/
8XA.component.search.query = (function ($, document) {
9    /**
10    * @name module:SearchQuery.QueryModel
11    * @constructor
12    * @augments Backbone.Model
13    */
14    var QueryModel = Backbone.Model.extend(
15        /** @lends module:SearchQuery.QueryModel.prototype **/
16        {
17            /**
18            * Default model options
19            * @default
20            */
21            defaults: {
22                hash: "",
23                hashObj: {}
24            },
25            /**
26            * Checks if hash is not empty and calls 
27            * ['createHashObj']{@link module:SearchQuery.QueryModel#createHashObj}
28            * @memberof SearchQuery.QueryModel
29            * @alias SearchQuery.QueryModel#initialize
30            */
31            initialize: function () {
32                var inst = this,
33                    hash = window.location.hash;
34
35                if (hash.length) {
36                    this.set({ hash: hash });
37                    this.createHashObj();
38                }
39            },
40            /**
41            * Set hashObj into model
42            * @memberof SearchQuery.QueryModel
43            * @alias SearchQuery.QueryModel#createHashObj
44            */
45            createHashObj: function () {
46                this.set({ hashObj: this.parseHashParameters(this.get("hash")) });
47            },
48            /**
49            * Converts hash string into Object
50            * @param {String} aUrl url for converting hash
51            * @memberof SearchQuery.QueryModel
52            * @alias SearchQuery.QueryModel#parseHashParameters
53            * @returns {Object} hash parameters as an object
54            */
55            parseHashParameters: function (aUrl) {
56                if (aUrl === null || aUrl === "") {
57                    return {};
58                }
59
60                aUrl = aUrl || window.location.hash;
61                var vars = {};
62                var hashes = aUrl.slice(aUrl.indexOf('#') + 1).split('&');
63
64                hashes = hashes.filter(function (x) {
65                    return x !== "";
66                });
67
68                for (var i = 0; i < hashes.length; i++) {
69                    var hash = hashes[i].split('='),
70                        facetPart = hash[0].toLowerCase();
71                    if (hash.length > 1) {
72                        vars[decodeURIComponent(facetPart)] = decodeURIComponent(hash[1].replace(/[+]/g, " "));
73                    }
74                    else {
75                        vars[decodeURIComponent(facetPart)] = null;
76                    }
77                }
78                return vars;
79            },
80            /**
81           * updates hash parameters
82           * @param {Object} newHash hash with new values
83           * @param {String} targetUrl targetUrl from component propertie
84           * @memberof SearchQuery.QueryModel
85           * @alias SearchQuery.QueryModel#updateHash
86           */
87            updateHash: function (newHash, targetUrl) {
88                var inst = this,
89                    hashStr = "#",
90                    hashObj = this.parseHashParameters(window.location.hash);
91
92                _.each(newHash, function (item, key) {
93                    if (item !== "") {
94                        hashObj[key] = item;
95                    } else {
96                        delete hashObj[key];
97                    }
98
99                });
100
101                if ((targetUrl === "#") || (targetUrl == undefined)) {
102                    targetUrl = window.location.pathname;
103                }
104
105                var i = 0;
106                _.each(hashObj, function (item, key) {
107                    if (i > 0) {
108                        hashStr += "&";
109                    }
110                    i++;
111                    hashStr += key + "=" + encodeURIComponent(item).replace(/%7C/g, "|");
112                });
113
114                Backbone.history.navigate(hashStr, { trigger: true });
115
116                if (hashStr.length) {
117                    this.set({ hash: hashStr });
118                    this.createHashObj();
119                }
120            },
121            /**
122            * Checks if signature present in hash
123            * @param {Object} hash hash object
124            * @param {String} signature signature for searching in hash parameter
125            * @memberof SearchQuery.QueryModel
126            * @alias SearchQuery.QueryModel#updateHash
127            * @returns {Boolean} True - if hash present, False if not
128            */
129            isSignaturePresentInHash: function (hash, signature) {
130                var hashKeys = Object.keys(hash),
131                    isSignatureFoundInHash = false;
132
133                for (var len = hashKeys.length, i = 0; i < len; i++) {
134                    if (hashKeys[i].startsWith(signature)) {
135                        isSignatureFoundInHash = true;
136                        break;
137                    }
138                }
139
140                return isSignatureFoundInHash;
141            }
142        });
143
144    return new QueryModel();
145
146}(jQuery, document));

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.