Dotclear

source: plugins/widgets/widgets.js @ 665:315ff9eb4199

Revision 665:315ff9eb4199, 6.0 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Ajout d'un bandeau de widgets supplémentaire (custom)

Line 
1var 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               img_ctrl = title.firstChild;
135               
136               // There already an image
137               if (img_ctrl.nodeName.toLowerCase() == 'img') {
138                    continue;
139               }
140               
141               // Nothing to configure
142               if (title.nextSibling.childNodes.length == 0) {
143                    continue;
144               }
145               
146               img = document.createElement('img');
147               img.src = dotclear.img_plus_src;
148               img.alt = dotclear.img_plus_alt;
149               img.control = title.nextSibling;
150               img.onclick = function() { widgetConfig.call(this); };
151               space = document.createTextNode(' ');
152               title.insertBefore(img,img_ctrl);
153               title.insertBefore(space,img_ctrl);
154          }
155     }
156     
157     function widgetConfig() {
158          if (this.control.style.display == 'block') {
159               this.control.style.display = 'none';
160               this.src = dotclear.img_plus_src;
161               this.alt = dotclear.img_plus_alt;
162          } else {
163               this.control.style.display = 'block';
164               this.src = dotclear.img_minus_src;
165               this.alt = dotclear.img_minus_alt;
166          }
167     }
168     
169     function setHandle(item) {
170          //var handle = item.getElementsByTagName('h4').item(0);
171          var handle = $('p.widget-name',item).get(0);
172          $(handle).addClass('handler');
173          item.toolManDragGroup.setHandle(handle);
174     }
175});
176
177//
178// For Safari, we need to make a hard copy of the form and submit copy.
179// Yeah, a simple clone of the form is not enough
180// I hate this browser!
181//
182if (document.childNodes && !document.all && !navigator.taintEnabled)
183{
184     $(function() {
185          $('#sidebarsWidgets').submit(function() {
186               // Create a new form and copy each element inside
187               var f = document.createElement('form');
188               f.action = this.action;
189               f.method = this.method;
190               $(f).hide();
191               var fset = document.createElement('fieldset');
192               f.appendChild(fset);
193               
194               document.body.appendChild(f);
195               
196               $(this).find('input').each(function() {
197                    var i = document.createElement('input');
198                    i.type = this.type;
199                    i.name = this.name;
200                    i.value = this.value;
201                    if (this.type == 'checkbox') {
202                         i.checked = this.checked;
203                    }
204                    fset.appendChild(i);
205               });
206               
207               $(this).find('textarea').each(function() {
208                    var i = document.createElement('textarea');
209                    i.name = this.name;
210                    i.value = this.value;
211                    fset.appendChild(i);
212               });
213               
214               $(this).find('select').each(function() {
215                    var i = document.createElement('input');
216                    i.name = this.name;
217                    i.value = this.value;
218                    fset.appendChild(i);
219               });
220               
221               $(fset).append('<input type="hidden" name="wup" value="1"/>');
222               f.submit();
223               
224               return false;
225          });
226     });
227}
Note: See TracBrowser for help on using the repository browser.

Sites map