decorate.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /* eslint-disable strict */
  6. (function (factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. define([
  9. 'jquery',
  10. 'mage/translate'
  11. ], factory);
  12. } else {
  13. factory(jQuery);
  14. }
  15. }(function ($) {
  16. var methods = {
  17. /**
  18. * Decorate a list (e.g. a <ul> containing <li>) recursively if specified.
  19. * @param {Boolean} isRecursive
  20. */
  21. list: function (isRecursive) {
  22. return this.each(function () {
  23. var list = $(this),
  24. items;
  25. if (list.length > 0) {
  26. items = typeof isRecursive === undefined || isRecursive ?
  27. list.find('li') :
  28. list.children();
  29. items.decorate('generic', ['odd', 'even', 'last']);
  30. }
  31. });
  32. },
  33. /**
  34. * Annotate a set of DOM elements with decorator classes.
  35. * @param {Array} decoratorParams
  36. */
  37. generic: function (decoratorParams) {
  38. var elements = $(this),
  39. allSupportedParams;
  40. if (elements) {
  41. allSupportedParams = {
  42. even: 'odd', // Flip jQuery odd/even so that index 0 is odd.
  43. odd: 'even',
  44. last: 'last',
  45. first: 'first'
  46. };
  47. decoratorParams = decoratorParams || allSupportedParams;
  48. $.each(decoratorParams, function (index, param) {
  49. if (param === 'even' || param === 'odd') {
  50. elements.filter(':' + param).removeClass('odd even').addClass(allSupportedParams[param]);
  51. } else {
  52. elements.filter(':' + param).addClass(allSupportedParams[param]);
  53. }
  54. });
  55. }
  56. return this;
  57. },
  58. /**
  59. * Decorate DOM elements in an HTML table with specified classes.
  60. * @param {Object} instanceOptions
  61. */
  62. table: function (instanceOptions) {
  63. return this.each(function () {
  64. var table = $(this),
  65. options;
  66. if (table.length > 0) {
  67. options = {
  68. 'tbody': false,
  69. 'tbody tr': ['odd', 'even', 'first', 'last'],
  70. 'thead tr': ['first', 'last'],
  71. 'tfoot tr': ['first', 'last'],
  72. 'tr td': ['last']
  73. };
  74. $.extend(options, instanceOptions || {});
  75. $.each(options, function (key, value) {
  76. if (options[key]) {
  77. if (key === 'tr td') {
  78. $.each(table.find('tr'), function () {
  79. $(this).find('td').decorate('generic', options['tr td']);
  80. });
  81. } else {
  82. table.find(key).decorate('generic', value);
  83. }
  84. }
  85. });
  86. }
  87. });
  88. },
  89. /**
  90. * Annotate data list elements with CSS classes.
  91. */
  92. dataList: function () {
  93. return this.each(function () {
  94. var list = $(this);
  95. if (list) {
  96. list.find('dt').decorate('generic', ['odd', 'even', 'last']);
  97. list.find('dd').decorate('generic', ['odd', 'even', 'last']);
  98. }
  99. });
  100. }
  101. };
  102. /**
  103. * @param {String} method
  104. * @return {*}
  105. */
  106. $.fn.decorate = function (method) {
  107. var message;
  108. if (methods[method]) {
  109. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  110. } else if (typeof method === 'object' || !method) {
  111. return methods.init.apply(this, arguments);
  112. }
  113. message = $.mage.__('Method %s does not exist on jQuery.decorate');
  114. $.error(message.replace('%s', method));
  115. };
  116. }));