]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/javascript/SimpleList.js
Release 6.1.4
[Github/sugarcrm.git] / modules / ModuleBuilder / javascript / SimpleList.js
1 /*********************************************************************************
2  * SugarCRM 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 if(typeof(SimpleList) == 'undefined'){
37         var Dom = YAHOO.util.Dom;
38     SimpleList = function(){
39         var editImage;
40         var deleteImage;
41         var ul_list;
42         var jstransaction;
43         var lastEdit;
44         var isIE = isSupportedIE();
45         return {
46     init: function(editImage, deleteImage) {
47         var ul = document.getElementById('ul1', 'drpdwn');
48         SimpleList.lastEdit = null; // Bug 14662
49         SimpleList.editImage = editImage;
50         SimpleList.deleteImage = deleteImage;
51         new YAHOO.util.DDTarget("ul1");
52            
53         for (i=0;i<SimpleList.ul_list.length;i++){
54             if ( typeof SimpleList.ul_list[i] != "number" && SimpleList.ul_list[i] == "" ) {
55                 SimpleList.ul_list[i] = SUGAR.language.get('ModuleBuilder', 'LBL_BLANK');
56             }
57             new Studio2.ListDD(SimpleList.ul_list[i], 'drpdwn', false);
58         }
59         YAHOO.util.Event.on("dropdownaddbtn", "click", this.addToList, 'dropdown_form');
60         SimpleList.jstransaction = new JSTransaction();
61         SimpleList.jstransaction.register('deleteDropDown', SimpleList.undoDeleteDropDown, SimpleList.undoDeleteDropDown);
62         SimpleList.jstransaction.register('changeDropDownValue', SimpleList.undoDropDownChange, SimpleList.redoDropDownChange);
63
64     },
65     isValidDropDownKey : function(value){
66         if(value.match(/^[\w\d \.]+$/i) || value == "")
67                 return true;
68         
69         return false;
70     },
71     isBlank : function(value){
72         return value == SUGAR.language.get('ModuleBuilder', 'LBL_BLANK') 
73                         || (typeof value != "number" && value == "");
74     },
75     addToList : function(event, form){
76         var drop_name = document.getElementById('drop_name');
77         var drop_value = document.getElementById('drop_value');
78         //Validate the dropdown key manually
79         removeFromValidate('dropdown_form', 'drop_name');
80         if(!SimpleList.isValidDropDownKey(drop_name.value)) {
81                         addToValidate('dropdown_form', 'drop_name', 'error', false, SUGAR.language.get("ModuleBuilder", "LBL_JS_VALIDATE_KEY"));
82         }
83         
84         if (!check_form("dropdown_form")) return;
85
86         var ul1=YAHOO.util.Dom.get("ul1");
87
88         var items = ul1.getElementsByTagName("li");
89         for (i=0;i<items.length;i=i+1) {
90             if((SimpleList.isBlank(items[i].id) && SimpleList.isBlank(drop_name.value)) || items[i].id == drop_name.value){
91                 alert("Key already exists in list");
92                 return;
93             }
94         }
95
96         liObj = document.createElement('li');
97         liObj.className = "draggable";
98         if(drop_name.value == '' || !drop_name.value){
99             liObj.id = SUGAR.language.get('ModuleBuilder', 'LBL_BLANK');
100         }else{
101             liObj.id = drop_name.value;
102         }
103
104         var text1 = document.createElement('input');
105         text1.type = 'hidden';
106         text1.id = 'value_' + liObj.id;
107         text1.name = 'value_' + liObj.id;
108         text1.value = drop_value.value;
109
110         var html = "<table width='100%'><tr><td><b>"+liObj.id+"</b><input id='value_"+liObj.id+"' value=\""+drop_value.value+"\" type = 'hidden'><span class='fieldValue' id='span_"+liObj.id+"'>";
111         if(drop_value.value == ""){
112             html += "[" + SUGAR.language.get('ModuleBuilder', 'LBL_BLANK') + "]";
113         }else{
114             html += "["+drop_value.value+"]";
115         }
116         html += "</span>";
117         html += "<span class='fieldValue' id='span_edit_"+liObj.id+"' style='display:none'>";
118         html += "<input type='text' id='input_"+liObj.id+"' value=\""+drop_value.value+"\" onchange='SimpleList.setDropDownValue(\""+liObj.id+"\", unescape(this.value), true)' >";
119         html += "</span>";
120         html += "</td><td align='right'><a href='javascript:void(0)' onclick='SimpleList.editDropDownValue(\""+liObj.id+"\", true)'>"+SimpleList.editImage+"</a>";
121         html += "&nbsp;<a href='javascript:void(0)' onclick='SimpleList.deleteDropDownValue(\""+liObj.id+"\", true)'>"+SimpleList.deleteImage+"</a>";
122         html += "</td></tr></table>";
123
124         liObj.innerHTML = html;
125         ul1.appendChild(liObj);
126         new Studio2.ListDD(liObj, 'drpdwn', false);
127         drop_value.value = "";
128         drop_name.value = "";
129         drop_name.focus();
130
131         SimpleList.jstransaction.record('deleteDropDown',{'id': liObj.id });
132
133     },
134  
135     sortAscending: function ()
136     {
137         // now sort using a Shellsort - do this rather than by using the inbuilt sort function as we need to sort a complex DOM inplace
138         var parent = YAHOO.util.Dom.get("ul1");
139         var items = parent.getElementsByTagName("li") ;
140         var increment = Math.floor ( items.length / 2 ) ;
141         
142         function swapItems(itemA, itemB) {
143                 var placeholder = document.createElement ( "li" ) ;
144             Dom.insertAfter(placeholder, itemA);
145             Dom.insertBefore(itemA, itemB);
146             Dom.insertBefore(itemB, placeholder);
147             
148             //Cleanup the placeholder element
149             parent.removeChild(placeholder);
150         }
151         
152         while ( increment > 0 )
153         {
154                 for (var i = increment; i < items.length; i++)
155                 {
156                 var j = i;
157                 var id = items[i].id;
158                 var iValue = document.getElementById( 'input_' + id ).value.toLowerCase() ;
159               
160                 while ( ( j >= increment ) && ( document.getElementById( 'input_' + items [j-increment].id ).value.toLowerCase() > iValue ) )
161                 {
162                         // logically, this is what we need to do: items [j] = items [j - increment];
163                         // but we're working with the DOM through a NodeList (items) which is readonly, so things aren't that simple
164                         // A placeholder will be used to keep track of where in the DOM the swap needs to take place
165                         // especially with IE which enforces the prohibition on duplicate Ids, so copying nodes is problematic
166                         swapItems(items [j], items [j - increment]);
167                         j = j - increment;
168                 }
169             }
170              
171             if (increment == 2)
172                 increment = 1;
173             else 
174                 increment = Math.floor (increment / 2.2);
175         }
176     },
177     sortDescending: function ()
178     {
179         this.sortAscending();
180         var reverse = function ( children )
181         {
182             var parent = children [ 0 ] . parentNode ;
183             var start = 0;
184             if ( children [ 0 ].id == '-blank-' ) // don't include -blank- element in the sort
185                 start = 1 ;
186             for ( var i = children.length - 1 ; i >= start ; i-- )
187             {
188                 parent.appendChild ( children [ i ] ) ;
189             }
190         };
191         reverse ( YAHOO.util.Dom.get("ul1").getElementsByTagName("li") ) ;
192     },
193     handleSave:function(){
194          var parseList = function(ul, title) {
195             var items = ul.getElementsByTagName("li");
196             var out = [];
197             for (i=0;i<items.length;i=i+1) {
198                 var name = items[i].id;
199                 var value = document.getElementById('input_'+name).value;
200                 out[i] = [ name , value ];
201             }
202             return YAHOO.lang.JSON.stringify(out);
203         };
204         var ul1=YAHOO.util.Dom.get("ul1");
205         var hasDeletedItem = false;
206         for(j = 0; j < SimpleList.jstransaction.JSTransactions.length; j++){            
207             var liEl = new YAHOO.util.Element(SimpleList.jstransaction.JSTransactions[j]['data']['id']);
208             if(liEl && liEl.hasClass('deleted'))
209                 hasDeletedItem = true;
210                 break;
211         }
212         if(hasDeletedItem) {
213                 if(!confirm(SUGAR.language.get('ModuleBuilder', 'LBL_CONFIRM_SAVE_DROPDOWN')))
214                         return false;           
215         }
216         
217         for(j = 0; j < SimpleList.jstransaction.JSTransactions.length; j++){
218             if(SimpleList.jstransaction.JSTransactions[j]['transaction'] == 'deleteDropDown'){
219                 var liEl = new YAHOO.util.Element(SimpleList.jstransaction.JSTransactions[j]['data']['id']);
220                 if(liEl && liEl.hasClass('deleted'))
221                         ul1.removeChild(liEl.get("element"));
222             }
223         }
224         var list = document.getElementById('list_value');
225
226         var out = parseList(ul1, "List 1");
227         list.value = out;
228         ModuleBuilder.refreshDD_name = document.getElementById('dropdown_name').value;
229         if (document.forms.popup_form)
230         {
231             ModuleBuilder.handleSave("dropdown_form", ModuleBuilder.refreshDropDown);
232         }
233         else
234         {
235             ModuleBuilder.handleSave("dropdown_form", ModuleBuilder.refreshGlobalDropDown);
236         }
237     },
238     deleteDropDownValue : function(id, record){
239         var field = new YAHOO.util.Element(id);
240         if(record){
241             SimpleList.jstransaction.record('deleteDropDown',{'id': id });
242         }
243         if (field.hasClass('deleted'))
244             field.removeClass('deleted');
245         else
246             field.addClass('deleted');
247     },
248     editDropDownValue : function(id, record){
249         //Close any other dropdown edits
250         if (SimpleList)
251             SimpleList.endCurrentDropDownEdit();
252         var dispSpan = document.getElementById('span_'+id);
253         var editSpan = document.getElementById('span_edit_'+id);
254         dispSpan.style.display = 'none';
255
256         if(SimpleList.isIE){
257             editSpan.style.display = 'inline-block';
258         }else{
259             editSpan.style.display = 'inline';
260         }
261         var textbox = document.getElementById('input_'+id);
262         textbox.focus();
263         SimpleList.lastEdit = id;
264     },
265     endCurrentDropDownEdit : function() {
266         if (SimpleList.lastEdit != null)
267         {
268             var valueLastEdit = unescape(document.getElementById('input_'+SimpleList.lastEdit).value);
269             SimpleList.setDropDownValue(SimpleList.lastEdit,valueLastEdit,true);
270         }
271     },
272     setDropDownValue : function(id, val, record){
273
274         if(record){
275             SimpleList.jstransaction.record('changeDropDownValue', {'id':id, 'new':val, 'old':document.getElementById('value_'+ id).value});
276         }
277         var dispSpan = document.getElementById('span_'+id);
278         var editSpan = document.getElementById('span_edit_'+id);
279         var textbox = document.getElementById('input_'+id);
280
281         dispSpan.style.display = 'inline';
282         editSpan.style.display = 'none';
283         dispSpan.innerHTML = "["+val+"]";
284         document.getElementById('value_'+ id).value = val;
285         SimpleList.lastEdit = null; // Bug 14662 - clear the last edit point behind us
286     },
287     undoDeleteDropDown : function(transaction){
288
289         SimpleList.deleteDropDownValue(transaction['id'], false);
290     },
291     undoDropDownChange : function(transaction){
292         SimpleList.setDropDownValue(transaction['id'], transaction['old'], false);
293     },
294     redoDropDownChange : function(transaction){
295         SimpleList.setDropDownValue(transaction['id'], transaction['new'], false);
296     },
297     undo : function(){
298         SimpleList.jstransaction.undo();
299     },
300     redo : function(){
301         SimpleList.jstransaction.redo();
302     }
303 }//return
304 }();
305 }