-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery-addable.js
More file actions
104 lines (80 loc) · 2.86 KB
/
jquery-addable.js
File metadata and controls
104 lines (80 loc) · 2.86 KB
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
/**
* Addable v1.0
* October 10, 2010
* Will Atwood Mitchell @ http://freepizza.cc
*
* Causes the selected element to add or remove another element. The other element can be a jQuery object
* or simply a selector. Options for 'adds' include applying an increment counter to each addition, removing the object
* used as a template (the default behavior), and specifying a selector to remove the added object. Options for
* 'removes' include specifying where to look for the selector in relation to the control element (parent, next, prev).
*
*/
;(function($){
$.fn.adds = function(selector, options) {
return this.each(function () {
var settings = $.extend({
counter: null,
count: 0,
removalSelector: '.remove',
removeOriginal: true
}, options);
var element = $(selector).clone();
var container = $(selector).parent();
if (settings.removeOriginal) {
$(selector).remove();
}
$(this).click(function (event) {
var newElement = element.clone();
if (typeof(settings.counter_string) != 'null') {
newElement.html(element.html().replace(new RegExp(settings.counter, 'g'), settings.count));
}
newElement.find(settings.removalSelector).removes(newElement);
container.append(newElement);
settings.count++;
event.preventDefault();
});
});
};
$.fn.removes = function(selector, options) {
return this.each(function () {
settings = $.extend({
node_type: 'root'
}, options);
if (typeof(selector) == 'object' || settings.node_type == 'root') {
$(this).click(function (event) {
$(selector).remove();
event.preventDefault();
});
} else {
if (settings.node_type == 'parent') {
var elementToRemove = $(this).parent(selector);
} else if (settings.node_type == 'parents') {
var elementToRemove = $(this).parents(selector);
} else if (settings.node_type == 'prev') {
var elementToRemove = $(this).prev(selector);
} else if (settings.node_type == 'next') {
var elementToRemove = $(this).prev(selector);
}
$(this).click(function (event) {
elementToRemove.remove();
event.preventDefault();
});
}
});
}
$.fn.removesParent = function(selector, options) {
return this.each(function () {
$(this).removes('', { node_type : 'parent' });
});
}
$.fn.removesNext = function(selector, options) {
return this.each(function () {
$(this).removes('', { node_type : 'next' });
});
}
$.fn.removesPrev = function(selector, options) {
return this.each(function () {
$(this).removes('', { node_type : 'prev' });
});
}
})(jQuery);