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