1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
36 if(typeof(SimpleList) == 'undefined'){
37 var Dom = YAHOO.util.Dom;
38 SimpleList = function(){
44 var isIE = isSupportedIE();
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");
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');
57 new Studio2.ListDD(SimpleList.ul_list[i], 'drpdwn', false);
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);
65 isValidDropDownKey : function(value){
66 if(value.match(/^[\w\d \.]+$/i) || value == "")
71 isBlank : function(value){
72 return value == SUGAR.language.get('ModuleBuilder', 'LBL_BLANK')
73 || (typeof value != "number" && value == "");
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(escape(drop_name.value))) {
81 addToValidate('dropdown_form', 'drop_name', 'error', false, SUGAR.language.get("ModuleBuilder", "LBL_JS_VALIDATE_KEY"));
84 if (!check_form("dropdown_form")) return;
86 var ul1=YAHOO.util.Dom.get("ul1");
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(escape(drop_name.value))) || items[i].id == escape(drop_name.value)){
91 alert("Key already exists in list");
94 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)))){
95 alert(SUGAR.language.get('ModuleBuilder', 'LBL_DROPDOWN_BLANK_WARNING'));
100 liObj = document.createElement('li');
101 liObj.className = "draggable";
102 if(escape(drop_name.value) == '' || !escape(drop_name.value)){
103 liObj.id = SUGAR.language.get('ModuleBuilder', 'LBL_BLANK');
105 liObj.id = escape(drop_name.value);
108 var text1 = document.createElement('input');
109 text1.type = 'hidden';
110 text1.id = 'value_' + liObj.id;
111 text1.name = 'value_' + liObj.id;
112 text1.value = escape(drop_value.value);
114 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+"'>";
115 if(drop_value.value == ""){
116 html += "[" + SUGAR.language.get('ModuleBuilder', 'LBL_BLANK') + "]";
118 html += "["+escape(drop_value.value)+"]";
121 html += "<span class='fieldValue' id='span_edit_"+liObj.id+"' style='display:none'>";
122 html += "<input type='text' id='input_"+liObj.id+"' value=\""+escape(drop_value.value)+"\" onchange='SimpleList.setDropDownValue(\""+liObj.id+"\", unescape(this.value), true)' >";
124 html += "</td><td align='right'><a href='javascript:void(0)' onclick='SimpleList.editDropDownValue(\""+liObj.id+"\", true)'>"+SimpleList.editImage+"</a>";
125 html += " <a href='javascript:void(0)' onclick='SimpleList.deleteDropDownValue(\""+liObj.id+"\", true)'>"+SimpleList.deleteImage+"</a>";
126 html += "</td></tr></table>";
128 liObj.innerHTML = html;
129 ul1.appendChild(liObj);
130 new Studio2.ListDD(liObj, 'drpdwn', false);
131 drop_value.value = "";
132 drop_name.value = "";
135 SimpleList.jstransaction.record('deleteDropDown',{'id': liObj.id });
139 sortAscending: function ()
141 // now sort using a Shellsort - do this rather than by using the inbuilt sort function as we need to sort a complex DOM inplace
142 var parent = YAHOO.util.Dom.get("ul1");
143 var items = parent.getElementsByTagName("li") ;
144 var increment = Math.floor ( items.length / 2 ) ;
146 function swapItems(itemA, itemB) {
147 var placeholder = document.createElement ( "li" ) ;
148 Dom.insertAfter(placeholder, itemA);
149 Dom.insertBefore(itemA, itemB);
150 Dom.insertBefore(itemB, placeholder);
152 //Cleanup the placeholder element
153 parent.removeChild(placeholder);
156 while ( increment > 0 )
158 for (var i = increment; i < items.length; i++)
161 var id = items[i].id;
162 var iValue = document.getElementById( 'input_' + id ).value.toLowerCase() ;
164 while ( ( j >= increment ) && ( document.getElementById( 'input_' + items [j-increment].id ).value.toLowerCase() > iValue ) )
166 // logically, this is what we need to do: items [j] = items [j - increment];
167 // but we're working with the DOM through a NodeList (items) which is readonly, so things aren't that simple
168 // A placeholder will be used to keep track of where in the DOM the swap needs to take place
169 // especially with IE which enforces the prohibition on duplicate Ids, so copying nodes is problematic
170 swapItems(items [j], items [j - increment]);
178 increment = Math.floor (increment / 2.2);
181 sortDescending: function ()
183 this.sortAscending();
184 var reverse = function ( children )
186 var parent = children [ 0 ] . parentNode ;
188 if ( children [ 0 ].id == '-blank-' ) // don't include -blank- element in the sort
190 for ( var i = children.length - 1 ; i >= start ; i-- )
192 parent.appendChild ( children [ i ] ) ;
195 reverse ( YAHOO.util.Dom.get("ul1").getElementsByTagName("li") ) ;
197 handleSave:function(){
198 var parseList = function(ul, title) {
199 var items = ul.getElementsByTagName("li");
201 for (i=0;i<items.length;i=i+1) {
202 var name = items[i].id;
203 var value = document.getElementById('input_'+name).value;
204 out[i] = [ name , unescape(value) ];
206 return YAHOO.lang.JSON.stringify(out);
208 var ul1=YAHOO.util.Dom.get("ul1");
209 var hasDeletedItem = false;
210 for(j = 0; j < SimpleList.jstransaction.JSTransactions.length; j++){
211 var liEl = new YAHOO.util.Element(SimpleList.jstransaction.JSTransactions[j]['data']['id']);
212 if(liEl && liEl.hasClass('deleted'))
213 hasDeletedItem = true;
217 if(!confirm(SUGAR.language.get('ModuleBuilder', 'LBL_CONFIRM_SAVE_DROPDOWN')))
221 for(j = 0; j < SimpleList.jstransaction.JSTransactions.length; j++){
222 if(SimpleList.jstransaction.JSTransactions[j]['transaction'] == 'deleteDropDown'){
223 var liEl = new YAHOO.util.Element(SimpleList.jstransaction.JSTransactions[j]['data']['id']);
224 if(liEl && liEl.hasClass('deleted'))
225 ul1.removeChild(liEl.get("element"));
228 var list = document.getElementById('list_value');
230 var out = parseList(ul1, "List 1");
232 ModuleBuilder.refreshDD_name = document.getElementById('dropdown_name').value;
233 if (document.forms.popup_form)
235 ModuleBuilder.handleSave("dropdown_form", ModuleBuilder.refreshDropDown);
239 ModuleBuilder.handleSave("dropdown_form", ModuleBuilder.refreshGlobalDropDown);
242 deleteDropDownValue : function(id, record){
243 var field = new YAHOO.util.Element(id);
245 SimpleList.jstransaction.record('deleteDropDown',{'id': id });
247 if (field.hasClass('deleted'))
248 field.removeClass('deleted');
250 field.addClass('deleted');
252 editDropDownValue : function(id, record){
253 //Close any other dropdown edits
255 SimpleList.endCurrentDropDownEdit();
256 var dispSpan = document.getElementById('span_'+id);
257 var editSpan = document.getElementById('span_edit_'+id);
258 dispSpan.style.display = 'none';
261 editSpan.style.display = 'inline-block';
263 editSpan.style.display = 'inline';
265 var textbox = document.getElementById('input_'+id);
267 SimpleList.lastEdit = id;
269 endCurrentDropDownEdit : function() {
270 if (SimpleList.lastEdit != null)
272 var valueLastEdit = unescape(document.getElementById('input_'+SimpleList.lastEdit).value);
273 SimpleList.setDropDownValue(SimpleList.lastEdit,valueLastEdit,true);
276 setDropDownValue : function(id, val, record){
279 SimpleList.jstransaction.record('changeDropDownValue', {'id':id, 'new':val, 'old':document.getElementById('value_'+ id).value});
281 var dispSpan = document.getElementById('span_'+id);
282 var editSpan = document.getElementById('span_edit_'+id);
283 var textbox = document.getElementById('input_'+id);
285 dispSpan.style.display = 'inline';
286 editSpan.style.display = 'none';
287 dispSpan.innerHTML = "["+val+"]";
288 document.getElementById('value_'+ id).value = val;
289 SimpleList.lastEdit = null; // Bug 14662 - clear the last edit point behind us
291 undoDeleteDropDown : function(transaction){
293 SimpleList.deleteDropDownValue(transaction['id'], false);
295 undoDropDownChange : function(transaction){
296 SimpleList.setDropDownValue(transaction['id'], transaction['old'], false);
298 redoDropDownChange : function(transaction){
299 SimpleList.setDropDownValue(transaction['id'], transaction['new'], false);
302 SimpleList.jstransaction.undo();
305 SimpleList.jstransaction.redo();