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