- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/widgets/dragdrop.js
r0 r1651 1 ToolMan._coordinatesFactory.inside = function(item,container) { 2 var iTL = this.topLeftOffset(item); 3 var iBR = this.bottomRightOffset(item); 4 var cTL = this.topLeftOffset(container); 5 var cBR = this.bottomRightOffset(container); 1 $(function() { 6 2 7 return (iTL.x >= cTL.x && iTL.x <= cBR.x && iTL.y >= cTL.y && iTL.y <= cBR.y) 8 || (iBR.x >= cTL.x && iBR.x <= cBR.x && iBR.y >= cTL.y && iBR.y <= cBR.y); 9 } 10 11 ToolMan._dragdropFactory = { 12 firstContainer: null, 13 lastContainer: null, 3 // clean 4 $('.js-remove').remove(); 5 $('.js-hide').hide(); 6 $('.widgetSettings').hide(); 7 $('.widgets, .sortable-delete').addClass('drag'); 14 8 15 makeDragable: function(item) { 16 var group = ToolMan.drag().createSimpleGroup(item); 17 group.register('dragstart',this._onDragStart); 18 group.register('dragmove', this._onDragMove); 19 group.register('dragend', this._onDragEnd); 20 21 item.isOutside = false; 22 item.started = false; 23 24 return group; 25 }, 26 27 makeListContainer: function(list,name) { 28 // each container becomes a linked list node 29 if (this.firstContainer == null) { 30 this.firstContainer = this.lastContainer = list; 31 list.previousContainer = null; 32 list.nextContainer = null; 33 } else { 34 list.previousContainer = this.lastContainer; 35 list.nextContainer = null; 36 this.lastContainer.nextContainer = list; 37 this.lastContainer = list; 38 } 39 40 var helpers = ToolMan.helpers(); 41 var coordinates = ToolMan.coordinates(); 42 43 //var children = list.childNodes; 44 var items = new Array(); 45 for (var i=0; i<list.childNodes.length; i++) { 46 if (list.childNodes[i].nodeType == 1 && 47 list.childNodes[i].nodeName.toLowerCase() == name.toLowerCase()) { 48 items.push(list.childNodes[i]); 49 } 50 } 51 52 // these functions are called when an item is draged over 53 // a container or out of a container bounds. onDragOut 54 // is also called when the drag ends with an item having 55 // been added to the container 56 list.onDragOver = new Function(); 57 list.onDragOut = new Function(); 58 list.onDragEnd = new Function(); 59 60 if (list.factory == undefined) { 61 list.factory = false; 62 } 63 64 var This = this; 65 helpers.map(items, function(item) { 66 var dragGroup = This.makeDragable(item); 67 dragGroup.setThreshold(4); 68 }); 69 for (var i = 2, n = arguments.length; i < n; i++) { 70 helpers.map(items, arguments[i]); 71 } 72 }, 73 74 _onDragStart: function(dragEvent) { 75 var container = ToolMan._dragdropFactory.firstContainer; 76 var item = dragEvent.group.element; 77 var coordinates = ToolMan.coordinates(); 78 79 // Factory is a sort of assembly pieces, we want to keep them 80 if (item.parentNode.factory) { 81 var origin = item.cloneNode(true); 82 item.parentNode.insertBefore(origin,item.nextSibling); 83 ToolMan._dragdropFactory.makeDragable(origin); 84 } 85 86 while (container != null) { 87 container.topLeft = coordinates.topLeftOffset(container); 88 container.bottomRight = coordinates.bottomRightOffset(container); 89 container = container.nextContainer; 90 } 91 92 // item starts out over current parent 93 item.started = true; 94 item.parentNode.onDragOver(); 95 }, 96 97 _onDragMove: function(dragEvent) { 98 var helpers = ToolMan.helpers(); 99 var coordinates = ToolMan.coordinates(); 100 101 var item = dragEvent.group.element; 102 var xmouse = dragEvent.transformedMouseOffset; 103 var moveTo = null; 104 105 // Check if we are nowhere 106 if (item.isOutside) { 107 var container = ToolMan._dragdropFactory.firstContainer; 108 while (container != null) { 109 if (coordinates.inside(item,container) && !container.factory) { 110 container.onDragOver(); 111 item.isOutside = false; 112 113 // since isOutside was true, the current parent is a 114 // temporary clone of some previous container node and 115 // it needs to be removed from the document 116 var tempParent = item.parentNode; 117 tempParent.removeChild(item); 118 container.appendChild(item); 119 //tempParent.parentNode.removeChild(tempParent); 120 121 break; 122 } 123 container = container.nextContainer; 9 // move 10 $( ".connected, .sortable-delete" ).sortable({ 11 tolerance: "move", 12 cursor: "move", 13 axis: "y", 14 dropOnEmpty: true, 15 handle: ".widget-name", 16 placeholder: "ui-sortable-placeholder", 17 connectWith: ".connected, .sortable-delete", 18 start: function( event, ui ) { 19 // petit décalage éstétique 20 ui.item.css('left', ui.item.position().left + 20); 21 }, 22 update: function(event, ui) { 23 24 ul = $(this); 25 widget = ui.item; 26 field = ul.parents('.widgets'); 27 28 // met a zéro le décalage 29 ui.item.css('left', 'auto'); 30 31 // signale les zone vide 32 if( ul.find('li').length == 0 ) 33 field.find('.empty-widgets').show(); 34 else field.find('.empty-widgets').hide(); 35 36 // remove 37 if( widget.parents('ul').is('.sortable-delete') ) { 38 widget.hide('slow', function() { 39 $(this).remove(); 40 }); 124 41 } 125 42 126 if (this.isOutside) { 127 return; 43 // réordonne 44 if( ul.attr('id') ) { 45 ul.find('li').each(function(i) { 46 47 // trouve la zone de récéption 48 var name = ul.attr('id').split('dnd').join(''); 49 50 // modifie le name en conséquence 51 $(this).find('*[name^=w]').each(function(){ 52 tab = $(this).attr('name').split(']['); 53 tab[0] = "w["+name; 54 tab[1] = i; 55 $(this).attr('name', tab.join('][')); 56 }); 57 58 // ainssi que le champ d'ordre sans js (au cas ou) 59 $(this).find('input[title=ordre]').val(i); 60 61 }); 128 62 } 63 64 // expand 65 if(widget.find('img.expand').length == 0) { 66 dotclear.postExpander(widget); 67 dotclear.viewPostContent(widget, 'close'); 68 } 69 129 70 } 130 // Outside the parent node 131 else if (!coordinates.inside(item,item.parentNode)) { 132 item.parentNode.onDragOut(); 133 item.isOutside = true; 134 var container = ToolMan._dragdropFactory.firstContainer; 135 while (container != null) { 136 if (coordinates.inside(item,container) && !container.factory) { 137 container.onDragOver(); 138 item.isOutside = false; 139 container.appendChild(item); 140 break; 141 } 142 container = container.nextContainer; 143 } 144 // if we're not in any container now, make a temporary clone of 145 // the previous container node and add it to the document 146 if (this.isOutside) { 147 var tempParent = item.parentNode.cloneNode(false); 148 item.parentNode.removeChild(item); 149 tempParent.appendChild(item); 150 document.body.appendChild(tempParent); 151 return; 152 } 71 }); 72 73 // add 74 $( "#widgets > li" ).draggable({ 75 tolerance: "move", 76 cursor: "move", 77 connectToSortable: ".connected", 78 helper: "clone", 79 revert: "invalid", 80 start: function( event, ui ) { 81 ui.helper.css({'width': $('#widgets > li').css('width')}); 153 82 } 154 155 // if we get here, we're inside some container bounds, so we do 156 // everything the original dragsort script did to swap us into the 157 // correct position 158 159 if (item.parentNode.factory) { 160 return; 161 } 162 163 var moveTo = null 164 165 var previous = helpers.previousItem(item, item.nodeName) 166 while (previous != null) { 167 var bottomRight = coordinates.bottomRightOffset(previous) 168 if (xmouse.y <= bottomRight.y && xmouse.x <= bottomRight.x) { 169 moveTo = previous 170 } 171 previous = helpers.previousItem(previous, item.nodeName) 172 } 173 if (moveTo != null) { 174 helpers.moveBefore(item, moveTo) 175 return 176 } 177 178 var next = helpers.nextItem(item, item.nodeName) 179 while (next != null) { 180 var topLeft = coordinates.topLeftOffset(next) 181 if (topLeft.y <= xmouse.y && topLeft.x <= xmouse.x) { 182 moveTo = next 183 } 184 next = helpers.nextItem(next, item.nodeName) 185 } 186 if (moveTo != null) { 187 helpers.moveBefore(item, helpers.nextItem(moveTo, item.nodeName)) 188 return 189 } 190 }, 83 }); 191 84 192 _onDragEnd: function(dragEvent) { 193 var item = dragEvent.group.element; 194 195 if (!item.started) { 196 return; 197 } 198 199 if (item.isOutside || item.parentNode.factory) { 200 item.parentNode.removeChild(item); 201 return; 202 } 203 204 item.parentNode.onDragEnd.call(item); 205 ToolMan.coordinates().create(0, 0).reposition(dragEvent.group.element); 206 } 207 }; 208 209 ToolMan.dragdrop = function() { 210 return ToolMan._dragdropFactory; 211 }; 85 });
Note: See TracChangeset
for help on using the changeset viewer.