]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/modules/Studio/studio.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / modules / Studio / studio.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
37
38 var yahooSlots = new Array();
39 function addNewRowToView(id){
40     var curRow = document.getElementById(id);
41     var parent = curRow.parentNode;
42     var newRow = document.createElement('tr');
43     var newRow = parent.insertRow(parent.rows.length);
44     var re = /studiorow[0-9]+/g;
45     var cell = newRow.insertCell(0);
46
47     cell.innerHTML = curRow.cells[0].innerHTML.replace(re, 'studiorow' + slotCount);
48     cell.className = curRow.cells[0].className;
49     for(var j = 1; j < curRow.cells.length ; j++){
50         var cell = newRow.insertCell(j);
51         cell.innerHTML = '&nbsp;';
52         cell.className = curRow.cells[j].className;
53     }
54     var index = parent.rows.length;
55     for(var i = 0; i < parent.rows.length ; i++){
56         if(parent.rows[i].id == id){
57             index = i + 1;
58         }
59     }
60     newRow.id = 'studiorow' + slotCount;
61     if(typeof(curRow.parentId) == 'undefined'){
62         newRow.parentId = id;
63     }else{
64         newRow.parentId = curRow.parentId;
65     }
66     if(index < parent.rows.length){
67         parent.insertBefore(newRow, parent.rows[index]);
68     }else{
69         parent.appendChild(newRow);
70     }
71     document.getElementById('add_' + newRow.parentId).value = 1 + parseInt(document.getElementById('add_' + newRow.parentId).value);
72     slotCount++;
73 }
74
75 function deleteRowFromView(id, index){
76     var curRow = document.getElementById(id);
77     curRow.parentNode.removeChild(curRow);
78     if(typeof(curRow.parentId) == 'undefined'){
79         document.getElementById('form_' + id).value=-1;
80     }else{
81         document.getElementById('add_' + curRow.parentId).value =  parseInt(document.getElementById('add_' + curRow.parentId).value) - 1;
82     }
83 }
84
85 function addNewColToView(id, index){
86     
87     var curCol = document.getElementById(id);
88     var index = curCol.cellIndex;
89     var parent = curCol.parentNode;
90     var cell = parent.insertCell(index + 1);
91     if(parent.parentNode.rows[parent.rowIndex + 1])parent.parentNode.rows[parent.rowIndex + 1].insertCell(index + 1)
92     var re = /studiocol[0-9]+/g;
93     cell.innerHTML = '[NEW]';
94     cell.className = curCol.className;
95     if(typeof(curCol.parentId) == 'undefined'){
96         cell.parentId = id;
97     }else{
98         cell.parentId = curCol.parentId;
99     }
100   
101     document.getElementById('add_' + cell.parentId).value = 1 + parseInt(document.getElementById('add_' + cell.parentId).value);
102     slotCount++;
103 }
104
105 function deleteColFromView(id, index){
106     var curCol = document.getElementById(id);
107     var row = curCol.parentNode;
108      var index = curCol.cellIndex;
109     if(typeof(row.cells[index + 1].parentId) == 'undefined'){
110        row.deleteCell(index);
111        row.deleteCell(index - 1);
112        if(row.parentNode.rows[row.rowIndex + 1]){
113         row.parentNode.rows[row.rowIndex + 1].deleteCell(index );
114          row.parentNode.rows[row.rowIndex + 1].deleteCell(index - 1);
115        }
116       
117       
118     }else{
119        row.deleteCell(index + 1);
120         if(row.parentNode.rows[row.rowIndex + 1])row.parentNode.rows[row.rowIndex + 1].deleteCell(index +1);
121         
122     }
123      document.getElementById('add_' + curCol.id).value =  parseInt(document.getElementById('add_' + curCol.id).value) - 1;
124     
125 }
126
127
128
129
130 var field_count_MSI = 0;
131 var studioLoaded = false;
132 var dyn_field_count = 0;
133 function addNewFieldType(type){
134     var select = document.getElementById('studio_display_type').options;
135     for(var i = 0; i < select.length; i++){
136         if(select[i].value == type){
137             return;
138         }
139     }
140     select[i] = new Option(type, type);
141 }
142
143 function filterStudioFields(type){
144     var table = document.getElementById('studio_fields');
145     for(var i = 0; i < table.rows.length; i++){
146         children = table.rows[i].cells[0].childNodes;
147         for(var j = 0; j < children.length; j++){
148             child = children[j];
149             if(child.nodeName == 'DIV' && typeof(child.fieldType) != 'undefined'){
150                 if(type == 'all'){
151                     table.rows[i].style.display = '';
152                 }else if(type == 'custom'){
153                     if(child.isCustom){
154                         table.rows[i].style.display = ''
155                     }else{
156                         table.rows[i].style.display = 'none';
157                     }
158                 }else{
159                     if(child.fieldType == type){
160                         table.rows[i].style.display = ''
161                     }else{
162                         table.rows[i].style.display = 'none';
163                     }
164
165                 }
166             }
167         }
168     }
169
170 }
171
172
173 function addNewField(id, name, label, html, fieldType,isCustom, table_id, top){
174     
175     html = replaceAll(html, "&qt;", '"');
176     html = replaceAll(html, "&sqt;", "'");
177     var table = document.getElementById(table_id);
178     var row = false;
179     if(top){
180          row = table.insertRow(1);
181     }else{
182          row = table.insertRow(table.rows.length);
183     }
184    
185     var cell = row.insertCell(0);
186     var div = document.createElement('div');
187     div.className = 'slot';
188     div.setAttribute('id', id);
189     div.fieldType = fieldType;
190     addNewFieldType(fieldType);
191     div.isCustom = isCustom;
192     div.style.width='100%';
193     var textEl = document.createElement('input');
194     textEl.setAttribute('type', 'hidden')
195     textEl.setAttribute('name',  'slot_field_' + field_count_MSI );
196     textEl.setAttribute('id', 'slot_field_' + field_count_MSI  );
197     textEl.setAttribute('value', 'add:' + name );
198     field_list_MSI['form_' + name] = textEl;
199     document.studio.appendChild(textEl);
200     div.innerHTML = label;
201     var cell2 = row.insertCell(1);
202     var div2 = document.createElement('div');
203     setMouseOverForField(div, true);
204     div2.style.display = 'none';
205     div2.setAttribute('id',  id + 'b' );
206     html = html.replace(/(<input)([^>]*)/g, '$1 disabled readonly $2');
207     html = html.replace(/(<select)([^>]*)/g, '$1 disabled readonly $2');
208     html = html.replace(/(onclick=')([^']*)/g, '$1'); // to strip {} from after a JS onclick call
209     div2.innerHTML += html;
210     cell.appendChild(div);
211     cell2.appendChild(div2);
212     field_count_MSI++;
213     if(top){
214         yahooSlots[id] = new ygDDSlot(id, "studio");
215     }else{
216         dyn_field_count++;
217     }
218     return name;
219
220 }
221
222
223 function removeFieldFromTable(field, table)
224 {
225     var table = document.getElementById(table);
226     var rows = table.rows;
227     for(i = 0 ; i < rows.length; i++){
228         cells = rows[i].cells;
229         for(j = 0; j < cells.length; j++){
230             cell = rows[i].cells[j];
231             children = cell.childNodes;
232             for(k = 0; k < children.length; k++){
233                 child = children[k];
234                 if(child.nodeType == 1){
235
236                     if(child.getAttribute('id') == 'slot_' + field){
237                         table.deleteRow(i);
238                         return;
239                     }
240                 }
241             }
242         }
243     }
244 }
245 function setMouseOverForField(field, on){
246
247     if(on){
248         field.onmouseover = function(){
249                 
250                 $(this).tipTip({maxWidth: "auto", edgeOffset: 10, content: document.getElementById(this.id + 'b').innerHTML});
251
252         };
253         field.onmouseout = function(){
254             return nd();
255         };
256     }else{
257         field.onmouseover = function(){};
258         field.onmouseout = function(){};
259     }
260 }
261 var lastIDClick = '';
262 var lastIDClickTime = 0;
263 var dblDelay = 500;
264 function wasDoubleClick(id) {
265     var d = new Date();
266     var now = d.getTime();
267
268     if (lastIDClick == id && (now - lastIDClickTime) < dblDelay) {
269         lastIDClick = '';
270
271         return true;
272     }
273     lastIDClickTime = now;
274     lastIDClick = id;
275     return false;
276 }
277 function confirmNoSave(){
278     return confirm(SUGAR.language.get('Studio', 'LBL_CONFIRM_UNSAVE'));
279 }
280 var labelEdit = false;
281  SUGAR.Studio = function(){
282     this.labelEdit = false;
283     this.lastLabel = false;
284 }
285  SUGAR.Studio.prototype.endLabelEdit =  function(id){
286      if(id == 'undefined')return;
287     document.getElementById('span' + id).style.display = 'none';
288     jstransaction.record('studioLabelEdit', {'id':id, 'new': document.getElementById(id).value , 'old':document.getElementById('label' + id).innerHTML});
289     document.getElementById('label' + id).innerHTML = document.getElementById(id).value;
290     document.getElementById('label_' + id).value = document.getElementById(id).value;
291      document.getElementById('label' + id).style.display = '';
292     this.labelEdit = false;
293     YAHOO.util.DragDropMgr.unlock();
294 };
295
296  SUGAR.Studio.prototype.undoLabelEdit =  function (transaction){
297     var id = transaction['id'];
298     document.getElementById('span' + id).style.display = 'none';
299     document.getElementById('label' + id).innerHTML = transaction['old'];
300     document.getElementById('label_' + id).value = transaction['old'];
301 };
302  SUGAR.Studio.prototype.redoLabelEdit=  function  (transaction){
303     var id = transaction['id'];
304     document.getElementById('span' + id).style.display = 'none';
305     document.getElementById('label' + id).innerHTML = transaction['new'];
306     document.getElementById('label_' + id).value = transaction['new'];
307 };
308
309  SUGAR.Studio.prototype.handleLabelClick =  function(id, count){
310     if(this.lastLabel != ''){
311         //endLabelEdit(lastLabel);
312     }
313     if(wasDoubleClick(id) || count == 1){
314         document.getElementById('span' + id).style.display = '';
315         document.getElementById(id).focus();
316         document.getElementById(id).select();
317         document.getElementById('label' + id).style.display = 'none';
318         this.lastLabel = id;
319         YAHOO.util.DragDropMgr.lock();
320     }
321     
322     
323
324 }
325 jstransaction.register('studioLabelEdit', SUGAR.Studio.prototype.undoLabelEdit, SUGAR.Studio.prototype.redoLabelEdit);
326
327
328 SUGAR.Studio.prototype.save = function(formName, publish){
329     ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));
330     var formObject = document.forms[formName]; 
331     YAHOO.util.Connect.setForm(formObject); 
332     var cObj = YAHOO.util.Connect.asyncRequest('POST','index.php',
333             {success: SUGAR.Studio.prototype.saved, failure: SUGAR.Studio.prototype.saved,timeout:5000, argument: publish});
334 }
335 SUGAR.Studio.prototype.saved= function(o){
336     if(o){
337     ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVED'));
338     window.setTimeout('ajaxStatus.hideStatus();', 2000);
339     
340     if(o.argument){
341        
342         studiojs.publish();
343     }else{
344         document.location.reload();
345     }
346     }else{
347         ajaxStatus.showStatus(SUGAR.language.get('Studio', 'LBL_FAILED_TO_SAVE'));
348         window.setTimeout('ajaxStatus.hideStatus();', 2000);
349     }
350 }
351     
352 SUGAR.Studio.prototype.publish = function(){
353     ajaxStatus.showStatus(SUGAR.language.get('Studio', 'LBL_PUBLISHING'));
354     var cObj = YAHOO.util.Connect.asyncRequest('GET','index.php?to_pdf=1&module=Studio&action=Publish',
355             {success: SUGAR.Studio.prototype.published, failure: SUGAR.Studio.prototype.published}, null);
356 }
357
358 SUGAR.Studio.prototype.published= function(o){
359     if(o){
360     ajaxStatus.showStatus(SUGAR.language.get('Studio', 'LBL_PUBLISHED'));
361     window.setTimeout('ajaxStatus.hideStatus();', 2000);
362     document.location.reload();
363     }else{
364         ajaxStatus.showStatus(SUGAR.language.get('Studio', 'LBL_FAILED_PUBLISHED'));
365         window.setTimeout('ajaxStatus.hideStatus();', 2000);
366     }
367     }
368
369 var studiopopup = function() {
370     return {
371         // covers the page w/ white overlay
372         display: function() {
373             if(studiojs.popupVisible)return false;
374            studiojs.popupVisible = true;
375             var cObj = YAHOO.util.Connect.asyncRequest('GET','index.php?to_pdf=1&module=Studio&action=wizard&wizard=EditCustomFieldsWizard&option=CreateCustomFields&popup=true',
376             {success: studiopopup.render, failure: studiopopup.render}, null);
377
378
379       
380
381         },
382         destroy:function(){
383             studiojs.popup.hide();
384         },
385         evalScript:function(text){
386             SUGAR.util.evalScript(text);
387              
388         },
389         render: function(obj){
390             if(obj){
391                 
392                 studiojs.popup = new YAHOO.widget.Dialog("dlg", {  effect:{effect:YAHOO.widget.ContainerEffect.SLIDE,duration:.5}, fixedcenter: false, constraintoviewport: false, underlay:"shadow",modal:true, close:true, visible:false, draggable:true, monitorresize:true} );
393                 
394                 studiojs.popup.setBody(obj.responseText); 
395                 studiojs.popupAvailable = true;
396                     studiojs.popup.render(document.body);
397                     studiojs.popup.center();
398                     studiojs.popup.beforeHideEvent.fire = function(e){
399                         studiojs.popupVisible = false;
400                     }
401                       studiopopup.evalScript(obj.responseText);
402                 
403                 
404             }
405             
406         }
407         
408
409     };
410 }();
411 var studiojs = new SUGAR.Studio();
412 studiojs.popupAvailable = false;
413 studiojs.popupVisible = false;
414
415
416
417
418
419 var popupSave = function(o){
420     var errorIndex = o.responseText.indexOf('[ERROR]');
421     
422     if(errorIndex > -1){
423         var error = o.responseText.substr(errorIndex + 7, o.responseText.length);
424                 ajaxStatus.showStatus(error);
425         window.setTimeout('ajaxStatus.hideStatus();', 2000);
426         return;
427     }
428     var typeIndex = o.responseText.indexOf('[TYPE]') ;
429    var labelIndex = o.responseText.indexOf('[LABEL]') ;
430     var dataIndex = o.responseText.indexOf('[DATA]');
431     var errorIndex = o.responseText.indexOf('[ERROR]');
432     var name = o.responseText.substr(6, typeIndex - 6);
433     var type =  o.responseText.substr(typeIndex + 6,labelIndex - (typeIndex + 6));
434    var label =  o.responseText.substr(labelIndex + 7,dataIndex - (labelIndex + 7));
435    var data = o.responseText.substr(dataIndex + 6, o.responseText.length);
436   
437      addNewField('dyn_field_' + field_count_MSI, name, label, data, type, 1, 'studio_fields', true)
438    
439    
440 };
441 function submitCustomFieldForm(isPopup){
442                 
443     if(typeof(document.popup_form.presave) != 'undefined'){
444         document.popup_form.presave();
445     }
446    
447     if(!check_form('popup_form'))return;
448     if(isPopup){
449         
450         
451         var callback =
452         {
453             success: popupSave ,
454             failure: popupSave ,
455             argument: ''
456         }
457         YAHOO.util.Connect.setForm('popup_form');
458         var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback);
459         studiopopup.destroy();
460     }else{
461         
462         document.popup_form.submit();
463     }
464 }
465
466 function deleteCustomFieldForm(isPopup){
467                 
468    
469    
470     if(confirm("WARNING\nDeleting a custom field will delete all data related to that custom field. \nYou will still need to remove the field from any layouts you have added it to.")){
471         document.popup_form.option.value = 'DeleteCustomField';
472                 document.popup_form.submit();
473     }
474 }
475
476 function dropdownChanged(value){
477     if(typeof(app_list_strings[value]) == 'undefined')return;
478     var select = document.getElementById('default_value').options;
479     select.length = 0;
480
481     var count = 0;
482     for(var key in app_list_strings[value]){
483         select[count] = new Option(app_list_strings[value][key], key);
484         count++;
485     }
486 }
487
488 function customFieldChanged(){
489 }
490
491 var populateCustomField = function(response){
492     var div = document.getElementById('customfieldbody');
493     if(response.status = 0){
494         div.innerHTML = 'Server Connection Failed';
495     }else{
496                 validate['popup_form'] = new Array();
497                 inputsWithErrors = new Array();
498         div.innerHTML = response.responseText;
499         studiopopup.evalScript(response.responseText);
500         if(studiojs.popupAvailable){
501            
502         var region = YAHOO.util.Dom.getRegion('custom_field_table') ;
503         studiojs.popup.cfg.setProperty('width', region.right - region.left + 30 + 'px');
504         studiojs.popup.cfg.setProperty('height', region.bottom - region.top + 30 + 'px');
505        
506          studiojs.popup.render(document.body);
507         studiojs.popup.center();
508         studiojs.popup.show();
509         }
510       
511     }
512 };
513 var populateCustomFieldCallback = {
514     success: populateCustomField ,
515     failure: populateCustomField,
516     argument: 1
517 };
518 var COBJ = false;
519 function changeTypeData(type){
520     document.getElementById('customfieldbody').innerHTML = '<h2>Loading...</h2>';
521     COBJ = YAHOO.util.Connect.asyncRequest('GET','index.php?module=Studio&popup=true&action=index&&ajax=editcustomfield&to_pdf=true&type=' + type,populateCustomFieldCallback,null);
522
523 }
524
525 function typeChanged(obj)
526 {
527     changeTypeData(obj.options[obj.selectedIndex].value);
528
529 }
530
531 function handle_duplicate(){
532     document.popup_form.action.value  = 'EditView';
533     document.popup_form.duplicate.value = 'true';
534     document.popup_form.submit();
535 }
536
537 function forceRange(field, min, max){
538         field.value = parseInt(field.value);
539         if(field.value == 'NaN')field.value = max;
540         if(field.value > max) field.value = max;
541         if(field.value < min) field.value = min;
542 }
543 function changeMaxLength(field, length){
544         field.maxLength = parseInt(length);
545         field.value = field.value.substr(0, field.maxLength);
546 }
547
548
549