Dotclear

source: plugins/widgets/widgets.js @ 0:54703be25dd6

Revision 0:54703be25dd6, 5.6 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

2.3 branch (trunk) first checkin

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

Sites map