yafowil.widget.array 1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,235 @@
1
+ /* jslint browser: true */
2
+ /* global jQuery, yafowil */
3
+ /*
4
+ * yafowil array widget
5
+ */
6
+
7
+ if (window.yafowil === undefined) {
8
+ window.yafowil = {};
9
+ }
10
+
11
+ (function($, yafowil) {
12
+ "use strict";
13
+
14
+ $(document).ready(function() {
15
+ // initial binding
16
+ yafowil.array.binder(document);
17
+
18
+ // add after ajax binding if bdajax present
19
+ if (window.bdajax !== undefined) {
20
+ window.bdajax.register(yafowil.array.binder.bind(yafowil.array));
21
+ }
22
+ });
23
+
24
+ $.extend(yafowil, {
25
+
26
+ array: {
27
+
28
+ hooks: {
29
+ before_add: {},
30
+ add: {},
31
+ remove: {},
32
+ before_up: {},
33
+ up: {},
34
+ before_down: {},
35
+ down: {},
36
+ index: {}
37
+ },
38
+
39
+ container: function(context) {
40
+ return $(context).parents('.array').first();
41
+ },
42
+
43
+ template: function(context) {
44
+ var container = this.container(context);
45
+ var tmpl = container.children('.arraytemplate').clone();
46
+ return tmpl;
47
+ },
48
+
49
+ create_row: function(context) {
50
+ var css = $(context).parents('.array').attr('class');
51
+ var row = '';
52
+ row += '<tr>';
53
+ row += '<td class="widget">';
54
+ row += '</td>';
55
+ if (css.indexOf('array-static') === -1) {
56
+ row += '<td class="actions">';
57
+ row += '<div class="array_actions">';
58
+ if (css.indexOf('array-add') > -1) {
59
+ row += '<a class="array_row_add" href="#">';
60
+ row += '<span class="icon-plus-sign"> </span>';
61
+ row += '</a>';
62
+ }
63
+ if (css.indexOf('array-remove') > -1) {
64
+ row += '<a class="array_row_remove" href="#">';
65
+ row += '<span class="icon-minus-sign"> </span>';
66
+ row += '</a>';
67
+ }
68
+ if (css.indexOf('array-sort') > -1) {
69
+ row += '<a class="array_row_up" href="#">';
70
+ row += '<span class="icon-circle-arrow-up"> </span>';
71
+ row += '</a>';
72
+ row += '<a class="array_row_down" href="#">';
73
+ row += '<span class="icon-circle-arrow-down"> </span>';
74
+ row += '</a>';
75
+ }
76
+ row += '</div>';
77
+ row += '</td>';
78
+ }
79
+ row += '</tr>';
80
+ row = $(row);
81
+ var template = this.template(context);
82
+ $('.widget', row).append(template.children());
83
+ return row;
84
+ },
85
+
86
+ get_row: function(action) {
87
+ return $(action).parent().parent().parent();
88
+ },
89
+
90
+ base_id: function(context) {
91
+ var id = context.parents('.array').first().attr('id');
92
+ return id.substring(6, id.length);
93
+ },
94
+
95
+ reset_indices: function(context) {
96
+ var index = 0;
97
+ var base_id = this.base_id(context);
98
+ var that = this;
99
+ var row;
100
+ context.children().each(function() {
101
+ row = $(this);
102
+ that.set_row_index(row, base_id, index);
103
+ that.notify_hooks(that.hooks.index, row, index);
104
+ index++;
105
+ });
106
+ this.binder(context);
107
+ },
108
+
109
+ set_row_index: function(node, base_id, index) {
110
+ var base_name = base_id.replace(/\-/g, '.');
111
+ var set_index = this.set_attr_index.bind(this);
112
+ var that = this;
113
+ var child;
114
+ node.children().each(function() {
115
+ child = $(this);
116
+ set_index(child, 'id', base_id, index, '-');
117
+ set_index(child, 'for', base_id, index, '-');
118
+ set_index(child, 'name', base_name, index, '.');
119
+ that.set_row_index(child, base_id, index);
120
+ });
121
+ },
122
+
123
+ set_attr_index: function(node, attr, base, index, delim) {
124
+ var value = node.attr(attr);
125
+ if (value && value.indexOf(base) > -1) {
126
+ node.attr(
127
+ attr,
128
+ this.set_value_index(value, base, index, delim)
129
+ );
130
+ }
131
+ },
132
+
133
+ set_value_index: function(value, base, index, delim) {
134
+ var idx_0 = value.indexOf(base) + base.length + 1;
135
+ var idx_1 = value.indexOf(delim, idx_0);
136
+ var pre = value.substring(0, idx_0);
137
+ var post = '';
138
+ if (idx_1 > -1) {
139
+ post = value.substring(idx_1, value.length);
140
+ }
141
+ return pre + index + post;
142
+ },
143
+
144
+ mark_disabled: function(context) {
145
+ if (context !== document) {
146
+ context = $(context).parent();
147
+ } else {
148
+ context = $(context);
149
+ }
150
+ var up_sel = '> tr > td.actions a.array_row_up';
151
+ var down_sel = '> tr > td.actions a.array_row_down';
152
+ $('tbody:visible', context).each(function() {
153
+ var body = $(this);
154
+ $(up_sel, body)
155
+ .removeClass('array_row_up_disabled')
156
+ .first()
157
+ .addClass('array_row_up_disabled');
158
+ $(down_sel, body)
159
+ .removeClass('array_row_down_disabled')
160
+ .last()
161
+ .addClass('array_row_down_disabled');
162
+ });
163
+ },
164
+
165
+ notify_hooks: function(hooks, ...args) {
166
+ for (var name in hooks) {
167
+ hooks[name].apply(null, args);
168
+ }
169
+ },
170
+
171
+ array_row_add: function(event) {
172
+ event.preventDefault();
173
+ var row = this.get_row(event.currentTarget);
174
+ var new_row = this.create_row(event.currentTarget);
175
+ var container = row.parent();
176
+ if (container.get(0).tagName.toLowerCase() === 'tbody') {
177
+ this.notify_hooks(this.hooks.before_add, new_row, container);
178
+ row.after(new_row);
179
+ } else {
180
+ var table = container.parent();
181
+ var body = $('tbody', table).first();
182
+ container = body;
183
+ this.notify_hooks(this.hooks.before_add, new_row, container);
184
+ container.prepend(new_row);
185
+ }
186
+ this.reset_indices(container);
187
+ this.notify_hooks(this.hooks.add, new_row);
188
+ },
189
+
190
+ array_row_remove: function(event) {
191
+ event.preventDefault();
192
+ var row = this.get_row(event.currentTarget);
193
+ this.notify_hooks(this.hooks.remove, row);
194
+ var container = row.parent();
195
+ row.remove();
196
+ this.reset_indices(container);
197
+ },
198
+
199
+ array_row_up: function(event) {
200
+ event.preventDefault();
201
+ var row = this.get_row(event.currentTarget);
202
+ this.notify_hooks(this.hooks.before_up, row);
203
+ row.insertBefore(row.prev());
204
+ this.reset_indices(row.parent());
205
+ this.notify_hooks(this.hooks.up, row);
206
+ },
207
+
208
+ array_row_down: function(event) {
209
+ event.preventDefault();
210
+ var row = this.get_row(event.currentTarget);
211
+ this.notify_hooks(this.hooks.before_down, row);
212
+ row.insertAfter(row.next());
213
+ this.reset_indices(row.parent());
214
+ this.notify_hooks(this.hooks.down, row);
215
+ },
216
+
217
+ binder: function(context) {
218
+ this.mark_disabled(context);
219
+ var add_sel = 'a.array_row_add';
220
+ var add_handler = this.array_row_add.bind(this)
221
+ $(add_sel, context).off().on('click', add_handler);
222
+ var remove_sel = 'a.array_row_remove';
223
+ var remove_handler = this.array_row_remove.bind(this);
224
+ $(remove_sel, context).off().on('click', remove_handler);
225
+ var up_sel = 'a.array_row_up';
226
+ var up_handler = this.array_row_up.bind(this)
227
+ $(up_sel, context).off().on('click', up_handler);
228
+ var down_sel = 'a.array_row_down';
229
+ var down_handler = this.array_row_down.bind(this)
230
+ $(down_sel, context).off().on('click', down_handler);
231
+ }
232
+ }
233
+ });
234
+
235
+ })(jQuery, yafowil);