PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/themes/euvapor/euvapor/js/tools.js

https://gitlab.com/ptisky/API_prestashop
JavaScript | 251 lines | 187 code | 22 blank | 42 comment | 67 complexity | d689dbe159cdce563e57934628804f5e MD5 | raw file
  1. /*
  2. * 2007-2013 PrestaShop
  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.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 license@prestashop.com 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 PrestaShop to newer
  17. * versions in the future. If you wish to customize PrestaShop for your
  18. * needs please refer to http://www.prestashop.com for more information.
  19. *
  20. * @author PrestaShop SA <contact@prestashop.com>
  21. * @copyright 2007-2013 PrestaShop SA
  22. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  23. * International Registered Trademark & Property of PrestaShop SA
  24. */
  25. function ps_round(value, precision)
  26. {
  27. if (typeof(roundMode) === 'undefined')
  28. roundMode = 2;
  29. if (typeof(precision) === 'undefined')
  30. precision = 2;
  31. method = roundMode;
  32. if (method === 0)
  33. return ceilf(value, precision);
  34. else if (method === 1)
  35. return floorf(value, precision);
  36. precisionFactor = precision === 0 ? 1 : Math.pow(10, precision);
  37. return Math.round(value * precisionFactor) / precisionFactor;
  38. }
  39. function ceilf(value, precision)
  40. {
  41. if (typeof(precision) === 'undefined')
  42. precision = 0;
  43. precisionFactor = precision === 0 ? 1 : Math.pow(10, precision);
  44. tmp = value * precisionFactor;
  45. tmp2 = tmp.toString();
  46. if (tmp2[tmp2.length - 1] === 0)
  47. return value;
  48. return Math.ceil(value * precisionFactor) / precisionFactor;
  49. }
  50. function floorf(value, precision)
  51. {
  52. if (typeof(precision) === 'undefined')
  53. precision = 0;
  54. precisionFactor = precision === 0 ? 1 : Math.pow(10, precision);
  55. tmp = value * precisionFactor;
  56. tmp2 = tmp.toString();
  57. if (tmp2[tmp2.length - 1] === 0)
  58. return value;
  59. return Math.floor(value * precisionFactor) / precisionFactor;
  60. }
  61. function formatedNumberToFloat(price, currencyFormat, currencySign)
  62. {
  63. price = price.replace(currencySign, '');
  64. if (currencyFormat === 1)
  65. return parseFloat(price.replace(',', '').replace(' ', ''));
  66. else if (currencyFormat === 2)
  67. return parseFloat(price.replace(' ', '').replace(',', '.'));
  68. else if (currencyFormat === 3)
  69. return parseFloat(price.replace('.', '').replace(' ', '').replace(',', '.'));
  70. else if (currencyFormat === 4)
  71. return parseFloat(price.replace(',', '').replace(' ', ''));
  72. return price;
  73. }
  74. //return a formatted price
  75. function formatCurrency(price, currencyFormat, currencySign, currencyBlank)
  76. {
  77. // if you modified this function, don't forget to modify the PHP function displayPrice (in the Tools.php class)
  78. blank = '';
  79. price = parseFloat(price.toFixed(6));
  80. price = ps_round(price, priceDisplayPrecision);
  81. if (currencyBlank > 0)
  82. blank = ' ';
  83. if (currencyFormat == 1)
  84. return currencySign + blank + formatNumber(price, priceDisplayPrecision, ',', '.');
  85. if (currencyFormat == 2)
  86. return (formatNumber(price, priceDisplayPrecision, ' ', ',') + blank + currencySign);
  87. if (currencyFormat == 3)
  88. return (currencySign + blank + formatNumber(price, priceDisplayPrecision, '.', ','));
  89. if (currencyFormat == 4)
  90. return (formatNumber(price, priceDisplayPrecision, ',', '.') + blank + currencySign);
  91. if (currencyFormat == 5)
  92. return (currencySign + blank + formatNumber(price, priceDisplayPrecision, '\'', '.'));
  93. return price;
  94. }
  95. //return a formatted number
  96. function formatNumber(value, numberOfDecimal, thousenSeparator, virgule)
  97. {
  98. value = value.toFixed(numberOfDecimal);
  99. var val_string = value+'';
  100. var tmp = val_string.split('.');
  101. var abs_val_string = (tmp.length === 2) ? tmp[0] : val_string;
  102. var deci_string = ('0.' + (tmp.length === 2 ? tmp[1] : 0)).substr(2);
  103. var nb = abs_val_string.length;
  104. for (var i = 1 ; i < 4; i++)
  105. if (value >= Math.pow(10, (3 * i)))
  106. abs_val_string = abs_val_string.substring(0, nb - (3 * i)) + thousenSeparator + abs_val_string.substring(nb - (3 * i));
  107. if (parseInt(numberOfDecimal) === 0)
  108. return abs_val_string;
  109. return abs_val_string + virgule + (deci_string > 0 ? deci_string : '00');
  110. }
  111. //change the text of a jQuery element with a sliding effect (velocity could be a number in ms, 'slow' or 'fast', effect1 and effect2 could be slide, fade, hide, show)
  112. function updateTextWithEffect(jQueryElement, text, velocity, effect1, effect2, newClass)
  113. {
  114. if(jQueryElement.text() !== text)
  115. if(effect1 === 'fade')
  116. jQueryElement.fadeOut(velocity, function(){
  117. $(this).addClass(newClass);
  118. if(effect2 === 'fade') $(this).text(text).fadeIn(velocity);
  119. else if(effect2 === 'slide') $(this).text(text).slideDown(velocity);
  120. else if(effect2 === 'show') $(this).text(text).show(velocity, function(){});
  121. });
  122. else if(effect1 === 'slide')
  123. jQueryElement.slideUp(velocity, function(){
  124. $(this).addClass(newClass);
  125. if(effect2 === 'fade') $(this).text(text).fadeIn(velocity);
  126. else if(effect2 === 'slide') $(this).text(text).slideDown(velocity);
  127. else if(effect2 === 'show') $(this).text(text).show(velocity);
  128. });
  129. else if(effect1 === 'hide')
  130. jQueryElement.hide(velocity, function(){
  131. $(this).addClass(newClass);
  132. if(effect2 === 'fade') $(this).text(text).fadeIn(velocity);
  133. else if(effect2 === 'slide') $(this).text(text).slideDown(velocity);
  134. else if(effect2 === 'show') $(this).text(text).show(velocity);
  135. });
  136. }
  137. //show a JS debug
  138. function dbg(value)
  139. {
  140. var active = false;//true for active
  141. var firefox = true;//true if debug under firefox
  142. if (active)
  143. if (firefox)
  144. console.log(value);
  145. else
  146. alert(value);
  147. }
  148. /**
  149. * Function : print_r()
  150. * Arguments: The data - array,hash(associative array),object
  151. * The level - OPTIONAL
  152. * Returns : The textual representation of the array.
  153. * This function was inspired by the print_r function of PHP.
  154. * This will accept some data as the argument and return a
  155. * text that will be a more readable version of the
  156. * array/hash/object that is given.
  157. */
  158. function print_r(arr, level)
  159. {
  160. var dumped_text = "";
  161. if (!level)
  162. level = 0;
  163. //The padding given at the beginning of the line.
  164. var level_padding = "";
  165. for (var j = 0 ; j < level + 1; j++)
  166. level_padding += " ";
  167. if (typeof(arr) === 'object')
  168. { //Array/Hashes/Objects
  169. for (var item in arr)
  170. {
  171. var value = arr[item];
  172. if (typeof(value) === 'object') { //If it is an array,
  173. dumped_text += level_padding + "'" + item + "' ...\n";
  174. dumped_text += dump(value,level+1);
  175. }
  176. else
  177. {
  178. dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  179. }
  180. }
  181. }
  182. else
  183. { //Stings/Chars/Numbers etc.
  184. dumped_text = "===>" + arr + "<===("+typeof(arr)+")";
  185. }
  186. return dumped_text;
  187. }
  188. //verify if value is in the array
  189. function in_array(value, array)
  190. {
  191. for (var i in array)
  192. if (array[i] === value)
  193. return true;
  194. return false;
  195. }
  196. function resizeAddressesBox(nameBox)
  197. {
  198. maxHeight = 0;
  199. if (typeof(nameBox) === 'undefined')
  200. nameBox = '.address';
  201. $(nameBox).each(function()
  202. {
  203. $(this).css('height', 'auto');
  204. currentHeight = $(this).height();
  205. if (maxHeight < currentHeight)
  206. maxHeight = currentHeight;
  207. });
  208. $(nameBox).height(maxHeight);
  209. }
  210. $(document).ready(function() {
  211. $.fn.checkboxChange = function(fnChecked, fnUnchecked) {
  212. if ($(this).prop('checked') && fnChecked)
  213. fnChecked.call(this);
  214. else if(fnUnchecked)
  215. fnUnchecked.call(this);
  216. if (!$(this).attr('eventCheckboxChange'))
  217. {
  218. $(this).live('change', function() { $(this).checkboxChange(fnChecked, fnUnchecked); });
  219. $(this).attr('eventCheckboxChange', true);
  220. }
  221. };
  222. });
  223. // Use it to simulate target blank link
  224. $(function(){
  225. $('a.js-new-window').click(function(){
  226. window.open(this.href);
  227. return false;
  228. });
  229. });