-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.container.js
More file actions
216 lines (177 loc) · 7.8 KB
/
Copy pathjquery.container.js
File metadata and controls
216 lines (177 loc) · 7.8 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Copyright (C) 2013 Robert DeSantis
hopluvr at gmail dot com
This file is part of DMX Studio.
DMX Studio is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
DMX Studio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with DMX Studio; see the file _COPYING.txt. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/
;
var ui_expandableContainer_id = 0;
(function($, undefined){
$.widget("ui.expandableContainer", {
pluginName: "expandableContainer",
options: {
remove_template: '<span class="ui-icon ui-icon-circle-minus" title="remove" style="cursor: pointer;"></span>',
add_template: '<span class="ui-icon ui-icon-circle-plus" title="insert" style="cursor: pointer;"></span>',
empty_template: '<span class="ui-icon ui-icon-circle-plus" title="insert first item" style="cursor: pointer;"></span>',
item_template: "<input class='container_input' type='text' size=5'>",
new_item_callback: null,
vertical: false,
controls: "right"
},
// Widget constructor (access with this.element)
_create: function() {
this.container = $('<div class="mainContainer"></div>');
this.container.appendTo(this.element).show();
},
_init: function () {
this.options.container_class_name = "container_item_" + ++ui_expandableContainer_id;
this.options.add_item_class_name = this.options.container_class_name + "_add_item";
this.container.empty();
this._checkEmpty();
},
_add: function (after) {
return this._add2( after, $(this.options.item_template), null );
},
_add2: function (after, widget, value) {
// Remove the itolated add item icon
var item_adder = this.container.find("." + this.options.add_item_class_name);
if (item_adder != null) {
item_adder.remove();
}
var style = (this.options.vertical) ? "clear:both; float:left; margin-bottom: 4px;" : "float:left; margin-right: 2px;";
var item = $('<div class="' + this.options.container_class_name + '" style="' + style + '"></div>');
var remove = $(this.options.remove_template);
var add = $(this.options.add_template);
if (this.options.vertical) { // Put the controls right or left for vertical
remove.css({ float: 'left', 'margin-left': '4px', 'margin-top' : '4px' } );
add.css({ float: 'left', 'margin-right': '4px', 'margin-top' : '4px' });
widget.css({ float: 'left' });
}
else { // Put the controls under for horizontal
remove.css( { clear: 'both', float: 'left', 'margin-right': '1px' } );
add.css({ float: 'left' });
widget.css({ clear: 'both', float: 'left' });
}
if (this.options.controls == "left") {
remove.appendTo(item);
add.appendTo(item);
widget.appendTo(item);
}
else {
widget.appendTo(item);
remove.appendTo(item);
add.appendTo(item);
}
var self = this;
add.bind("click", function (event) {
self._add(event.target.parentNode);
return false;
});
remove.bind("click", function (event) {
self._remove( $(event.target.parentNode) );
return false;
});
if (after != null)
item.insertAfter(after);
else
item.appendTo(this.container);
if (value != null) {
var input = item.find(".container_input");
if (input != null && input.length > 0) {
input.get(0).value = value;
input.get(0).val = value;
if (input.option != null)
input.option("value", value);
}
}
if (this.options.new_item_callback)
this.options.new_item_callback(item,value);
return item;
},
_remove: function ( item ) {
item.remove();
this._checkEmpty();
},
_checkEmpty: function () {
if (this.container.children().length == 0) {
var add = $( this.options.empty_template );
add.addClass(this.options.add_item_class_name);
add.css({ 'margin-right': '4px', 'margin-top': '4px' });
add.appendTo(this.container);
var self = this;
add.bind("click", function (event) {
self._add(null);
return false;
});
}
},
// Add any arbitrary widget
items: function () {
if (arguments.length == 0) { // return items
var items = this.container.find("." + this.options.container_class_name );
return items;
}
else { // set item(s)
if (jQuery.isArray(arguments[0])) {
var self = this;
$.map(arguments[0], function (widget) { self._add2(null, $(widget), null ) });
}
else
this._add2(null,$(arguments[0]), null);
return null;
}
},
values: function (value_fetcher) {
var values = [];
if (value_fetcher == null) {
value_fetcher = function (item) {
var item = item.find(".container_input");
if (item != null) {
item = item.get(0);
if (item.value != null)
return item.value;
else if (item.val != null)
return item.val;
else if (item.option != null)
return item.option("value");
else if (item.getAttribute("value") != null)
return item.getAttribute("value");
}
return null;
}
}
this.container.find("." + this.options.container_class_name).each(function () {
values.push(value_fetcher($(this)));
});
return values;
},
set_values: function (values) {
if (values == null)
return;
if (jQuery.isArray(values)) {
var self = this;
$.map(values, function (value) {
var item = self._add2(null, $(self.options.item_template), value);
});
}
else {
this._add2(null, $(this.options.item_template), values);
}
},
_setOption: function (key, value) {
this.options[key] = value;
$.Widget.prototype._setOption.apply(this, arguments);
}
});
})(jQuery);