tools.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. function setLocation(url) {
  6. window.location.href = url;
  7. }
  8. function setElementDisable(element, disable) {
  9. if ($(element)) {
  10. $(element).disabled = disable;
  11. }
  12. }
  13. function toggleParentVis(obj) {
  14. obj = $(obj).parentNode;
  15. if (obj.style.display == 'none') {
  16. obj.style.display = '';
  17. } else {
  18. obj.style.display = 'none';
  19. }
  20. }
  21. // to fix new app/design/adminhtml/default/default/template/widget/form/renderer/fieldset.phtml
  22. // with toggleParentVis
  23. function toggleFieldsetVis(obj) {
  24. id = obj;
  25. obj = $(obj);
  26. if (obj.style.display == 'none') {
  27. obj.style.display = '';
  28. } else {
  29. obj.style.display = 'none';
  30. }
  31. obj = obj.parentNode.childElements();
  32. for (var i = 0; i < obj.length; i++) {
  33. if (obj[i].id != undefined &&
  34. obj[i].id == id &&
  35. obj[i - 1].classNames() == 'entry-edit-head') {
  36. if (obj[i - 1].style.display == 'none') {
  37. obj[i - 1].style.display = '';
  38. } else {
  39. obj[i - 1].style.display = 'none';
  40. }
  41. }
  42. }
  43. }
  44. function toggleVis(obj) {
  45. obj = $(obj);
  46. if (obj.style.display == 'none') {
  47. obj.style.display = '';
  48. } else {
  49. obj.style.display = 'none';
  50. }
  51. }
  52. function imagePreview(element) {
  53. if ($(element)) {
  54. var win = window.open('', 'preview', 'width=400,height=400,resizable=1,scrollbars=1');
  55. win.document.open();
  56. win.document.write('<body style="padding:0;margin:0"><img src="' + $(element).src + '" id="image_preview"/></body>');
  57. win.document.close();
  58. Event.observe(win, 'load', function () {
  59. var img = win.document.getElementById('image_preview');
  60. win.resizeTo(img.width + 40, img.height + 80);
  61. });
  62. }
  63. }
  64. function checkByProductPriceType(elem) {
  65. if (elem.id == 'price_type') {
  66. this.productPriceType = elem.value;
  67. return false;
  68. }
  69. if (elem.id == 'price' && this.productPriceType == 0) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. function toggleSeveralValueElements(checkbox, containers, excludedElements, checked) {
  75. 'use strict';
  76. if (containers && checkbox) {
  77. if (Object.prototype.toString.call(containers) != '[object Array]') {
  78. containers = [containers];
  79. }
  80. containers.each(function (container) {
  81. toggleValueElements(checkbox, container, excludedElements, checked);
  82. });
  83. }
  84. }
  85. function toggleValueElements(checkbox, container, excludedElements, checked) {
  86. if (container && checkbox) {
  87. var ignoredElements = [checkbox];
  88. if (typeof excludedElements != 'undefined') {
  89. if (Object.prototype.toString.call(excludedElements) != '[object Array]') {
  90. excludedElements = [excludedElements];
  91. }
  92. for (var i = 0; i < excludedElements.length; i++) {
  93. ignoredElements.push(excludedElements[i]);
  94. }
  95. }
  96. //var elems = container.select('select', 'input');
  97. var elems = Element.select(container, ['select', 'input', 'textarea', 'button', 'img']).filter(function (el) {
  98. return el.readAttribute('type') != 'hidden';
  99. });
  100. var isDisabled = checked != undefined ? checked : checkbox.checked;
  101. elems.each(function (elem) {
  102. if (checkByProductPriceType(elem)) {
  103. var i = ignoredElements.length;
  104. while (i-- && elem != ignoredElements[i]);
  105. if (i != -1) {
  106. return;
  107. }
  108. elem.disabled = isDisabled;
  109. if (isDisabled) {
  110. elem.addClassName('disabled');
  111. } else {
  112. elem.removeClassName('disabled');
  113. }
  114. if (elem.nodeName.toLowerCase() == 'img') {
  115. isDisabled ? elem.hide() : elem.show();
  116. }
  117. }
  118. });
  119. }
  120. }
  121. /**
  122. * @todo add validation for fields
  123. */
  124. function submitAndReloadArea(area, url) {
  125. if ($(area)) {
  126. var fields = $(area).select('input', 'select', 'textarea');
  127. var data = Form.serializeElements(fields, true);
  128. url += url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true';
  129. new Ajax.Request(url, {
  130. parameters: $H(data),
  131. loaderArea: area,
  132. onSuccess: function (transport) {
  133. try {
  134. if (transport.responseText.isJSON()) {
  135. var response = transport.responseText.evalJSON();
  136. if (response.error) {
  137. alert(response.message);
  138. }
  139. if (response.ajaxExpired && response.ajaxRedirect) {
  140. setLocation(response.ajaxRedirect);
  141. }
  142. } else {
  143. $(area).update(transport.responseText);
  144. }
  145. }
  146. catch (e) {
  147. $(area).update(transport.responseText);
  148. }
  149. }
  150. });
  151. }
  152. }
  153. /********** MESSAGES ***********/
  154. function syncOnchangeValue(baseElem, distElem) {
  155. var compare = {
  156. baseElem: baseElem, distElem: distElem
  157. };
  158. Event.observe(baseElem, 'change', function () {
  159. if ($(this.baseElem) && $(this.distElem)) {
  160. $(this.distElem).value = $(this.baseElem).value;
  161. }
  162. }.bind(compare));
  163. }
  164. // Insert some content to the cursor position of input element
  165. function updateElementAtCursor(el, value, win) {
  166. if (win == undefined) {
  167. win = window.self;
  168. }
  169. if (document.selection) {
  170. el.focus();
  171. sel = win.document.selection.createRange();
  172. sel.text = value;
  173. } else if (el.selectionStart || el.selectionStart == '0') {
  174. var startPos = el.selectionStart;
  175. var endPos = el.selectionEnd;
  176. el.value = el.value.substring(0, startPos) + value + el.value.substring(endPos, el.value.length);
  177. } else {
  178. el.value += value;
  179. }
  180. }
  181. // Firebug detection
  182. function firebugEnabled() {
  183. if (window.console && window.console.firebug) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. function disableElement(elem) {
  189. elem.disabled = true;
  190. elem.addClassName('disabled');
  191. }
  192. function enableElement(elem) {
  193. elem.disabled = false;
  194. elem.removeClassName('disabled');
  195. }
  196. function disableElements(search) {
  197. $$('.' + search).each(disableElement);
  198. }
  199. function enableElements(search) {
  200. $$('.' + search).each(enableElement);
  201. }
  202. /** Cookie Reading And Writing **/
  203. var Cookie = {
  204. all: function () {
  205. var pairs = document.cookie.split(';');
  206. var cookies = {};
  207. pairs.each(function (item, index) {
  208. var pair = item.strip().split('=');
  209. cookies[unescape(pair[0])] = unescape(pair[1]);
  210. });
  211. return cookies;
  212. },
  213. read: function (cookieName) {
  214. var cookies = this.all();
  215. if (cookies[cookieName]) {
  216. return cookies[cookieName];
  217. }
  218. return null;
  219. },
  220. write: function (cookieName, cookieValue, cookieLifeTime) {
  221. var expires = '';
  222. if (cookieLifeTime) {
  223. var date = new Date();
  224. date.setTime(date.getTime() + cookieLifeTime * 1000);
  225. expires = '; expires=' + date.toUTCString();
  226. }
  227. var urlPath = '/' + BASE_URL.split('/').slice(3).join('/'); // Get relative path
  228. document.cookie = escape(cookieName) + '=' + escape(cookieValue) + expires + '; path=' + urlPath;
  229. },
  230. clear: function (cookieName) {
  231. this.write(cookieName, '', -1);
  232. }
  233. };
  234. var Fieldset = {
  235. cookiePrefix: 'fh-',
  236. applyCollapse: function (containerId) {
  237. //var collapsed = Cookie.read(this.cookiePrefix + containerId);
  238. //if (collapsed !== null) {
  239. // Cookie.clear(this.cookiePrefix + containerId);
  240. //}
  241. if ($(containerId + '-state')) {
  242. collapsed = $(containerId + '-state').value == 1 ? 0 : 1;
  243. } else {
  244. collapsed = $(containerId + '-head').collapsed;
  245. }
  246. if (collapsed == 1 || collapsed === undefined) {
  247. $(containerId + '-head').removeClassName('open');
  248. if ($(containerId + '-head').up('.section-config')) {
  249. $(containerId + '-head').up('.section-config').removeClassName('active');
  250. }
  251. $(containerId).hide();
  252. } else {
  253. $(containerId + '-head').addClassName('open');
  254. if ($(containerId + '-head').up('.section-config')) {
  255. $(containerId + '-head').up('.section-config').addClassName('active');
  256. }
  257. $(containerId).show();
  258. }
  259. },
  260. toggleCollapse: function (containerId, saveThroughAjax) {
  261. if ($(containerId + '-state')) {
  262. collapsed = $(containerId + '-state').value == 1 ? 0 : 1;
  263. } else {
  264. collapsed = $(containerId + '-head').collapsed;
  265. }
  266. //Cookie.read(this.cookiePrefix + containerId);
  267. if (collapsed == 1 || collapsed === undefined) {
  268. //Cookie.write(this.cookiePrefix + containerId, 0, 30*24*60*60);
  269. if ($(containerId + '-state')) {
  270. $(containerId + '-state').value = 1;
  271. }
  272. $(containerId + '-head').collapsed = 0;
  273. } else {
  274. //Cookie.clear(this.cookiePrefix + containerId);
  275. if ($(containerId + '-state')) {
  276. $(containerId + '-state').value = 0;
  277. }
  278. $(containerId + '-head').collapsed = 1;
  279. }
  280. this.applyCollapse(containerId);
  281. if (typeof saveThroughAjax != 'undefined') {
  282. this.saveState(saveThroughAjax, {
  283. container: containerId, value: $(containerId + '-state').value
  284. });
  285. }
  286. },
  287. addToPrefix: function (value) {
  288. this.cookiePrefix += value + '-';
  289. },
  290. saveState: function (url, parameters) {
  291. new Ajax.Request(url, {
  292. method: 'post',
  293. parameters: Object.toQueryString(parameters),
  294. loaderArea: false
  295. });
  296. }
  297. };
  298. var Base64 = {
  299. // private property
  300. _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  301. //'+/=', '-_,'
  302. // public method for encoding
  303. encode: function (input) {
  304. var output = '';
  305. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  306. var i = 0;
  307. input = Base64._utf8_encode(input);
  308. if (typeof window.btoa === 'function') {
  309. return window.btoa(input);
  310. }
  311. while (i < input.length) {
  312. chr1 = input.charCodeAt(i++);
  313. chr2 = input.charCodeAt(i++);
  314. chr3 = input.charCodeAt(i++);
  315. enc1 = chr1 >> 2;
  316. enc2 = (chr1 & 3) << 4 | chr2 >> 4;
  317. enc3 = (chr2 & 15) << 2 | chr3 >> 6;
  318. enc4 = chr3 & 63;
  319. if (isNaN(chr2)) {
  320. enc3 = enc4 = 64;
  321. } else if (isNaN(chr3)) {
  322. enc4 = 64;
  323. }
  324. output = output +
  325. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  326. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  327. }
  328. return output;
  329. },
  330. // public method for decoding
  331. decode: function (input) {
  332. var output = '';
  333. var chr1, chr2, chr3;
  334. var enc1, enc2, enc3, enc4;
  335. var i = 0;
  336. if (typeof window.atob === 'function') {
  337. return Base64._utf8_decode(window.atob(input));
  338. }
  339. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
  340. while (i < input.length) {
  341. enc1 = this._keyStr.indexOf(input.charAt(i++));
  342. enc2 = this._keyStr.indexOf(input.charAt(i++));
  343. enc3 = this._keyStr.indexOf(input.charAt(i++));
  344. enc4 = this._keyStr.indexOf(input.charAt(i++));
  345. chr1 = enc1 << 2 | enc2 >> 4;
  346. chr2 = (enc2 & 15) << 4 | enc3 >> 2;
  347. chr3 = (enc3 & 3) << 6 | enc4;
  348. output += String.fromCharCode(chr1);
  349. if (enc3 != 64) {
  350. output += String.fromCharCode(chr2);
  351. }
  352. if (enc4 != 64) {
  353. output += String.fromCharCode(chr3);
  354. }
  355. }
  356. return Base64._utf8_decode(output);
  357. },
  358. mageEncode: function (input) {
  359. return this.encode(input).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ',');
  360. },
  361. mageDecode: function (output) {
  362. output = output.replace(/\-/g, '+').replace(/_/g, '/').replace(/,/g, '=');
  363. return this.decode(output);
  364. },
  365. idEncode: function (input) {
  366. return this.encode(input).replace(/\+/g, ':').replace(/\//g, '_').replace(/=/g, '-');
  367. },
  368. idDecode: function (output) {
  369. output = output.replace(/\-/g, '=').replace(/_/g, '/').replace(/\:/g, '\+');
  370. return this.decode(output);
  371. },
  372. // private method for UTF-8 encoding
  373. _utf8_encode: function (string) {
  374. string = string.replace(/\r\n/g, '\n');
  375. var utftext = '';
  376. for (var n = 0; n < string.length; n++) {
  377. var c = string.charCodeAt(n);
  378. if (c < 128) {
  379. utftext += String.fromCharCode(c);
  380. } else if (c > 127 && c < 2048) {
  381. utftext += String.fromCharCode(c >> 6 | 192);
  382. utftext += String.fromCharCode(c & 63 | 128);
  383. } else {
  384. utftext += String.fromCharCode(c >> 12 | 224);
  385. utftext += String.fromCharCode(c >> 6 & 63 | 128);
  386. utftext += String.fromCharCode(c & 63 | 128);
  387. }
  388. }
  389. return utftext;
  390. },
  391. // private method for UTF-8 decoding
  392. _utf8_decode: function (utftext) {
  393. var string = '';
  394. var i = 0;
  395. var c = c1 = c2 = 0;
  396. while (i < utftext.length) {
  397. c = utftext.charCodeAt(i);
  398. if (c < 128) {
  399. string += String.fromCharCode(c);
  400. i++;
  401. } else if (c > 191 && c < 224) {
  402. c2 = utftext.charCodeAt(i + 1);
  403. string += String.fromCharCode((c & 31) << 6 | c2 & 63);
  404. i += 2;
  405. } else {
  406. c2 = utftext.charCodeAt(i + 1);
  407. c3 = utftext.charCodeAt(i + 2);
  408. string += String.fromCharCode((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  409. i += 3;
  410. }
  411. }
  412. return string;
  413. }
  414. };
  415. /**
  416. * Array functions
  417. */
  418. /**
  419. * Callback function for sort numeric values
  420. *
  421. * @param val1
  422. * @param val2
  423. */
  424. function sortNumeric(val1, val2) {
  425. return val1 - val2;
  426. }