PageSourceSearch

https://daheshheritage.org/js/varien/form.js

js daheshheritage.org collected 2026-10-04 14:58:27 UTC 11,714 bytes, 328 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.magentocommerce.com for more information.
19 *
20 * @category    Varien
21 * @package     js
22 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.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.regions = regions;
172
173        this.disableAction = (typeof disableAction=='undefined') ? 'hide' : disableAction;
174        this.zipOptions = (typeof zipOptions=='undefined') ? false : zipOptions;
175
176        if (this.regionSelectEl.options.length<=1) {
177            this.update();
178        }
179
180        Event.observe(this.countryEl, 'change', this.update.bind(this));
181    },
182
183    update: function()
184    {
185        if (this.regions[this.countryEl.value]) {
186            var i, option, region, def;
187
188            if (this.regionTextEl) {
189                def = this.regionTextEl.value.toLowerCase();
190                this.regionTextEl.value = '';
191            }
192            if (!def) {
193                def = this.regionSelectEl.getAttribute('defaultValue');
194            }
195
196            this.regionSelectEl.options.length = 1;
197            for (regionId in this.regions[this.countryEl.value]) {
198                region = this.regions[this.countryEl.value][regionId];
199
200                option = document.createElement('OPTION');
201                option.value = regionId;
202                option.text = region.name;
203
204                if (this.regionSelectEl.options.add) {
205                    this.regionSelectEl.options.add(option);
206                } else {
207                    this.regionSelectEl.appendChild(option);
208                }
209
210                if (regionId==def || region.name.toLowerCase()==def || region.code.toLowerCase()==def) {
211                    this.regionSelectEl.value = regionId;
212                }
213            }
214
215            if (this.disableAction=='hide') {
216                if (this.regionTextEl) {
217                    this.regionTextEl.style.display = 'none';
218                }
219
220                this.regionSelectEl.style.display = '';
221            } else if (this.disableAction=='disable') {
222                if (this.regionTextEl) {
223                    this.regionTextEl.disabled = true;
224                }
225                this.regionSelectEl.disabled = false;
226            }
227            this.setMarkDisplay(this.regionSelectEl, true);
228        } else {
229            if (this.disableAction=='hide') {
230                if (this.regionTextEl) {
231                    this.regionTextEl.style.display = '';
232                }
233                this.regionSelectEl.style.display = 'none';
234                Validation.reset(this.regionSelectEl);
235            } else if (this.disableAction=='disable') {
236                if (this.regionTextEl) {
237                    this.regionTextEl.disabled = false;
238                }
239                this.regionSelectEl.disabled = true;
240            } else if (this.disableAction=='nullify') {
241                this.regionSelectEl.options.length = 1;
242                this.regionSelectEl.value = '';
243                this.regionSelectEl.selectedIndex = 0;
244                this.lastCountryId = '';
245            }
246            this.setMarkDisplay(this.regionSelectEl, false);
247        }
248
249        // Make Zip and its label required/optional
250        var zipUpdater = new ZipUpdater(this.countryEl.value, this.zipEl);
251        zipUpdater.update();
252    },
253
254    setMarkDisplay: function(elem, display){
255        elem = $(elem);
256        var labelElement = elem.up(0).down('label > span.required') ||
257                           elem.up(1).down('label > span.required') ||
258                           elem.up(0).down('label.required > em') ||
259                           elem.up(1).down('label.required > em');
260        if(labelElement) {
261            inputElement = labelElement.up().next('input');
262            if (display) {
263                labelElement.show();
264                if (inputElement) {
265                    inputElement.addClassName('required-entry');
266                }
267            } else {
268                labelElement.hide();
269                if (inputElement) {
270                    inputElement.removeClassName('required-entry');
271                }
272            }
273        }
274    }
275}
276
277ZipUpdater = Class.create();
278ZipUpdater.prototype = {
279    initialize: function(country, zipElement)
280    {
281        this.country = country;
282        this.zipElement = $(zipElement);
283    },
284
285    update: function()
286    {
287        // Country ISO 2-letter codes must be pre-defined
288        if (typeof optionalZipCountries == 'undefined') {
289            return false;
290        }
291
292        // Ajax-request and normal content load compatibility
293        if (this.zipElement != undefined) {
294            this._setPostcodeOptional();
295        } else {
296            Event.observe(window, "load", this._setPostcodeOptional.bind(this));
297        }
298    },
299
300    _setPostcodeOptional: function()
301    {
302        this.zipElement = $(this.zipElement);
303        if (this.zipElement == undefined) {
304            return false;
305        }
306
307        // find label
308        var label = $$('label[for="' + this.zipElement.id + '"]')[0];
309        if (label != undefined) {
310            var wildCard = label.down('em') || label.down('span.required');
311        }
312
313        // Make Zip and its label required/optional
314        if (optionalZipCountries.indexOf(this.country) != -1) {
315            while (this.zipElement.hasClassName('required-entry')) {
316                this.zipElement.removeClassName('required-entry');
317            }
318            if (wildCard != undefined) {
319                wildCard.hide();
320            }
321        } else {
322            this.zipElement.addClassName('required-entry');
323            if (wildCard != undefined) {
324                wildCard.show();
325            }
326        }
327    }
328}

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.