PageSourceSearch

https://droom.in/themes/common/js/locations.js

js droom.in collected 2026-10-02 03:36:43 UTC 24,268 bytes, 504 lines download raw bytes

1/*!
2 * A jQuery Plugin to render cities
3 * Plugin Name: cities1 (product, make, model, year, trim)
4 * Author: @dsingh
5 */
6;
7$(document).mouseup(function(e) 
8    {
9        var container = $(".lp-dropdown");
10        // if the target of the click isn't the container nor a descendant of the container
11        if (!container.is(e.target) && container.has(e.target).length === 0) 
12        {
13            $(".lp-dropdown").addClass("hidden");
14            $('.lp-search').val('');
15            $(".overlay").addClass("hidden");
16            $('#headerPopularLocIn').css('display','block');
17           
18        }
19    });
20
21
22$(".hp-select-city").on("click", function() {
23    $('.hp-select-city').val('');
24    $('.lp-search-location').css('display','none');
25});
26(function ($, window, document, undefined) {
27    var pluginName = 'locations',
28            defaults = {
29                selected: null,
30                cities: null,
31                name: null,
32                onSelect: null,
33                search: true,
34                detectLocation: true,
35                saveInCookie: true,
36                inline: false,
37                fullHeight: false,
38                onManualSelect: null,
39                showCityText: false
40            };
41          
42    // The actual plugin constructor
43    function Plugin(element, options) {
44        this.element = element;
45        this.options = $.extend({}, defaults, options);
46        this._defaults = defaults;
47        this._name = pluginName;
48        this.init();
49    }
50    function getCitiesSpilit(val){
51        var splitVal = val.split(",");
52        var getCity = splitVal[0].trim();
53        var getState = splitVal[1]!==undefined?', '+splitVal[1]:'';
54        return `<span>${getCity}</span> ${getState}`;
55    }
56
57    function capitalize(s)
58    { 
59        if(s.length>0)
60        {
61            return s[0].toUpperCase() + s.slice(1);
62        }
63    }
64 
65    function saveInLocalStorage(city,val){
66        var recent_cities = JSON.parse(localStorage.getItem('recent_cities')) || [];
67        var recentCity = capitalize(val);
68        recentCity  = recentCity.replace(' ,',',');
69        recentCity  = recentCity.replace('  ',' ');
70        var recent_cities1 = {"city":recentCity,"matchCity":city};
71        let isAvail = 0;
72        recent_cities.map(function(cities){
73            if(cities.matchCity==city)
74            {
75                 isAvail =  1;
76                
77            }
78        })
79        if(isAvail==0)
80        {
81            recent_cities.push(recent_cities1);
82            localStorage.setItem("recent_cities", JSON.stringify(recent_cities));
83    
84        }
85    var recent_city_del = JSON.parse(localStorage.getItem('recent_cities')) || [];
86    let iSAvail = 0;
87    var getCount = recent_city_del.length;
88     if (getCount > 4) {
89        var ele = recent_city_del;
90        ele.splice(0, 1);
91        localStorage.setItem("recent_cities", JSON.stringify(ele));
92     }
93    }
94    
95    $.extend(Plugin.prototype, {
96        init: function () {
97            this.buildCache();
98            this.buildHtml();
99            this.getCities();
100            this.bindEvents();
101            this.selectCity();
102        },
103        destroy: function () {
104            this.unbindEvents();
105            this.$element.removeData();
106        },
107        buildCache: function () {
108            this.$element = $(this.element);
109        },
110        bindEvents: function () {
111            var plugin = this;
112            var main = plugin.$element;
113            var cities_element = main.find(".lp-element");
114            var search_cities =$(".hp-select-city");
115            var locate_me = main.find(".lp-locate-me-btn");
116            var results = main.find(".lp-cities");
117            var cities_dropdown = main.find(".lp-dropdown");
118            
119            if(plugin.options.inline === false){
120                main.on("click", ".lp-element", function (e) {
121                    $('#headerPopularLocIn').css('display','none');
122                    $('#headerPopularLoc').css('width','550px');
123                    if ($(window).width() < 768) {
124                        $('#headerPopularLoc').css('height','340px');
125                     }
126                     else {
127                        $('#headerPopularLoc').css('height','273px');
128                     }
129                   
130
131                    
132                    if (cities_dropdown.hasClass("hidden")) {
133                        cities_dropdown.removeClass("hidden");
134                        $(".overlay").removeClass("hidden");
135                        search_cities.focus();
136                    } else {
137                        cities_dropdown.addClass("hidden");
138                        $(".overlay").addClass("hidden");
139                    }
140                });
141                $("body").on("mouseover", locate_me, function(){
142                    locate_me.css('cursor', 'pointer');
143                });
144                $("body").on("click", ".overlay", function(){
145                    cities_dropdown.addClass("hidden");
146                    $(".overlay").addClass("hidden");
147
148                });
149                if($('.overlay').hasClass('hidden')){
150                    cities_dropdown.removeClass("hidden");
151                }
152            }
153            
154            search_cities.searchList(results, 'data-val');
155            locate_me.on("click", function (e) {
156               // alert('s');
157                locate_me =  $('.lp-locate-me-btn');
158                main.find(".lp-locate-me-btn .error-span").addClass('hide');
159                plugin.getLocationPlugin();
160                setTimeout(function () {
161                    locate_me.text('Detect My Location')
162                }, 3000);
163            });
164            results.on("mouseover", "li", function () {
165                results.css('cursor', 'pointer');
166            });
167            results.on("click", "li", function () {
168                 $('.lp-search-location').css('display','none');
169                plugin.$element.find(".lp-cities").find(".d-bg-gray-light").removeClass("d-bg-gray-light");
170                $(this).addClass("d-bg-gray-light");
171                var city = $(this).data('val');
172                var text = $(this).text().trim();
173                text = text.replace(/<\/?span[^>]*>/g,"");
174                if (plugin.options.saveInCookie == true && city !=undefined) {
175                    window.Cookies.set('dul', btoa(city), 30);
176                }
177                if (typeof plugin.options.onManualSelect === 'function') {
178                    plugin.options.onManualSelect(city);
179                }
180                
181                if(city != undefined)
182                   saveInLocalStorage(city,text);
183                $('input[name="city"]').val(city);          
184                $("#locationModal").modal("hide");
185                plugin.getCities();
186                $('#headerPopularLocIn').css('display','block');
187                plugin.selectCity(city, text);
188
189            });
190            main.find(".lp-reset").on("click", function () {
191                plugin.$element.find(".lp-hidden-field").val("");
192                plugin.$element.find(".lp-cities").find(".d-bg-gray-light").removeClass("d-bg-gray-light");
193                if(!plugin.options.inline) {
194                    plugin.$element.find(".lp-dropdown").addClass("hidden");
195                    $(".overlay").addClass("hidden");
196                }
197                if (plugin.options.saveInCookie == true) {
198                    window.Cookies.set('dul', btoa(""), -1);
199                }
200                search_cities.val('').trigger('keyup');
201                if (typeof plugin.options.onManualSelect === 'function') {
202                    plugin.options.onManualSelect(null);
203                }
204                if (typeof plugin.options.onSelect === 'function') {
205                    return plugin.options.onSelect(null, plugin);
206                }
207                
208                
209            });
210            main.find(".lp-close").on("click", function() {
211                plugin.$element.find(".lp-dropdown").addClass("hidden");
212                $(".overlay").addClass("hidden");
213                $('#headerPopularLocIn').css('display','block');
214                plugin.getCities();
215            });
216            main.find(".lp-clear").on("click", function() {
217                $('.lp-search').val('');
218                $('.hp-select-city').val('');
219                 plugin.getCities();
220            });
221            main.find(".lp-clear-rto-old-city").on("click", function(id) {
222                $('.lp-clear-rto-old-city').val('');
223                plugin.getCities();
224            });
225            main.find(".lp-clear-rto-new-city").on("click", function(id) {
226                $('.lp-clear-rto-new-city').val('');
227                plugin.getCities();
228            });
229            $("body").on("click", "#headerPopularLocation li", function() {
230                $("#locationModal").find("ul.popular-cities li.selected").removeClass("selected");
231                $(this).addClass("selected");
232    
233                var locationValue = $(this).attr("value");
234                var locationText  =  $(this).data("original-value");
235
236                window.Cookies.set('dul', btoa(locationValue.toLowerCase()), 30);
237                $("#locationSearch").val(locationValue);
238                $("#locationSearch").addClass("value-added");
239                $(".header-location").find("label").text(locationValue);
240                $("#locationModal").modal("hide");
241                
242                $('#selected-location-filter-homepage').val(locationValue);
243                $('#selected-location-filter-homepage_buy').val(locationValue);
244                $('#selected-location-filter').val(locationValue);
245                $('input[name="city"]').val(locationValue);
246                saveInLocalStorage(locationValue.toLowerCase(),locationText);
247                plugin.$element.find(".lp-cities").find(".d-bg-gray-light").removeClass("d-bg-gray-light");
248                plugin.getCities();
249            });
250        },
251        unbindEvents: function () {
252            this.$element.off('.' + this._name);
253        },
254        someOtherFunction: function () {
255            alert('I promise to do something cool!');
256            this.callback();
257        },
258        callback: function () {
259            var onComplete = this.options.onComplete;
260            if (typeof onComplete === 'function') {
261                onComplete.call(this.element);
262            }
263        },
264        setCities: function (cities) {
265            this.options.cities = cities;
266            return true;
267        },
268        getCities: function () {
269            var plugin = this;
270            var cities = plugin.options.cities;
271            if (cities == null) {
272                if (!LS.exist('all_locations')) {
273                    $.get("https://cdn1.acedms.com/photos/data/cities_new.json", function (d) {
274                        if (d.code == 'success') {
275                                d.data.other= {...d.data.popular, ...d.data.other}; //d.data.popular+d.data.other;
276                                d.data.popular={};
277                                const sortedCitiesList=
278                                     Object.keys(d.data.other).sort().reduce(function (result, key) {
279                                      result[key] = d.data.other[key];
280                                      return result;
281                                    }, {});
282                                d.data.other= sortedCitiesList;    
283                            LS.set('all_locations', d.data, function (cities) {
284                                plugin.renderCities(cities);
285                            });
286                            plugin.$element.find(".lp-search").searchList(plugin.$element.find(".lp-cities"), 'data-val', function(str) {
287                            });
288                        } else {
289                            throw Error("Error while loading cities");
290                        }
291                    }, 'json');
292                    return this;
293                }
294               // $('#headerPopularLocIn').css('display','none');
295            }
296            cities = cities || LS.get('all_locations');
297            plugin.setCities(cities);
298            plugin.renderCities(cities);
299            return this;
300        },
301        renderCities: function (cities) {
302            var plugin = this;
303            var main = plugin.$element;
304            var cities_element = main.find(".lp-cities");
305            var selected = this.getSelected();
306            var getRecentCity = JSON.parse(localStorage.getItem('recent_cities')) || []
307            getRecentCity = getRecentCity.reverse();
308            var getRecentArray = [];
309            var html = '<ul class="lp-cities-ul  d-margin-bottom-5 d-font-weight-300 d-font-size-13 d-line-height-2">';
310            var has_populars = false;
311            if ( Object.keys(getRecentCity).length > 0) {
312                html += '<strong  class=" d-margin-left-10 d-display-block d-font-weight-500 d-font-size-16 d-margin-bottom-5 ">Recent Searches</strong>';
313                $.each( getRecentCity, function( key, value ) {
314               if(typeof(value.city)!='undefined')
315                {
316                        html += '<li data-type="recent" class="d-display-block d-cursor-pointer" data-val="' +value.matchCity + '"><img src="https://cdn1.acedms.com/photos/images/web/beta/images/location/location.svg" alt="location icon" width="15px" class="d-margin-right-10">'+getCitiesSpilit(value.city)+'</li>';
317                        getRecentArray.push(value.matchCity);
318                     }
319                });
320                has_populars = true;
321            }
322            if (typeof cities['popular'] !== 'undefined' && Object.keys(cities.popular).length > 0) {
323                html += '<strong class=" d-margin-left-10 d-display-block d-font-weight-500 d-font-size-16 d-margin-bottom-5 ">Popular Cities</strong>';
324                $.each( cities.popular, function( key, val ) {
325                  
326                if ($.inArray(key, getRecentArray) <0) {
327                    if (selected.toLowerCase() === val.toLowerCase()) {
328                        html += '<li data-type="popular" class="d-display-block d-bg-gray-light" data-val="' + key + '"><img src="https://cdn1.acedms.com/photos/images/web/beta/images/location/location.svg" alt="location icon" width="15px" class="d-margin-right-10">'+ getCitiesSpilit(val) +'</li>';
329                    } else {
330                        html += '<li data-type="popular" class="d-display-block d-cursor-pointer" data-val="' + key + '"><img src="https://cdn1.acedms.com/photos/images/web/beta/images/location/location.svg" alt="location icon" width="15px" class="d-margin-right-10">'+ getCitiesSpilit(val) +'</li>';
331                    }
332                }
333                });
334                has_populars = true;
335            }
336            // if (has_populars === true) {
337            //     html += '<strong class=" d-margin-left-10 d-display-block d-font-weight-500 d-font-size-16 d-margin-bottom-5 ">Other Cities</strong>';
338            // }
339            if (typeof cities['other'] !== 'undefined' && Object.keys(cities.other).length > 0) {
340                html += '<strong class=" d-margin-left-10 d-display-block d-font-weight-500 d-font-size-16 d-margin-bottom-5 ">All Cities</strong>';
341                $.each( cities.other, function( key, val ) {
342                if ($.inArray(key, getRecentArray) <0) {
343                   var val = key =='gurgoan'?'gurgoan/gurgram':val;
344                    if (selected.toLowerCase() === val.toLowerCase()) {
345                        html += '<li data-type="other" class="d-display-block d-bg-gray-light" data-val="' + key + '"><img src="https://cdn1.acedms.com/photos/images/web/beta/images/location/location.svg" alt="location icon" width="15px" class="d-margin-right-10">'+ getCitiesSpilit(val) +'</li>';
346                    } else {
347                        html += '<li data-type="other" class="d-display-block" data-val="' + key + '"><img src="https://cdn1.acedms.com/photos/images/web/beta/images/location/location.svg" alt="location icon" width="15px" class="d-margin-right-10">'+ getCitiesSpilit(val) +'</li>';
348                    }
349                }
350                });
351            }
352            html += '</ul>';
353            cities_element.html(html);
354        },
355        getSelected: function () {
356            var plugin = this;
357            var main = plugin.$element;
358            var city = null
359            if (main.data("value")) {
360                city = main.data("value");
361            } else if (plugin.options.selected !== null) {
362                city = plugin.options.selected;
363            } else {
364                var loc = window.Cookies.get('dul') || null;
365                city = loc ? atob(loc) : 'Location';
366            }
367          
368           // this.getLocationPlugin();
369            if(city=='Location')
370            { 
371               // this.getLocationPlugin();
372                
373            }
374            return city;
375        },
376        selectCity: function (city ,text) {
377            var plugin = this;
378            city = city || this.getSelected();
379            text = text || city;
380            if(plugin.options.showCityText){
381                text = plugin.$element.find(".lp-cities-ul").find('li[data-val="'+city.toLowerCase()+'"]').eq(0).text();
382            }
383            plugin.$element.find(".lp-hidden-field").val(city);
384            $(".header-location").find("label").html(city);
385            $('.location-plugin').find('input').val(city);
386            if(!plugin.options.inline) {
387                plugin.$element.find(".lp-dropdown").addClass("hidden");
388            }
389            $(".overlay").addClass("hidden");
390
391            if (typeof plugin.options.onSelect === 'function') {
392                $("#selected-location-filter").val(city);
393                return plugin.options.onSelect(city, plugin, text);
394            }
395            
396        },
397        buildHtml: function () {
398            var plugin = this;
399            var html = '<div class="lp-element ' + ($(plugin.element).attr("class") ? $(plugin.element).attr("class") : '') + '">';
400            html += $(plugin.element).html();
401            html += '</div>';
402            html += plugin.buildDropdown();
403            if (this.options.name !== null) {
404                html += '<input type="hidden" class="lp-hidden-field" name="' + this.options.name + '">';
405            }
406            $(plugin.element).removeClass().addClass("lp-main d-position-relative");
407            plugin.$element.html(html);
408        },
409        buildDropdown: function () {
410            var html = '';
411            html += '<div class="lp-dropdown d-bg-white d-border-1 d-border-gray-light d-width-100 d-bg-color-white '+(this.options.inline ? '' : 'hidden d-position-absolute')+'" style="z-index: 3;top: '+(this.$element.height() ? this.$element.height() : 34)+'px;">';
412            if (this.options.detectLocation == true) {
413                // html += '<div class="lp-detect-location d-padding-10">\n\
414                //             <div class="lp-after-location clickable">\n\
415                //                 <i class="ico web-g-icon-gps g-icon g-icon-gps d-text-blue"></i>\n\
416                //                 <span class="lp-locate-me-btn">Detect My Location</span>';
417                //                 html += '<a href="javascript:void(0)" class="pull-right float-right lp-close d-text-gray d-margin-left-10" role="button">Close</a>'
418                // html += '<a href="javascript:void(0)" class="pull-right lp-reset float-right" role="button">Reset</a>\n\
419                //             </div>\n\
420                //         </div>';
421            }
422            // if (this.options.detectLocation == true && this.options.search == true) {
423            //     html += '<div class="text-center">\n\
424            //                 <strong>OR</strong>\n\
425            //             </div>';
426            // }
427            // <a href="javascript:void(0)" class="pull-right lp-close  d-margin-left-10" style ="float:right" role="button">X</a>\n\
428            if (this.options.search == true) {
429                if (this.options.detectLocation == true) {
430                html += '<div class="lp-detect-location d-padding-left-10 d-padding-top-10">\n\
431                <div class="lp-after-location clickable  d-text-blue">\n\
432                    <i class="lcp lcp-gps d-font-size-20 d-margin-right-5"></i>\n\
433                    <span class="lp-locate-me-btn d-valign-top">Detect My City</span> </div>\n\
434                       </div>';
435                }
436                html += '<div class="lp-search-location d-padding-left-10 d-padding-right-10 d-margin-top-5" style="display:none">\n\
437                <div class="d-margin-bottom-0 text-end text-right">\n\
438                         <a href="javascript:void(0)" class="lp-clear d-text-gray d-margin-top-5 d-font-weight-500" role="button">&#x2715;</a>\n\
439                     </div>\n\
440                </div>';
441            }
442            html += '<style>a:hover{text-decoration:none;}a.lp-clear{position: absolute;right: 10px;top: 8px;}.lp-cities {cursor: pointer;background-color: #fff;overflow-y: auto;'+(!this.options.fullHeight ? 'max-height: 300px;' : '')+'min-height: inherit;} .lp-cities li{padding: 3px 10px;font-size: 14px; color:#666}.lp-search-location .form-group{margin-bottom: 10px}.overlay{background-color:rgba(255,255,255,0) !important}.lp-cities li img{display:inline-block; width: 13px}.lp-cities li span{font-weight:500;}.lp-cities li:hover{color:#3babff; font-weight:500}.lp-cities li:hover span{font-weight: 500}.lp-citiesdropdown{z-index: 5 !important}</style><div class="lp-cities"></div>';
443            html += '</div>';
444            return html;
445        },
446
447        getLocationPlugin: function () {
448            var plugin = this;
449            var main = plugin.$element;
450            var locate_me = $(".lp-locate-me-btn");
451            if (navigator.geolocation) {
452                locate_me.html("Detecting ...");
453                navigator.geolocation.getCurrentPosition(function (position) {
454                    var lat = position.coords.latitude;
455                    var long = position.coords.longitude;
456                    console.log(lat);
457                    console.log(long);
458                    $.ajax({
459                        type: "GET",
460                        url: base_url + "/locate_me?latitude=" + lat + "&longitude=" + long,
461                        dataType: 'json',
462                        success: function (d) {
463                            if (d.code == 'success') {
464                                if (plugin.options.saveInCookie == true) {
465                                    window.Cookies.set('dul', btoa(d.data), 30);
466                                }
467                                if (typeof plugin.options.onManualSelect === 'function') {
468                                    plugin.options.onManualSelect(d.data);
469                                }
470                                plugin.selectCity(d.data);
471                            }
472                        }
473                    });
474                }, function (error) {
475                    if (error.code == error.PERMISSION_DENIED) {
476                        locate_me.html("<span class='lp-locate-me-btn small'>Location Access Denied. Please go to Browser settings to give Access.</span>");
477                    } else {
478                        locate_me.html("<span class='lp-locate-me-btn small'>Unable to fetch Location, please select a location from below list.</span>");
479                    }
480                }
481                );
482            } else {
483                locate_me.html("<span class='lp-locate-me-btn small'>Unable to fetch Location, please select a location from below list.</span>")
484            }
485        },
486        setCity: function(city){
487            this.options.selected = city;
488            this.refresh();
489        },
490        refresh: function(){
491            this.init();
492        }
493    });
494
495    $.fn[pluginName] = function (options) {
496        return this.each(function () {
497            if (!$.data(this, 'plugin_' + pluginName)) {
498                $.data(this, 'plugin_' + pluginName,
499                        new Plugin(this, options));
500            }
501        });
502    };
503
504})(jQuery, window, 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.