1 | var dragdrop = ToolMan.dragdrop(); |
---|
2 | $(function() { |
---|
3 | $('input[name="wreset"]').click(function() { |
---|
4 | return window.confirm(dotclear.msg.confirm_widgets_reset); |
---|
5 | }); |
---|
6 | }); |
---|
7 | |
---|
8 | $(function() { |
---|
9 | var widgets = document.getElementById('widgets'); |
---|
10 | var w_nav = document.getElementById('dndnav'); |
---|
11 | var w_ext = document.getElementById('dndextra'); |
---|
12 | var w_custom = document.getElementById('dndcustom'); |
---|
13 | |
---|
14 | w_nav.className = 'hideControls'; |
---|
15 | w_ext.className = 'hideControls'; |
---|
16 | w_custom.className = 'hideControls'; |
---|
17 | |
---|
18 | removeElements(document.getElementById('listWidgets'),'input'); |
---|
19 | removeElements(widgets,'p'); |
---|
20 | removeElements(w_nav,'p'); |
---|
21 | removeElements(w_ext,'p'); |
---|
22 | removeElements(w_custom,'p'); |
---|
23 | hideElements(w_nav,'input'); |
---|
24 | hideElements(w_ext,'input'); |
---|
25 | hideElements(w_custom,'input'); |
---|
26 | |
---|
27 | configControls(w_nav); |
---|
28 | configControls(w_ext); |
---|
29 | configControls(w_custom); |
---|
30 | |
---|
31 | dragdrop.makeListContainer(widgets,'div',setHandle); |
---|
32 | if (!document.all) { widgets.factory = true; } |
---|
33 | dragdrop.makeListContainer(w_nav,'div',setHandle); |
---|
34 | w_nav.onDragEnd = navDragEnd; |
---|
35 | dragdrop.makeListContainer(w_ext,'div',setHandle); |
---|
36 | w_ext.onDragEnd = extraDragEnd; |
---|
37 | dragdrop.makeListContainer(w_custom,'div',setHandle); |
---|
38 | w_custom.onDragEnd = customDragEnd; |
---|
39 | |
---|
40 | // Helper to remove some elements |
---|
41 | function removeElements(p,name) { |
---|
42 | name = name || 'div'; |
---|
43 | $(p).find(name+'.js-remove').each(function() { |
---|
44 | this.parentNode.removeChild(this); |
---|
45 | }); |
---|
46 | } |
---|
47 | |
---|
48 | // Helper to hide elements (but keep them) |
---|
49 | function hideElements(p,name) { |
---|
50 | name = name || 'div'; |
---|
51 | $(p).find(name+'.js-hide').each(function() { |
---|
52 | $(this).toggle(); |
---|
53 | }); |
---|
54 | } |
---|
55 | |
---|
56 | function removeEmptyMsg(p) { |
---|
57 | $(p).find('p.empty-widgets').each(function() { |
---|
58 | this.parentNode.removeChild(this); |
---|
59 | }); |
---|
60 | } |
---|
61 | |
---|
62 | // Events on dragEnd |
---|
63 | function navDragEnd() { |
---|
64 | formControls(this.parentNode,'nav'); |
---|
65 | configControls(this.parentNode); |
---|
66 | removeEmptyMsg(this.parentNode); |
---|
67 | } |
---|
68 | function extraDragEnd() { |
---|
69 | formControls(this.parentNode,'extra'); |
---|
70 | configControls(this.parentNode); |
---|
71 | removeEmptyMsg(this.parentNode); |
---|
72 | } |
---|
73 | function customDragEnd() { |
---|
74 | formControls(this.parentNode,'custom'); |
---|
75 | configControls(this.parentNode); |
---|
76 | removeEmptyMsg(this.parentNode); |
---|
77 | } |
---|
78 | |
---|
79 | // dragEnd helper |
---|
80 | function formControls(e,pr) { |
---|
81 | var items = new Array(); |
---|
82 | for (var i=0; i<e.childNodes.length; i++) { |
---|
83 | if (e.childNodes[i].nodeType == 1 && e.childNodes[i].nodeName.toLowerCase() == 'div') { |
---|
84 | items.push(e.childNodes[i]); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | var fields, itype; |
---|
89 | var r = new RegExp('^w\[[a-z]+]\[[0-9]+][[](.+?)]$',''); |
---|
90 | for (i=0; i<items.length; i++) { |
---|
91 | // Change field names |
---|
92 | fields = getFormControls(items[i]); |
---|
93 | var j; |
---|
94 | var f; |
---|
95 | for (j=0; j<fields.length; j++) { |
---|
96 | if (r.test(fields[j].name)) { |
---|
97 | itype = fields[j].name.replace(r,'$1'); |
---|
98 | |
---|
99 | $(fields[j]).attr('name','w['+pr+']['+i+']['+itype+']'); |
---|
100 | |
---|
101 | if (itype == 'order') { |
---|
102 | fields[j].value = i; |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | function getFormControls(e) { |
---|
110 | var input = e.getElementsByTagName('input'); |
---|
111 | var textarea = e.getElementsByTagName('textarea'); |
---|
112 | var select = e.getElementsByTagName('select'); |
---|
113 | var items = new Array(); |
---|
114 | var i; |
---|
115 | for (i=0; i<input.length; i++) { items.push(input[i]); } |
---|
116 | for (i=0; i<select.length; i++) { items.push(select[i]); } |
---|
117 | for (i=0; i<textarea.length; i++) { items.push(textarea[i]); } |
---|
118 | |
---|
119 | return items; |
---|
120 | } |
---|
121 | |
---|
122 | function configControls(e) { |
---|
123 | var items = new Array(); |
---|
124 | for (var i=0; i<e.childNodes.length; i++) { |
---|
125 | if (e.childNodes[i].nodeType == 1 && e.childNodes[i].nodeName.toLowerCase() == 'div') { |
---|
126 | items.push(e.childNodes[i]); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | var title, img_ctrl, img, space; |
---|
131 | for (i in items) { |
---|
132 | // Append config control |
---|
133 | title = $('p.widget-name',items[i]).get(0); |
---|
134 | order = $(title).find('input[name*=order]'); |
---|
135 | link = $('<a href="#" alt="expand" class="aexpand"/>').append($(title).text()); |
---|
136 | $(title).empty().append(order).append(link); |
---|
137 | img_ctrl = title.firstChild; |
---|
138 | |
---|
139 | // There already an image |
---|
140 | if (img_ctrl.nodeName.toLowerCase() == 'img') { |
---|
141 | continue; |
---|
142 | } |
---|
143 | |
---|
144 | // Nothing to configure |
---|
145 | if (title.nextSibling.childNodes.length == 0) { |
---|
146 | continue; |
---|
147 | } |
---|
148 | |
---|
149 | img = document.createElement('img'); |
---|
150 | img.src = dotclear.img_plus_src; |
---|
151 | img.alt = dotclear.img_plus_alt; |
---|
152 | img.control = title.nextSibling; |
---|
153 | img.onclick = function() { widgetConfig.call(this); }; |
---|
154 | link.click(function(e) { |
---|
155 | e.preventDefault(); |
---|
156 | widgetConfig.call($(this).prevAll('img').get(0)); |
---|
157 | }); |
---|
158 | space = document.createTextNode(' '); |
---|
159 | title.insertBefore(img,img_ctrl); |
---|
160 | title.insertBefore(space,img_ctrl); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | function widgetConfig() { |
---|
165 | if (this.control.style.display == 'block') { |
---|
166 | this.control.style.display = 'none'; |
---|
167 | this.src = dotclear.img_plus_src; |
---|
168 | this.alt = dotclear.img_plus_alt; |
---|
169 | } else { |
---|
170 | this.control.style.display = 'block'; |
---|
171 | this.src = dotclear.img_minus_src; |
---|
172 | this.alt = dotclear.img_minus_alt; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | function setHandle(item) { |
---|
177 | //var handle = item.getElementsByTagName('h4').item(0); |
---|
178 | var handle = $('p.widget-name',item).get(0); |
---|
179 | $(handle).addClass('handler'); |
---|
180 | item.toolManDragGroup.setHandle(handle); |
---|
181 | } |
---|
182 | }); |
---|
183 | |
---|
184 | // |
---|
185 | // For Safari, we need to make a hard copy of the form and submit copy. |
---|
186 | // Yeah, a simple clone of the form is not enough |
---|
187 | // I hate this browser! |
---|
188 | // |
---|
189 | if (document.childNodes && !document.all && !navigator.taintEnabled) |
---|
190 | { |
---|
191 | $(function() { |
---|
192 | $('#sidebarsWidgets').submit(function() { |
---|
193 | // Create a new form and copy each element inside |
---|
194 | var f = document.createElement('form'); |
---|
195 | f.action = this.action; |
---|
196 | f.method = this.method; |
---|
197 | $(f).hide(); |
---|
198 | var fset = document.createElement('fieldset'); |
---|
199 | f.appendChild(fset); |
---|
200 | |
---|
201 | document.body.appendChild(f); |
---|
202 | |
---|
203 | $(this).find('input').each(function() { |
---|
204 | var i = document.createElement('input'); |
---|
205 | i.type = this.type; |
---|
206 | i.name = this.name; |
---|
207 | i.value = this.value; |
---|
208 | if (this.type == 'checkbox') { |
---|
209 | i.checked = this.checked; |
---|
210 | } |
---|
211 | fset.appendChild(i); |
---|
212 | }); |
---|
213 | |
---|
214 | $(this).find('textarea').each(function() { |
---|
215 | var i = document.createElement('textarea'); |
---|
216 | i.name = this.name; |
---|
217 | i.value = this.value; |
---|
218 | fset.appendChild(i); |
---|
219 | }); |
---|
220 | |
---|
221 | $(this).find('select').each(function() { |
---|
222 | var i = document.createElement('input'); |
---|
223 | i.name = this.name; |
---|
224 | i.value = this.value; |
---|
225 | fset.appendChild(i); |
---|
226 | }); |
---|
227 | |
---|
228 | $(fset).append('<input type="hidden" name="wup" value="1"/>'); |
---|
229 | f.submit(); |
---|
230 | |
---|
231 | return false; |
---|
232 | }); |
---|
233 | }); |
---|
234 | } |
---|