PageSourceSearch

https://www.pageseditors.cat/js/varien/form.js

js pageseditors.cat collected 2026-10-01 12:07:44 UTC 14,272 bytes, 394 lines download raw bytes

1/**
2 * Magento
3 *
4 * NOTICE OF LICENSE
5 *
6 * This source file is subject to the Academic Free License (AFL 3.0)
7 * that is bundled with this package in the file LICENSE_AFL.txt.
8 * It is also available through the world-wide-web at this URL:
9 * http://opensource.org/licenses/afl-3.0.php
10 * If you did not receive a copy of the license and are unable to
11 * obtain it through the world-wide-web, please send an email
12 * to [email protected] so we can send you a copy immediately.
13 *
14 * DISCLAIMER
15 *
16 * Do not edit or add to this file if you wish to upgrade Magento to newer
17 * versions in the future. If you wish to customize Magento for your
18 * needs please refer to http://www.magento.com for more information.
19 *
20 * @category    Varien
21 * @package     js
22 * @copyright   Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
23 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
24 */
25VarienForm = Class.create();
26VarienForm.prototype = {
27    initialize: function(formId, firstFieldFocus){
28        this.form       = $(formId);
29        if (!this.form) {
30            return;
31        }
32        this.cache      = $A();
33        this.currLoader = false;
34        this.currDataIndex = false;
35        this.validator  = new Validation(this.form);
36        this.elementFocus   = this.elementOnFocus.bindAsEventListener(this);
37        this.elementBlur    = this.elementOnBlur.bindAsEventListener(this);
38        this.childLoader    = this.onChangeChildLoad.bindAsEventListener(this);
39        this.highlightClass = 'highlight';
40        this.extraChildParams = '';
41        this.firstFieldFocus= firstFieldFocus || false;
42        this.bindElements();
43        if(this.firstFieldFocus){
44            try{
45                Form.Element.focus(Form.findFirstElement(this.form))
46            }
47            catch(e){}
48        }
49    },
50
51    submit : function(url){
52        if(this.validator && this.validator.validate()){
53             this.form.submit();
54        }
55        return false;
56    },
57
58    bindElements:function (){
59        var elements = Form.getElements(this.form);
60        for (var row in elements) {
61            if (elements[row].id) {
62                Event.observe(elements[row],'focus',this.elementFocus);
63                Event.observe(elements[row],'blur',this.elementBlur);
64            }
65        }
66    },
67
68    elementOnFocus: function(event){
69        var element = Event.findElement(event, 'fieldset');
70        if(element){
71            Element.addClassName(element, this.highlightClass);
72        }
73    },
74
75    elementOnBlur: function(event){
76        var element = Event.findElement(event, 'fieldset');
77        if(element){
78            Element.removeClassName(element, this.highlightClass);
79        }
80    },
81
82    setElementsRelation: function(parent, child, dataUrl, first){
83        if (parent=$(parent)) {
84            // TODO: array of relation and caching
85            if (!this.cache[parent.id]){
86                this.cache[parent.id] = $A();
87                this.cache[parent.id]['child']     = child;
88                this.cache[parent.id]['dataUrl']   = dataUrl;
89                this.cache[parent.id]['data']      = $A();
90                this.cache[parent.id]['first']      = first || false;
91            }
92            Event.observe(parent,'change',this.childLoader);
93        }
94    },
95
96    onChangeChildLoad: function(event){
97        element = Event.element(event);
98        this.elementChildLoad(element);
99    },
100
101    elementChildLoad: function(element, callback){
102        this.callback = callback || false;
103        if (element.value) {
104            this.currLoader = element.id;
105            this.currDataIndex = element.value;
106            if (this.cache[element.id]['data'][element.value]) {
107                this.setDataToChild(this.cache[element.id]['data'][element.value]);
108            }
109            else{
110                new Ajax.Request(this.cache[this.currLoader]['dataUrl'],{
111                        method: 'post',
112                        parameters: {"parent":element.value},
113                        onComplete: this.reloadChildren.bind(this)
114                });
115            }
116        }
117    },
118
119    reloadChildren: function(transport){
120        var data = eval('(' + transport.responseText + ')');
121        this.cache[this.currLoader]['data'][this.currDataIndex] = data;
122        this.setDataToChild(data);
123    },
124
125    setDataToChild: function(data){
126        if (data.length) {
127            var child = $(this.cache[this.currLoader]['child']);
128            if (child){
129                var html = '<select name="'+child.name+'" id="'+child.id+'" class="'+child.className+'" title="'+child.title+'" '+this.extraChildParams+'>';
130                if(this.cache[this.currLoader]['first']){
131                    html+= '<option value="">'+this.cache[this.currLoader]['first']+'</option>';
132                }
133                for (var i in data){
134                    if(data[i].value) {
135                        html+= '<option value="'+data[i].value+'"';
136                        if(child.value && (child.value == data[i].value || child.value == data[i].label)){
137                            html+= ' selected';
138                        }
139                        html+='>'+data[i].label+'</option>';
140                    }
141                }
142                html+= '</select>';
143                Element.insert(child, {before: html});
144                Element.remove(child);
145            }
146        }
147        else{
148            var child = $(this.cache[this.currLoader]['child']);
149            if (child){
150                var html = '<input type="text" name="'+child.name+'" id="'+child.id+'" class="'+child.className+'" title="'+child.title+'" '+this.extraChildParams+'>';
151                Element.insert(child, {before: html});
152                Element.remove(child);
153            }
154        }
155
156        this.bindElements();
157        if (this.callback) {
158            this.callback();
159        }
160    }
161}
162
163RegionUpdater = Class.create();
164RegionUpdater.prototype = {
165    initialize: function (countryEl, regionTextEl, regionSelectEl, regions, disableAction, zipEl)
166    {
167        this.countryEl = $(countryEl);
168        this.regionTextEl = $(regionTextEl);
169        this.regionSelectEl = $(regionSelectEl);
170        this.zipEl = $(zipEl);
171        this.config = regions['config'];
172        delete regions.config;
173        this.regions = regions;
174
175        this.disableAction = (typeof disableAction=='undefined') ? 'hide' : disableAction;
176        this.zipOptions = (typeof zipOptions=='undefined') ? false : zipOptions;
177
178        if (this.regionSelectEl.options.length<=1) {
179            this.update();
180        }
181
182        Event.observe(this.countryEl, 'change', this.update.bind(this));
183    },
184
185    _checkRegionRequired: function()
186    {
187        var label, wildCard;
188        var elements = [this.regionTextEl, this.regionSelectEl];
189        var that = this;
190        if (typeof this.config == 'undefined') {
191            return;
192        }
193        var regionRequired = this.config.regions_required.indexOf(this.countryEl.value) >= 0;
194
195        elements.each(function(currentElement) {
196            Validation.reset(currentElement);
197            label = $$('label[for="' + currentElement.id + '"]')[0];
198            if (label) {
199                wildCard = label.down('em') || label.down('span.required');
200                if (!that.config.show_all_regions) {
201                    if (regionRequired) {
202                        label.up().show();
203                    } else {
204                        label.up().hide();
205                    }
206                }
207            }
208
209            if (label && wildCard) {
210                if (!regionRequired) {
211                    wildCard.hide();
212                    if (label.hasClassName('required')) {
213                        label.removeClassName('required');
214                    }
215                } else if (regionRequired) {
216                    wildCard.show();
217                    if (!label.hasClassName('required')) {
218                        label.addClassName('required')
219                    }
220                }
221            }
222
223            if (!regionRequired) {
224                if (currentElement.hasClassName('required-entry')) {
225                    currentElement.removeClassName('required-entry');
226                }
227                if ('select' == currentElement.tagName.toLowerCase() &&
228                    currentElement.hasClassName('validate-select')) {
229                    currentElement.removeClassName('validate-select');
230                }
231            } else {
232                if (!currentElement.hasClassName('required-entry')) {
233                    currentElement.addClassName('required-entry');
234                }
235                if ('select' == currentElement.tagName.toLowerCase() &&
236                    !currentElement.hasClassName('validate-select')) {
237                    currentElement.addClassName('validate-select');
238                }
239            }
240        });
241    },
242
243    update: function()
244    {
245        if (this.regions[this.countryEl.value]) {
246            var i, option, region, def;
247
248            def = this.regionSelectEl.getAttribute('defaultValue');
249            if (this.regionTextEl) {
250                if (!def) {
251                    def = this.regionTextEl.value.toLowerCase();
252                }
253                this.regionTextEl.value = '';
254            }
255
256            this.regionSelectEl.options.length = 1;
257            for (regionId in this.regions[this.countryEl.value]) {
258                region = this.regions[this.countryEl.value][regionId];
259
260                option = document.createElement('OPTION');
261                option.value = regionId;
262                option.text = region.name.stripTags();
263                option.title = region.name;
264
265                if (this.regionSelectEl.options.add) {
266                    this.regionSelectEl.options.add(option);
267                } else {
268                    this.regionSelectEl.appendChild(option);
269                }
270
271                if (regionId==def || (region.name && region.name.toLowerCase()==def) ||
272                    (region.name && region.code.toLowerCase()==def)
273                ) {
274                    this.regionSelectEl.value = regionId;
275                }
276            }
277
278            if (this.disableAction=='hide') {
279                if (this.regionTextEl) {
280                    this.regionTextEl.style.display = 'none';
281                }
282
283                this.regionSelectEl.style.display = '';
284            } else if (this.disableAction=='disable') {
285                if (this.regionTextEl) {
286                    this.regionTextEl.disabled = true;
287                }
288                this.regionSelectEl.disabled = false;
289            }
290            this.setMarkDisplay(this.regionSelectEl, true);
291        } else {
292            this.regionSelectEl.options.length = 1;
293            if (this.disableAction=='hide') {
294                if (this.regionTextEl) {
295                    this.regionTextEl.style.display = '';
296                }
297                this.regionSelectEl.style.display = 'none';
298                Validation.reset(this.regionSelectEl);
299            } else if (this.disableAction=='disable') {
300                if (this.regionTextEl) {
301                    this.regionTextEl.disabled = false;
302                }
303                this.regionSelectEl.disabled = true;
304            } else if (this.disableAction=='nullify') {
305                this.regionSelectEl.options.length = 1;
306                this.regionSelectEl.value = '';
307                this.regionSelectEl.selectedIndex = 0;
308                this.lastCountryId = '';
309            }
310            this.setMarkDisplay(this.regionSelectEl, false);
311        }
312
313        this._checkRegionRequired();
314        // Make Zip and its label required/optional
315        var zipUpdater = new ZipUpdater(this.countryEl.value, this.zipEl);
316        zipUpdater.update();
317    },
318
319    setMarkDisplay: function(elem, display){
320        elem = $(elem);
321        var labelElement = elem.up(0).down('label > span.required') ||
322                           elem.up(1).down('label > span.required') ||
323                           elem.up(0).down('label.required > em') ||
324                           elem.up(1).down('label.required > em');
325        if(labelElement) {
326            inputElement = labelElement.up().next('input');
327            if (display) {
328                labelElement.show();
329                if (inputElement) {
330                    inputElement.addClassName('required-entry');
331                }
332            } else {
333                labelElement.hide();
334                if (inputElement) {
335                    inputElement.removeClassName('required-entry');
336                }
337            }
338        }
339    }
340}
341
342ZipUpdater = Class.create();
343ZipUpdater.prototype = {
344    initialize: function(country, zipElement)
345    {
346        this.country = country;
347        this.zipElement = $(zipElement);
348    },
349
350    update: function()
351    {
352        // Country ISO 2-letter codes must be pre-defined
353        if (typeof optionalZipCountries == 'undefined') {
354            return false;
355        }
356
357        // Ajax-request and normal content load compatibility
358        if (this.zipElement != undefined) {
359            Validation.reset(this.zipElement)
360            this._setPostcodeOptional();
361        } else {
362            Event.observe(window, "load", this._setPostcodeOptional.bind(this));
363        }
364    },
365
366    _setPostcodeOptional: function()
367    {
368        this.zipElement = $(this.zipElement);
369        if (this.zipElement == undefined) {
370            return false;
371        }
372
373        // find label
374        var label = $$('label[for="' + this.zipElement.id + '"]')[0];
375        if (label != undefined) {
376            var wildCard = label.down('em') || label.down('span.required');
377        }
378
379        // Make Zip and its label required/optional
380        if (optionalZipCountries.indexOf(this.country) != -1) {
381            while (this.zipElement.hasClassName('required-entry')) {
382                this.zipElement.removeClassName('required-entry');
383            }
384            if (wildCard != undefined) {
385                wildCard.hide();
386            }
387        } else {
388            this.zipElement.addClassName('required-entry');
389            if (wildCard != undefined) {
390                wildCard.show();
391            }
392        }
393    }
394}

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.