]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/javascript/studio2FieldDD.js
Release 6.2.0
[Github/sugarcrm.git] / modules / ModuleBuilder / javascript / studio2FieldDD.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36
37 Studio2.FieldDD = function(id, sGroup) {
38         Studio2.FieldDD.superclass.constructor.call(this, id, sGroup);
39 };
40
41
42 YAHOO.extend(Studio2.FieldDD, YAHOO.util.DDProxy, {
43
44     startDrag: function(x, y) {
45         // make the proxy look like the source element
46                 var dragEl = this.getDragEl();
47                 var clickEl = this.getEl();
48                 dragEl.innerHTML = clickEl.innerHTML;
49                 dragEl.className = clickEl.className;
50                 Studio2.copyId = null;
51                 this.showAnimation = true;
52
53                 if (Studio2.isSpecial(clickEl) && (Studio2.establishLocation(clickEl) == 'toolbox')) {
54                         var copy = Studio2.copyElement(clickEl);
55                         Studio2.setCopy(copy);
56                         clickEl.parentNode.insertBefore(copy,clickEl.nextSibling);
57                         YAHOO.util.Dom.setStyle(clickEl, "display", "none");    // don't want it to take up any space
58                 } else {
59                         YAHOO.util.Dom.setStyle(clickEl, "visibility", "hidden"); // want a empty space as we're dragging it away from this place
60                 }
61     },
62
63     endDrag: function(e) {
64         ModuleBuilder.state.isDirty=true;
65         var srcEl = this.getEl();
66         var proxy = this.getDragEl();
67         var proxyid = proxy.id;
68         var thisid = this.id;
69
70         if (YAHOO.util.Dom.get(srcEl)) { // if we have a valid srcEl still...hasn't been deleted earlier
71                         // Show the proxy element and animate it to the src element's location
72                 YAHOO.util.Dom.setStyle(proxy, "visibility", "");
73                 YAHOO.util.Dom.setStyle(srcEl, "display", ""); // display!=none for getXY to work
74                 //YAHOO.util.Dom.setStyle(proxy).alignTo(srcEl, 'tl', null, {
75                                 //callback: function(){
76                 YAHOO.util.Dom.setStyle(proxyid, "visibility", "hidden");
77                         if(typeof(YAHOO.util.Dom.get(thisid)) != 'undefined' && YAHOO.util.Dom.get(thisid)!=null) 
78                                 YAHOO.util.Dom.setStyle(thisid, "visibility", "");
79                 }
80                 if (Studio2.isSpecial(srcEl) && Studio2.copy()) {
81                         Studio2.activateCopy(); // activateCopy makes it active, and removes the flag that says there is a copy
82                 } 
83                 
84                 proxy.innerHTML = "";
85     },
86
87         onInvalidDrop: function(e) {
88                 var dragEl = this.getDragEl();
89                 dragEl.innerHTML = '';
90                 Studio2.removeCopy();
91                 YAHOO.util.Dom.setStyle(this.getEl(), "display", "block");
92         },
93
94     onDragDrop: function(e, id) {
95                 var srcEl = this.getEl();
96                 var destEl = YAHOO.util.Dom.get(id); // where this element is being dropped
97
98                 var srcLocation = Studio2.establishLocation(srcEl);
99                 var destLocation = Studio2.establishLocation(destEl);
100
101                 // CASE1: Trying to delete an item from the toolbox or move fields within the toolbox - don't allow
102                 if ( ((srcLocation == 'toolbox') && (destLocation == 'delete')) ||
103                          ((srcLocation == 'toolbox') && (destLocation == 'toolbox'))) {
104                         Studio2.removeCopy();
105                         YAHOO.util.Dom.setStyle(srcEl, "display", "block");     // make it visible again - we made special elements invisible in startDrag
106                         return;
107                 }
108                 // CASE2: Delete a panel element
109                 // if source was in a panel (not toolbox) and destination is delete then remove this element
110                 if ((srcLocation == 'panels') && (destLocation == 'delete')) {
111
112                         if(Studio2.isSpecial(srcEl)) //nsingh- Bug 23057 Disallow deleting a (filler) as it does not make sense to do so.
113                                 return;
114                         var parent = srcEl.parentNode;
115                         var sibling = srcEl.previousSibling;
116                         while(sibling != null) {
117                                 if (sibling.className && (sibling.className.indexOf('le_field') != -1)) {
118                                         break;
119                                 }
120                                 sibling = sibling.previousSibling;
121                         }
122                         if (sibling == null) {
123                                 sibling = srcEl.nextSibling;
124                                 while(sibling != null) {
125                                         if (sibling.className && (sibling.className.indexOf('le_field') != -1)) {
126                                                 break;
127                                         }
128                                         sibling = sibling.nextSibling;
129                                 }
130                         }
131                         Studio2.removeElement(srcEl);
132                         Studio2.unregisterExpandableField( srcEl );
133 //                      this.showAnimation = false; // can't show animation as the source no longer exists
134                         if (sibling == null) {
135                         // If we've just deleted the last field from a panel then we need to tidy up
136                                 Studio2.tidyFields(parent);
137                         } else {
138                                 Studio2.registerExpandableField(sibling);
139                         }
140                         return;
141                 } // end delete
142
143                 // CASE3: Simple field swap
144                 // Either neither one is special, or they're both special and both in panels
145                 if (( ! Studio2.isSpecial(srcEl) && ! Studio2.isSpecial(destEl)) ||
146                         ( Studio2.isSpecial(srcEl) && Studio2.isSpecial(destEl) && (srcLocation == 'panels') && (destLocation == 'panels')) ) {
147                         Studio2.swapElements(srcEl, destEl);
148                         this.runSpecialCode(srcEl, destEl);
149
150                         return;
151                 }
152
153                 // CASE4: swapping a special field from the toolbox with a field in a panel
154                 if (Studio2.copy() && (destLocation == 'panels')) {
155                         // CASE: split a field
156                         //Disallow (filler) on (filler)
157                          if( Studio2.isSpecial(destEl) ) {Studio2.removeCopy(); return }
158
159                         var destSibling = Studio2.nextField( destEl ) || Studio2.prevField( destEl );
160                         if( Studio2.isExpandable( destEl ) && destEl.getAttribute("state") == 'expanded' ){
161                                 Studio2.removeCopy(); return;
162                         }
163                         if( Studio2.isExpandable( destEl ) && destEl.getAttribute("state") == 'reduced' ){ Studio2.unregisterExpandableField( destEl ); }
164                         var copy = Studio2.copyElement(srcEl);
165                         Studio2.activateElement(copy);
166                         YAHOO.util.Dom.setStyle(copy, "display", "");
167                         Studio2.swapElements( Studio2.copy(),destEl );
168                         YAHOO.util.Dom.setStyle(srcEl, "display", "");
169                         Studio2.registerExpandableField (destSibling );
170
171                         return;
172                 }
173
174                 // CASE5: moving a plain field from the panel to a special field in the toolbox - just copy
175                 if ( ! Studio2.isSpecial(srcEl) && Studio2.isSpecial(destEl) && (destLocation == 'toolbox')) {
176                         // make a copy of the destination
177                         if(Studio2.isExpandable (srcEl ) && Studio2.isExpanded( srcEl)) {
178                                 Studio2.toggleFieldWidth(srcEl.id); //bring back the old filler.
179                                 Studio2.unregisterExpandableField ( srcEl );
180                         }
181                         //check if srcSibling needs to expand
182 //                      var srcSibling = ;
183
184                         var copy = Studio2.copyElement(destEl);
185                         var destination = document.getElementById('availablefields');
186                         destination.appendChild(copy);
187                         Studio2.swapElements(copy,srcEl);
188                         YAHOO.util.Dom.setStyle(srcEl, "display", "");
189                         Studio2.activateElement(copy);
190                         //if src is expanded, reduce it then unregister
191
192
193                         //After Swap Only.
194                         Studio2.registerExpandableField( Studio2.nextField( srcEl ) || Studio2.prevField( srcEl ) );
195                         return;
196                 }
197
198                 //CASE6: (filler) droppped on a expandable field.
199                 if(Studio2.isSpecial(srcEl)  && destLocation == srcLocation  ){
200                         //Disallow Swap if dropping on a expanded field.
201                         if( Studio2.isExpandable( destEl ) && Studio2.isExpanded( destEl )) {return; }
202                         var srcSibling = Studio2.prevField( srcEl ) || Studio2.nextField( srcEl );
203                         var destSibling = Studio2.prevField( destEl ) || Studio2.nextField( destEl );
204                         Studio2.swapElements(srcEl, destEl); //don't change order.
205                         if ( !Studio2.isExpandable( destSibling ) && Studio2.isExpandable(srcSibling) && Studio2.isReduced(srcSibling) && !(srcSibling.id == destEl.id && srcEl.id == destSibling.id)) {
206                                 Studio2.unregisterExpandableField( srcSibling );
207                                 Studio2.registerExpandableField (destSibling );
208                                 Studio2.unregisterExpandableField( destEl );
209                         }
210                         if ( !Studio2.isExpandable( destEl ) && Studio2.isSpecial( destSibling )) {
211                                 Studio2.registerExpandableField (destEl );
212                         }
213                         if(!Studio2.isSpecial(destSibling)) {Studio2.registerExpandableField (destSibling )}
214                         return;
215                 }
216                 //CASE 7: A special field swapped with a regular field. Source is not-special, destination is special.
217                 if(!Studio2.isSpecial(srcEl) && Studio2.isSpecial(destEl) && destLocation == srcLocation) {
218                         /**
219                                 if destination's left sibling is expandable.
220                                         unregister left sibling from expandable.
221                                 if src field's left sibling is not special
222                                         register left sibling to expandable.
223                         */
224
225                         var srcSibling = Studio2.prevField(srcEl) || Studio2.nextField( srcEl ) ;
226                         var destSibling = Studio2.prevField(destEl) || Studio2.nextField( destEl );
227
228                         var sameRow = (srcSibling!=null && destSibling!=null) ? (srcSibling.id == destEl.id && destSibling.id == srcEl.id) : false;
229
230                         if (Studio2.isExpandable( srcEl ) && Studio2.isExpanded( srcEl )) {return;} //disallow dropping expanded fields onto fillers.
231                         if (Studio2.isExpandable ( srcEl ) && Studio2.isReduced( srcEl ) && !sameRow) {Studio2.unregisterExpandableField( srcEl );}
232                         if (Studio2.isExpandable (destSibling) && !sameRow ){Studio2.unregisterExpandableField( destSibling )}
233                         //expand src sibling
234                         if( srcEl.id == destSibling.id && srcSibling.id == destEl.id ) {Studio2.registerExpandableField ( srcEl ) }
235                         Studio2.swapElements(srcEl, destEl);
236                         if (Studio2.isSpecial(destSibling)) {Studio2.registerExpandableField(srcEl)}
237                         Studio2.registerExpandableField( srcSibling );
238
239                         return;
240
241                 }
242
243                 if( !Studio2.isSpecial( srcEl ) && Studio2.isSpecial( destEl) && destLocation == 'panels' && srcLocation =='toolbox'){
244                         var destSibling = Studio2.nextField( destEl ) || Studio2.prevField ( destEl );
245                         Studio2.unregisterExpandableField( destSibling );
246                         Studio2.swapElements( srcEl,destEl );
247                         Studio2.removeElement( destEl ) ;
248                         return;
249                 }
250
251                 Studio2.swapElements( srcEl,destEl );
252                 this.runSpecialCode(srcEl,destEl);
253                 if ((srcLocation != destLocation)) {
254                         if (Studio2.isSpecial(srcEl) && ! Studio2.isSpecial(destEl))  {
255                                 Studio2.removeElement(srcEl);
256 //                              this.showAnimation = false;
257                                 return;
258                         }
259                         if (Studio2.isSpecial(destEl) && ! Studio2.isSpecial(srcEl))  {
260                                 Studio2.removeElement(destEl);
261 //                              this.showAnimation = false;
262                                 return;
263                         }
264                 }
265
266
267     },
268     runSpecialCode: function(srcEl, destEl){
269                 var srcLeftSibling = Studio2.prevField(srcEl);
270                 var srcRightSibling = Studio2.nextField(srcEl);
271                 var destRightSibling = Studio2.nextField(destEl);
272                 var destLeftSibling = Studio2.prevField(destEl);
273
274
275         //For every affected element unexpand if needed.
276         //registration vs Transformation.
277
278                 if ( Studio2.isExpandable (srcEl ) && Studio2.isExpandable( destEl) ){
279                         //src is dest now. copy dest's properties to src.
280                         Studio2.swapStates( srcEl, destEl );
281                         //srcEl.setAttribute("state", destEl.getAttribute("state"));
282                 }
283                 var registerSrc = !Studio2.isExpandable( srcEl );
284                 var destExpandable = !Studio2.isSpecial(destEl) && ((null==destRightSibling && null==destLeftSibling)
285                                                         || (null !== destRightSibling) && Studio2.isSpecial(destRightSibling));
286
287                 var srcUnexpandable = !Studio2.isSpecial(srcEl) && ((null!==srcLeftSibling && !Studio2.isSpecial(srcLeftSibling))
288                                                         || ((null !== srcRightSibling) && !Studio2.isSpecial(srcRightSibling)));
289                 var destUnexpandable = !Studio2.isSpecial(destEl) && ((null!==destLeftSibling && !Studio2.isSpecial(destLeftSibling))
290                                                         || ((null!== destRightSibling) && !Studio2.isSpecial(destRightSibling)));
291
292
293                         if( registerSrc ){
294                                 Studio2.registerExpandableField( srcEl );
295                         }
296                         if(srcUnexpandable){
297                                 Studio2.unregisterExpandableField(  srcEl );
298                         }
299                         if(destExpandable){
300                                 Studio2.registerExpandableField(destEl);
301                         }
302                         if(destUnexpandable){
303                                 Studio2.unregisterExpandableField( destEl );
304                         }
305                 if(srcLeftSibling!==null && !Studio2.isSpecial(srcLeftSibling) && !Studio2.isSpecial(srcEl))
306                         Studio2.unregisterExpandableField( srcLeftSibling );
307
308
309
310         return;
311     }
312
313 });
314
315