PageSourceSearch

https://krymwood.ru/catalog/view/javascript/jquery/jquery.geoip-module.js

js krymwood.ru collected 2026-10-08 14:17:27 UTC 14,217 bytes, 407 lines download raw bytes

1(function($) {
2
3    var GeoIPModuleDialog = function(o, el) {
4        this.o = $.extend({autoOpen: false}, o);
5        this.el = el;
6        this.prefix = 'geoip-';
7
8        this.modal = $('<div class="' + this.prefix + 'modal ' + this.prefix + 'fade"></div>');
9        this.backdrop = $('<div class="' + this.prefix + 'backdrop ' + this.prefix + 'fade"></div>').height($(window).height());
10        this.dialog = $('<div class="' + this.prefix + 'modal-dialog"></div>');
11        this.content = $('<div class="' + this.prefix + 'modal-content"></div>');
12        var close = $('<div class="' + this.prefix + 'close">&times;</div>');
13        this.content.append(close);
14        $('body').append(this.modal.append(this.backdrop, this.dialog.append(this.content.append(el))));
15
16        var self = this;
17
18        $(window).resize(function() {
19            self.backdrop.height($(window).height());
20        });
21
22        if (this.o.autoOpen) {
23            this.open();
24        } else {
25            this.close();
26        }
27
28        close.click(function() {
29            self.close();
30        });
31
32        this.backdrop.click(function() {
33            self.close();
34        });
35
36        $(document).keydown(function(e) {
37            if (e.which == 27) {
38                self.close();
39            }
40        });
41    };
42
43    GeoIPModuleDialog.prototype.open = function() {
44        $('body').addClass(this.prefix + 'modal-open');
45        this.modal.addClass(this.prefix + 'in').show();
46        this.backdrop.addClass(this.prefix + 'in');
47
48        if (this.o.open) {
49            this.o.open.apply(this);
50        }
51    };
52
53    GeoIPModuleDialog.prototype.close = function() {
54        $('body').removeClass(this.prefix + 'modal-open');
55        this.modal.removeClass(this.prefix + 'in').hide();
56        this.backdrop.removeClass(this.prefix + 'in');
57    };
58
59    var methods = {
60        init: function(o) {
61            return this.each(function() {
62                var self = $(this),
63                    data = self.data('GeoIPModuleDialog');
64
65                if (!data) {
66                    self.data('GeoIPModuleDialog', {target: self, obj: new GeoIPModuleDialog(o, self)});
67                }
68            });
69        },
70        open: function() {
71            $(this).data('GeoIPModuleDialog').obj.open();
72        },
73        close: function() {
74            $(this).data('GeoIPModuleDialog').obj.close();
75        }
76    };
77
78    $.fn.geoipModuleDialog = function(method) {
79
80        if (methods[method]) {
81            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
82        } else if (typeof method === 'object' || !method) {
83            return methods.init.apply(this, arguments);
84        } else {
85            $.error('Method ' + method + ' does not exist!');
86        }
87
88        return false;
89    }
90})(jQuery);
91
92(function($) {
93    var DialogCustom = {
94        createConfirm: function(self) {
95            self.confirmBlock.addClass('geoip-custom-popup');
96
97            self.confirmBlock.append('<div class="geoip-confirm-buttons"><input type="button" class="geoip-confirm-yes" value="' + self.lang['btnYes'] + '">'
98            + ' <input type="button" class="geoip-confirm-no" value="' + self.lang['btnNo'] + '"> </div>');
99
100            self.confirmBlock.find('.geoip-confirm-yes').click(function() {
101                self.confirmBlock.hide();
102                if (self.o.confirmRedirect) {
103                    location.href = self.o.confirmRedirect;
104                }
105            });
106
107            self.confirmBlock.find('.geoip-confirm-no').click(function() {
108                self.confirmBlock.hide();
109                DialogCustom.openChoose(self);
110            });
111        },
112        createChoose:  function(self) {
113            self.chooseBlock.addClass('geoip-custom-popup');
114            self.chooseBlock.geoipModuleDialog();
115        },
116        openChoose:    function(self) {
117            if (self.citiesLoaded) {
118                self.chooseBlock.geoipModuleDialog('open');
119            } else {
120                self.loadCities(function() {
121                    self.chooseBlock.geoipModuleDialog('open');
122                });
123            }
124        },
125        closeChoose:   function(self) {
126           self.chooseBlock.geoipModuleDialog('close');
127        }
128    };
129
130    var GeoIPModule = function(o, el) {
131        this.o = $.extend({useAjax: false, confirmRegion: false, dialogView: 'custom', httpServer: location.host, lang: {}}, o);
132        this.lang = this.o.lang;
133        this.http_host = location.protocol + '//' + this.o.httpServer + '/';
134        this.el = el;
135        this.citiesLoaded = false;
136        var self = this;
137        var dialogs = DialogCustom;
138
139        el.addClass('geoip-module').append('<div class="geoip-text"></div>');
140
141        if (this.o.useAjax) {
142            $.get(this.http_host + 'index.php?route=module/geoip/getCity',
143                function(json) {
144                    el.find('.geoip-text').html(self.lang['yourZone'] + ' <span class="zone">' + json.zone + '</span>');
145                },
146                'json'
147            );
148        } else {
149            el.find('.geoip-text').html(self.lang['yourZone'] + ' <span class="zone">' + self.lang['zoneName'] + '</span>');
150        }
151
152        this.chooseBlock = $('<div class="geoip-choose-region"></div>');
153        el.after(this.chooseBlock);
154        dialogs.createChoose(this);
155
156        this.chooseBlock.on('click', '.choose-city', function() {
157            self.setRegion($(this).attr('data-id'));
158            dialogs.closeChoose(self);
159            return false;
160        });
161
162        el.on('click', '.geoip-text', function() {
163            dialogs.openChoose(self);
164        });
165
166        if (this.o.confirmRegion) {
167            this.confirmBlock = $('<div class="geoip-confirm-region">' + this.lang['confirmRegion'] + '</div>');
168            el.append(this.confirmBlock);
169            dialogs.createConfirm(self);
170        }
171
172        this.setRules();
173    };
174
175    GeoIPModule.prototype.loadCities = function(callback) {
176        var self = this;
177        if (!this.citiesLoaded) {
178            $.ajax({
179                url:      self.http_host + 'index.php?route=module/geoip/getList',
180                dataType: 'html',
181                success:  function(html) {
182                    self.chooseBlock.html(html);
183                    var input = self.chooseBlock.find('.geoip-popup-input');
184                    self.autocomplete(input, self.chooseBlock.find('.geoip-block'));
185                    input.siblings('ul.dropdown-menu').css({'maxHeight': 300, 'overflowY': 'auto', 'overflowX': 'hidden'});
186                    input.focus();
187                    self.citiesLoaded = true;
188                    callback.apply();
189                }
190            });
191        }
192    };
193
194    GeoIPModule.prototype.autocomplete = function(el, appendTo) {
195        var xhr = false;
196        var self = this;
197
198        el.geoipAutocomplete({
199            'source': function(request, response) {
200                if (xhr) {
201                    xhr.abort();
202                }
203
204                request = $.trim(request);
205                if (request && request.length > 2) {
206                    xhr = $.get(self.http_host + 'index.php?route=module/geoip/search&term=' + encodeURIComponent(request),
207                        function(json) {
208                            response(json);
209                        }, 'json');
210                }
211                else {
212                    response([]);
213                }
214            },
215            'select': function(item) {
216                el.val(item.name);
217                self.setRegion(item.value);
218            }
219        });
220    };
221
222    GeoIPModule.prototype.setRegion = function(id) {
223        $.get(this.http_host + 'index.php?route=module/geoip/save&fias_id=' + id,
224            function(json) {
225                if (json.success) {
226                    location.reload();
227                }
228            },
229            'json'
230        );
231    };
232
233    GeoIPModule.prototype.setRules = function() {
234        if (this.o.useAjax) {
235            $.get(this.http_host + 'index.php?route=module/geoip/getRules', function(json) {
236                if (json.rules) {
237                    $('.geoip-rule').each(function() {
238                        if (json.rules[$(this).attr('data-key')]) {
239                            $(this).after(json.rules[$(this).attr('data-key')]);
240                        } else if ($(this).attr('data-default')) {
241                            $(this).after($(this).attr('data-default'));
242                        }
243                        $(this).remove();
244                    });
245                } else {
246                    $('.geoip-rule').each(function() {
247                        if ($(this).attr('data-default')) {
248                            $(this).after($(this).attr('data-default'));
249                        }
250                        $(this).remove();
251                    });
252                }
253            }, 'json');
254        }
255    };
256
257    var methods = {
258        init: function(o) {
259            return this.each(function() {
260                var self = $(this),
261                    data = self.data('GeoIPModule');
262
263                if (!data) {
264                    self.data('GeoIPModule', {target: self, obj: new GeoIPModule(o, self)});
265                }
266            });
267        }
268    };
269
270    $.fn.geoipModule = function(method) {
271
272        if (methods[method]) {
273            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
274        } else if (typeof method === 'object' || !method) {
275            return methods.init.apply(this, arguments);
276        } else {
277            $.error('Method ' + method + ' does not exist!');
278        }
279
280        return false;
281    }
282})(jQuery);
283
284(function($) {
285    $.fn.geoipAutocomplete = function(option) {
286        return this.each(function() {
287            this.timer = null;
288            this.items = [];
289
290            $.extend(this, option);
291
292            $(this).attr('autocomplete', 'off');
293
294            // Focus
295            $(this).on('focus', function() {
296                this.request();
297            });
298
299            // Blur
300            $(this).on('blur', function() {
301                setTimeout(function(object) {
302                    object.hide();
303                }, 200, this);
304            });
305
306            // Keydown
307            $(this).on('keydown', function(event) {
308                switch (event.keyCode) {
309                    case 27: // escape
310                        this.hide();
311                        break;
312                    default:
313                        this.request();
314                        break;
315                }
316            });
317
318            // Click
319            this.click = function(event) {
320                event.preventDefault();
321
322                value = $(event.target).parent().attr('data-value');
323
324                if (value && this.items[value]) {
325                    this.select(this.items[value]);
326                }
327            };
328
329            // Show
330            this.show = function() {
331                var pos = $(this).position();
332
333                $(this).siblings('ul.dropdown-menu').css({
334                    top: pos.top + $(this).outerHeight(),
335                    left: pos.left
336                });
337
338                $(this).siblings('ul.dropdown-menu').show();
339            };
340
341            // Hide
342            this.hide = function() {
343                $(this).siblings('ul.dropdown-menu').hide();
344            };
345
346            // Request
347            this.request = function() {
348                clearTimeout(this.timer);
349
350                this.timer = setTimeout(function(object) {
351                    object.source($(object).val(), $.proxy(object.response, object));
352                }, 200, this);
353            };
354
355            // Response
356            this.response = function(json) {
357                html = '';
358
359                if (json.length) {
360                    for (i = 0; i < json.length; i++) {
361                        this.items[json[i]['value']] = json[i];
362                    }
363
364                    for (i = 0; i < json.length; i++) {
365                        if (!json[i]['category']) {
366                            html += '<li data-value="' + json[i]['value'] + '"><a href="#">
366' + json[i]['label'] + '</a></li>';
367                        }
368                    }
369
370                    // Get all the ones with a categories
371                    var category = [];
372
373                    for (i = 0; i < json.length; i++) {
374                        if (json[i]['category']) {
375                            if (!category[json[i]['category']]) {
376                                category[json[i]['category']] = [];
377                                category[json[i]['category']]['name'] = json[i]['category'];
378                                category[json[i]['category']]['item'] = [];
379                            }
380
381                            category[json[i]['category']]['item'].push(json[i]);
382                        }
383                    }
384
385                    for (i in category) {
386                        html += '<li class="dropdown-header">' + category[i]['name'] + '</li>';
387
388                        for (j = 0; j < category[i]['item'].length; j++) {
389                            html += '<li data-value="' + category[i]['item'][j]['value'] + '"><a href="#">&nbsp;&nbsp;&nbsp;' + category[i]['item'][j]['label'] + '</a></li>';
390                        }
391                    }
392                }
393
394                if (html) {
395                    this.show();
396                } else {
397                    this.hide();
398                }
399
400                $(this).siblings('ul.dropdown-menu').html(html);
401            };
402
403            $(this).after('<ul class="dropdown-menu"></ul>');
404            $(this).siblings('ul.dropdown-menu').delegate('a', 'click', $.proxy(this.click, this));
405        });
406    }
407})(jQuery);

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.