PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/web/varien/form.js

https://gitlab.com/crazybutterfly815/magento2
JavaScript | 378 lines | 329 code | 35 blank | 14 comment | 96 complexity | 4400f4d5763943e5dcf8e514b42c7a9f MD5 | raw file
  1. /**
  2. * Copyright © 2016 Magento. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * Most likely it's depracated classes.
  7. * Not used anywhere among pages.
  8. */
  9. VarienForm = Class.create();
  10. VarienForm.prototype = {
  11. initialize: function(formId, firstFieldFocus){
  12. this.form = $(formId);
  13. if (!this.form) {
  14. return;
  15. }
  16. this.cache = $A();
  17. this.currLoader = false;
  18. this.currDataIndex = false;
  19. this.validator = new Validation(this.form);
  20. this.elementFocus = this.elementOnFocus.bindAsEventListener(this);
  21. this.elementBlur = this.elementOnBlur.bindAsEventListener(this);
  22. this.childLoader = this.onChangeChildLoad.bindAsEventListener(this);
  23. this.highlightClass = 'highlight';
  24. this.extraChildParams = '';
  25. this.firstFieldFocus= firstFieldFocus || false;
  26. this.bindElements();
  27. if(this.firstFieldFocus){
  28. try{
  29. Form.Element.focus(Form.findFirstElement(this.form))
  30. }
  31. catch(e){}
  32. }
  33. },
  34. submit : function(url){
  35. if(this.validator && this.validator.validate()){
  36. this.form.submit();
  37. }
  38. return false;
  39. },
  40. bindElements:function (){
  41. var elements = Form.getElements(this.form);
  42. for (var row in elements) {
  43. if (elements[row].id) {
  44. Event.observe(elements[row],'focus',this.elementFocus);
  45. Event.observe(elements[row],'blur',this.elementBlur);
  46. }
  47. }
  48. },
  49. elementOnFocus: function(event){
  50. var element = Event.findElement(event, 'fieldset');
  51. if(element){
  52. Element.addClassName(element, this.highlightClass);
  53. }
  54. },
  55. elementOnBlur: function(event){
  56. var element = Event.findElement(event, 'fieldset');
  57. if(element){
  58. Element.removeClassName(element, this.highlightClass);
  59. }
  60. },
  61. setElementsRelation: function(parent, child, dataUrl, first){
  62. if (parent=$(parent)) {
  63. // TODO: array of relation and caching
  64. if (!this.cache[parent.id]){
  65. this.cache[parent.id] = $A();
  66. this.cache[parent.id]['child'] = child;
  67. this.cache[parent.id]['dataUrl'] = dataUrl;
  68. this.cache[parent.id]['data'] = $A();
  69. this.cache[parent.id]['first'] = first || false;
  70. }
  71. Event.observe(parent,'change',this.childLoader);
  72. }
  73. },
  74. onChangeChildLoad: function(event){
  75. element = Event.element(event);
  76. this.elementChildLoad(element);
  77. },
  78. elementChildLoad: function(element, callback){
  79. this.callback = callback || false;
  80. if (element.value) {
  81. this.currLoader = element.id;
  82. this.currDataIndex = element.value;
  83. if (this.cache[element.id]['data'][element.value]) {
  84. this.setDataToChild(this.cache[element.id]['data'][element.value]);
  85. }
  86. else{
  87. new Ajax.Request(this.cache[this.currLoader]['dataUrl'],{
  88. method: 'post',
  89. parameters: {"parent":element.value},
  90. onComplete: this.reloadChildren.bind(this)
  91. });
  92. }
  93. }
  94. },
  95. reloadChildren: function(transport){
  96. var data = eval('(' + transport.responseText + ')');
  97. this.cache[this.currLoader]['data'][this.currDataIndex] = data;
  98. this.setDataToChild(data);
  99. },
  100. setDataToChild: function(data){
  101. if (data.length) {
  102. var child = $(this.cache[this.currLoader]['child']);
  103. if (child){
  104. var html = '<select name="'+child.name+'" id="'+child.id+'" class="'+child.className+'" title="'+child.title+'" '+this.extraChildParams+'>';
  105. if(this.cache[this.currLoader]['first']){
  106. html+= '<option value="">'+this.cache[this.currLoader]['first']+'</option>';
  107. }
  108. for (var i in data){
  109. if(data[i].value) {
  110. html+= '<option value="'+data[i].value+'"';
  111. if(child.value && (child.value == data[i].value || child.value == data[i].label)){
  112. html+= ' selected';
  113. }
  114. html+='>'+data[i].label+'</option>';
  115. }
  116. }
  117. html+= '</select>';
  118. Element.insert(child, {before: html});
  119. Element.remove(child);
  120. }
  121. }
  122. else{
  123. var child = $(this.cache[this.currLoader]['child']);
  124. if (child){
  125. var html = '<input type="text" name="'+child.name+'" id="'+child.id+'" class="'+child.className+'" title="'+child.title+'" '+this.extraChildParams+'>';
  126. Element.insert(child, {before: html});
  127. Element.remove(child);
  128. }
  129. }
  130. this.bindElements();
  131. if (this.callback) {
  132. this.callback();
  133. }
  134. }
  135. }
  136. RegionUpdater = Class.create();
  137. RegionUpdater.prototype = {
  138. initialize: function (countryEl, regionTextEl, regionSelectEl, regions, disableAction, zipEl)
  139. {
  140. this.countryEl = $(countryEl);
  141. this.regionTextEl = $(regionTextEl);
  142. this.regionSelectEl = $(regionSelectEl);
  143. this.zipEl = $(zipEl);
  144. this.config = regions['config'];
  145. delete regions.config;
  146. this.regions = regions;
  147. this.disableAction = (typeof disableAction=='undefined') ? 'hide' : disableAction;
  148. this.zipOptions = (typeof zipOptions=='undefined') ? false : zipOptions;
  149. if (this.regionSelectEl.options.length<=1) {
  150. this.update();
  151. }
  152. Event.observe(this.countryEl, 'change', this.update.bind(this));
  153. },
  154. _checkRegionRequired: function()
  155. {
  156. var label, wildCard;
  157. var elements = [this.regionTextEl, this.regionSelectEl];
  158. var that = this;
  159. if (typeof this.config == 'undefined') {
  160. return;
  161. }
  162. var regionRequired = this.config.regions_required.indexOf(this.countryEl.value) >= 0;
  163. elements.each(function(currentElement) {
  164. Validation.reset(currentElement);
  165. label = $$('label[for="' + currentElement.id + '"]')[0];
  166. if (label) {
  167. wildCard = label.down('em') || label.down('span.required');
  168. if (!that.config.show_all_regions) {
  169. if (regionRequired) {
  170. label.up().show();
  171. } else {
  172. label.up().hide();
  173. }
  174. }
  175. }
  176. if (label && wildCard) {
  177. if (!regionRequired) {
  178. wildCard.hide();
  179. if (label.hasClassName('required')) {
  180. label.removeClassName('required');
  181. }
  182. } else if (regionRequired) {
  183. wildCard.show();
  184. if (!label.hasClassName('required')) {
  185. label.addClassName('required')
  186. }
  187. }
  188. }
  189. if (!regionRequired) {
  190. if (currentElement.hasClassName('required-entry')) {
  191. currentElement.removeClassName('required-entry');
  192. }
  193. if ('select' == currentElement.tagName.toLowerCase() &&
  194. currentElement.hasClassName('validate-select')) {
  195. currentElement.removeClassName('validate-select');
  196. }
  197. } else {
  198. if (!currentElement.hasClassName('required-entry')) {
  199. currentElement.addClassName('required-entry');
  200. }
  201. if ('select' == currentElement.tagName.toLowerCase() &&
  202. !currentElement.hasClassName('validate-select')) {
  203. currentElement.addClassName('validate-select');
  204. }
  205. }
  206. });
  207. },
  208. update: function()
  209. {
  210. if (this.regions[this.countryEl.value]) {
  211. var i, option, region, def;
  212. def = this.regionSelectEl.getAttribute('defaultValue');
  213. if (this.regionTextEl) {
  214. if (!def) {
  215. def = this.regionTextEl.value.toLowerCase();
  216. }
  217. this.regionTextEl.value = '';
  218. }
  219. this.regionSelectEl.options.length = 1;
  220. for (regionId in this.regions[this.countryEl.value]) {
  221. region = this.regions[this.countryEl.value][regionId];
  222. option = document.createElement('OPTION');
  223. option.value = regionId;
  224. option.text = region.name.stripTags();
  225. option.title = region.name;
  226. if (this.regionSelectEl.options.add) {
  227. this.regionSelectEl.options.add(option);
  228. } else {
  229. this.regionSelectEl.appendChild(option);
  230. }
  231. if (regionId==def || (region.name && region.name.toLowerCase()==def) ||
  232. (region.name && region.code.toLowerCase()==def)
  233. ) {
  234. this.regionSelectEl.value = regionId;
  235. }
  236. }
  237. if (this.disableAction=='hide') {
  238. if (this.regionTextEl) {
  239. this.regionTextEl.style.display = 'none';
  240. }
  241. this.regionSelectEl.style.display = '';
  242. } else if (this.disableAction=='disable') {
  243. if (this.regionTextEl) {
  244. this.regionTextEl.disabled = true;
  245. }
  246. this.regionSelectEl.disabled = false;
  247. }
  248. this.setMarkDisplay(this.regionSelectEl, true);
  249. } else {
  250. this.regionSelectEl.options.length = 1;
  251. if (this.disableAction=='hide') {
  252. if (this.regionTextEl) {
  253. this.regionTextEl.style.display = '';
  254. }
  255. this.regionSelectEl.style.display = 'none';
  256. Validation.reset(this.regionSelectEl);
  257. } else if (this.disableAction=='disable') {
  258. if (this.regionTextEl) {
  259. this.regionTextEl.disabled = false;
  260. }
  261. this.regionSelectEl.disabled = true;
  262. } else if (this.disableAction=='nullify') {
  263. this.regionSelectEl.options.length = 1;
  264. this.regionSelectEl.value = '';
  265. this.regionSelectEl.selectedIndex = 0;
  266. this.lastCountryId = '';
  267. }
  268. this.setMarkDisplay(this.regionSelectEl, false);
  269. }
  270. this._checkRegionRequired();
  271. // Make Zip and its label required/optional
  272. var zipUpdater = new ZipUpdater(this.countryEl.value, this.zipEl);
  273. zipUpdater.update();
  274. },
  275. setMarkDisplay: function(elem, display){
  276. elem = $(elem);
  277. var labelElement = elem.up(0).down('label > span.required') ||
  278. elem.up(1).down('label > span.required') ||
  279. elem.up(0).down('label.required > em') ||
  280. elem.up(1).down('label.required > em');
  281. if(labelElement) {
  282. inputElement = labelElement.up().next('input');
  283. if (display) {
  284. labelElement.show();
  285. if (inputElement) {
  286. inputElement.addClassName('required-entry');
  287. }
  288. } else {
  289. labelElement.hide();
  290. if (inputElement) {
  291. inputElement.removeClassName('required-entry');
  292. }
  293. }
  294. }
  295. }
  296. }
  297. ZipUpdater = Class.create();
  298. ZipUpdater.prototype = {
  299. initialize: function(country, zipElement)
  300. {
  301. this.country = country;
  302. this.zipElement = $(zipElement);
  303. },
  304. update: function()
  305. {
  306. // Country ISO 2-letter codes must be pre-defined
  307. if (typeof optionalZipCountries == 'undefined') {
  308. return false;
  309. }
  310. // Ajax-request and normal content load compatibility
  311. if (this.zipElement != undefined) {
  312. this._setPostcodeOptional();
  313. } else {
  314. Event.observe(window, "load", this._setPostcodeOptional.bind(this));
  315. }
  316. },
  317. _setPostcodeOptional: function()
  318. {
  319. this.zipElement = $(this.zipElement);
  320. if (this.zipElement == undefined) {
  321. return false;
  322. }
  323. // find label
  324. var label = $$('label[for="' + this.zipElement.id + '"]')[0];
  325. if (label != undefined) {
  326. var wildCard = label.down('em') || label.down('span.required');
  327. }
  328. // Make Zip and its label required/optional
  329. if (optionalZipCountries.indexOf(this.country) != -1) {
  330. while (this.zipElement.hasClassName('required-entry')) {
  331. this.zipElement.removeClassName('required-entry');
  332. }
  333. if (wildCard != undefined) {
  334. wildCard.hide();
  335. }
  336. } else {
  337. this.zipElement.addClassName('required-entry');
  338. if (wildCard != undefined) {
  339. wildCard.show();
  340. }
  341. }
  342. }
  343. }