1 /*********************************************************************************
2 * SugarCRM 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 ********************************************************************************/
37 if(typeof(SUGAR.collection) == "undefined") {
38 SUGAR.collection = function(form_name, field_name, module, popupData){
41 * boolean variable to handle expand/collapse views
42 * false if the collection field is collapsed and true if the rows are expanded.
44 this.more_status = false;
47 * Store the form name containing this field. Example: EditView
49 this.form = form_name;
52 * Store the name of the collection field. Example: account_name
54 this.field = field_name;
58 * Store the unique form + field name that uses the combination of form and field
60 this.field_element_name = this.form + '_' + this.field;
63 * Store the name of the module from where come the field. Example: Accounts
68 * Number of secondaries linked records (total of linked records - 1).
70 this.fields_count = 0;
73 * Number of extra fields.
75 this.extra_fields_count = 0;
78 * Set to true if it is the initialization.
83 * Name of the primary field. Example: "accounts_collection_0"
85 this.primary_field = "";
88 * Store the row cloned in key "0" and the context cloned in key "1".
90 this.cloneField = new Array();
93 * Store the sqs_objects for the cloned row encoded in JSON.
98 * Store the name and the id of all the secondaries linked records. this is used to create the secondary rows.
100 this.secondaries_values = new Array();
103 * Store all the extra fields which has been updated in the collection field to save on save of the main record.
105 this.update_fields = new Object();
108 * boolean variable indicating whether or not to show the expand/collapse arrow
110 this.show_more_image = true;
114 SUGAR.collection.prototype = {
116 * Remove the row designated by the passed 'id' or clear the row if there is only one row.
118 remove: function(num){
119 // if there is only one record, clear it instead of removing it
120 // this is determined by the visibility of the drop down arrow element
121 var radio_els = this.get_radios();
123 if(radio_els.length == 1) {
124 div_el = document.getElementById(this.field_element_name + '_input_div_' + num);
125 var input_els = div_el.getElementsByTagName('input');
127 input_els[0].value = '';
130 input_els[1].value = '';
132 if(this.primary_field) {
133 div_el = document.getElementById(this.field_element_name + '_radio_div_' + num);
134 radio_els = div_el.getElementsByTagName('input');
135 //Clear the radio field
136 radio_els[0].checked = false;
139 div_el = document.getElementById(this.field_element_name + '_input_div_' + num);
141 div_el = document.getElementById(this.field_element_name + '_radio_div_' + num);
142 var tr_to_remove = document.getElementById('lineFields_' + this.field_element_name + '_' + num);
143 div_el.parentNode.parentNode.parentNode.removeChild(tr_to_remove);
145 var div_id = 'lineFields_' + this.field_element_name + '_' + num;
146 if (typeof sqs_objects[div_id.replace("_field_", "_")] != 'undefined') {
147 delete (sqs_objects[div_id.replace("_field_", "_")]);
150 for(var k=0; k< radio_els.length; k++){
151 if(radio_els[k].checked){
155 // If we remove an entry marked as the primary, set another record as the primary
156 var primary_checked = document.forms[this.form].elements[this.field+"_allowed_to_check"];
157 var allowed_to_check = true;
158 if(primary_checked && primary_checked.value == 'false'){
159 allowed_to_check = false;
161 if(/EditView/.test(this.form) && !checked && typeof radio_els[0] != 'undefined' && allowed_to_check) {
162 radio_els[0].checked = true;
163 this.changePrimary(true);
167 // if there is now only one record, hide the "more..." link
168 if(radio_els.length == 1){
169 this.more_status = false;
170 if (document.getElementById('more_'+this.field_element_name) && document.getElementById('more_'+this.field_element_name).style.display != 'none') {
171 document.getElementById('more_'+this.field_element_name).style.display='none';
174 this.show_arrow_label(false);
183 get_radios: function() {
184 return YAHOO.util.Selector.query('input[name^=primary]', document.getElementById(this.field_element_name+'_table'));
188 * Add a new empty row.
190 add: function(values){
192 var Field0 = this.init_clone(values);
193 this.cloneField[1].appendChild(Field0);
194 //Enable quicksearch for this field
196 this.changePrimary(false);
198 //If the arrow field and label are collapsed, un-collapse it
199 if(document.getElementById('more_'+this.field_element_name) && document.getElementById('more_'+this.field_element_name).style.display == 'none'){
200 document.getElementById('more_'+this.field_element_name).style.display='';
203 if(!this.is_expanded()) {
205 this.show_arrow_label(true);
210 * Add the secondaries rows on load of the page.
212 add_secondaries: function(){
213 clone_id = this.form + '_' + this.field + '_collection_0';
215 if(typeof sqs_objects == 'undefined' || typeof sqs_objects[clone_id] == 'undefined') {
216 setTimeout('collection["'+this.field_element_name+'"].add_secondaries();',1000);
217 } else if(typeof document.getElementById(this.form + '_' + this.field + '_collection_0') == 'undefined'){
218 setTimeout('collection["'+this.field_element_name+'"].add_secondaries();',1000);
222 this.changePrimary(true);
223 for(key in this.secondaries_values){
224 if (isInteger(key)) {
225 this.add(this.secondaries_values[key]);
231 // Update the "hash" of the unchanged form, because this is just adding data, not actually changing anything
232 initEditView(document.forms[this.form]);
235 * Create the new row from a cloned row.
237 init_clone: function(values){
239 //Safety check, this means that the clone field was not created yet
240 if(typeof this.cloneField[0] == 'undefined') {
244 if (typeof values == "undefined") {
245 values = new Array();
250 var count = this.fields_count;
252 //Clone the table element containing the fields for each row, use safe_clone uder IE to prevent events from being cloned
253 Field0 = SUGAR.isIE ?
254 SUGAR.collection.safe_clone(this.cloneField[0], true) :
255 this.cloneField[0].cloneNode(true);
257 Field0.id = "lineFields_"+this.field_element_name+"_"+count;
259 for ( var ii = 0; ii < Field0.childNodes.length; ii++ ){
260 if(typeof(Field0.childNodes[ii].tagName) != 'undefined' && Field0.childNodes[ii].tagName == "TD") {
261 for (var jj = 0; jj < Field0.childNodes[ii].childNodes.length; jj++) {
262 currentNode = Field0.childNodes[ii].childNodes[jj];
263 this.process_node(Field0.childNodes[ii], currentNode, values);
272 * method to process cloning of nodes, moved out of init_clone so that
273 * this may be recursively called
275 process_node: function(parentNode, currentNode, values) {
276 if(parentNode.className == 'td_extra_field'){
277 // If this is an extra field
281 var toreplace = this.field + "_collection_extra_0";
282 var re = new RegExp(toreplace, 'g');
283 parentNode.innerHTML = parentNode.innerHTML.replace(re, this.field + "_collection_extra_" + this.fields_count);
284 } else if (currentNode.tagName && currentNode.tagName == 'SPAN') {
285 //If it is our div element, recursively find all input elements to process
286 currentNode.id = /_input/.test(currentNode.id) ? this.field_element_name + '_input_div_' + this.fields_count : this.field_element_name + '_radio_div_' + this.fields_count;
287 if (/_input/.test(currentNode.id)) {
288 currentNode.name = 'teamset_div';
291 var input_els = currentNode.getElementsByTagName('input');
292 for ( var x = 0; x < input_els.length; x++ ){
293 if(input_els[x].tagName && input_els[x].tagName == 'INPUT') {
294 this.process_node(parentNode, input_els[x], values);
297 } else if (currentNode.name) {
298 // If this is a standard field
299 var toreplace = this.field + "_collection_0";
300 var re = new RegExp(toreplace, 'g');
301 var name = currentNode.name;
302 var new_name = name.replace(re, this.field + "_collection_" + this.fields_count);
306 var sqs_id = this.form + '_' + new_name;
307 if (typeof this.sqs_clone != 'undefined') {
308 var sqs_clone = YAHOO.lang.JSON.stringify(this.sqs_clone);
309 eval('sqs_objects[sqs_id]=' + sqs_clone);
311 for (var pop_field in sqs_objects[sqs_id]['populate_list']) {
312 if (typeof sqs_objects[sqs_id]['populate_list'][pop_field] == 'string') {
313 sqs_objects[sqs_id]['populate_list'][pop_field] = sqs_objects[sqs_id]['populate_list'][pop_field].replace(RegExp('_0', 'g'), "_" + this.fields_count);
316 for (var req_field in sqs_objects[sqs_id]['required_list']) {
317 if (typeof sqs_objects[sqs_id]['required_list'][req_field] == 'string') {
318 sqs_objects[sqs_id]['required_list'][req_field] = sqs_objects[sqs_id]['required_list'][req_field].replace(RegExp('_0', 'g'), "_" + this.fields_count);
323 currentNode.name = new_name;
324 currentNode.id = new_name;
325 currentNode.value = values['name'];
327 case "id_" + toreplace:
328 currentNode.name = new_name.replace(RegExp('_0', 'g'), "_" + this.fields_count);
329 currentNode.id = new_name.replace(RegExp('_0', 'g'), "_" + this.fields_count);
330 currentNode.value = values['id'];
332 case "btn_" + toreplace:
333 currentNode.name = new_name;
334 currentNode.attributes['onclick'].value = currentNode.attributes['onclick'].value.replace(re, this.field + "_collection_" + this.fields_count);
335 currentNode.attributes['onclick'].value = currentNode.attributes['onclick'].value.replace(RegExp(this.field + "_collection_extra_0", 'g'), this.field + "_collection_extra_" + this.fields_count);
337 case "allow_new_value_" + toreplace:
338 currentNode.name = new_name;
339 currentNode.id = new_name;
341 case "remove_" + toreplace:
342 currentNode.name = new_name;
343 currentNode.id = new_name;
344 currentNode.setAttribute('collection_id', this.field_element_name);
345 currentNode.setAttribute('remove_id', this.fields_count);
346 currentNode.onclick = function() {
347 collection[this.getAttribute('collection_id')].remove(this.getAttribute('remove_id'));
350 case "primary_" + this.field + "_collection":
351 currentNode.id = new_name;
352 currentNode.value = this.fields_count;
353 currentNode.checked = false; //Firefox
354 currentNode.setAttribute('defaultChecked', '');
357 alert(toreplace + '|' + currentNode.name + '|' + name + '|' + new_name);
365 * Collapse or expand the rows to show for the editview(depending of the this.more_status attribute).
367 js_more: function(val){
368 if(this.show_more_image){
369 var more_ = document.getElementById('more_img_'+this.field_element_name);
370 var arrow = document.getElementById('arrow_'+this.field);
371 var radios = this.get_radios();
372 // if we want to collapse
373 if(this.more_status == false){
374 more_.src = "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=advanced_search.gif";
375 this.more_status = true;
376 // show the primary only and hidde the other one.
377 var hidden_count = 0;
378 for(var k=0; k< radios.length; k++){
379 if (radios[k].type && radios[k].type == 'radio') {
380 if (radios[k].checked) {
381 radios[k].parentNode.parentNode.parentNode.style.display = '';
383 radios[k].parentNode.parentNode.parentNode.style.display = 'none';
388 //rrs - add code to not remove the first field if non if the fields are selected as primary
389 if(hidden_count == radios.length){
390 radios[0].parentNode.parentNode.parentNode.style.display = '';
393 arrow.value = 'hide';
395 more_.src = "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=basic_search.gif";
396 this.more_status=false;
397 // display all the records
398 for(var k=0; k< radios.length; k++){
400 radios[k].parentNode.parentNode.parentNode.style.display='';
404 arrow.value = 'show';
407 var more_div = document.getElementById('more_div_'+this.field_element_name);
409 more_div.innerHTML = arrow.value == 'show' ? SUGAR.language.get('app_strings','LBL_HIDE') : SUGAR.language.get('app_strings','LBL_SHOW');
415 * Create the clone on load of the page and store it in this.cloneField
417 create_clone: function() {
418 var oneField = document.getElementById('lineFields_'+this.field_element_name+'_0');
419 this.cloneField[0] = SUGAR.isIE ?
420 SUGAR.collection.safe_clone(oneField, true) :
421 oneField.cloneNode(true);
422 this.cloneField[1] = oneField.parentNode;
423 this.more_status = true;
424 var clone_id = this.form + '_' + this.field + '_collection_0';
426 if (typeof sqs_objects[clone_id] != 'undefined') {
427 var clone = YAHOO.lang.JSON.stringify(sqs_objects[clone_id]);
428 eval('this.sqs_clone=' + clone);
432 * Validates team set to check if the primary team id has been set or not
434 validateTemSet : function(formname, fieldname) {
435 var table_element_id = formname + '_' + fieldname + '_table';
436 if(document.getElementById(table_element_id)) {
437 var input_elements = YAHOO.util.Selector.query('input[type=radio]', document.getElementById(table_element_id));
438 var has_primary = false;
439 var primary_field_id = fieldname + '_collection_0';
440 for(t in input_elements) {
441 primary_field_id = fieldname + '_collection_' + input_elements[t].value;
442 if(input_elements[t].type && input_elements[t].type == 'radio' && input_elements[t].checked == true) {
443 if(document.forms[formname].elements[primary_field_id].value != '') {
457 * return an array of teamids for a team field
459 getTeamIdsfromUI: function(formname, fieldname) {
460 var team_ids = new Array();
461 var table_element_id = formname + '_' + fieldname + '_table';
462 if(document.getElementById(table_element_id)) {
463 input_elements = YAHOO.util.Selector.query('input[type=hidden]', document.getElementById(table_element_id));
464 for(t = 0; t < input_elements.length; t++) {
465 if (input_elements[t].id.match("id_" + fieldname + "_collection_") != null) {
466 team_ids.push(input_elements[t].value);
473 * return a primary team id
475 getPrimaryTeamidsFromUI: function(formname, fieldname) {
476 var table_element_id = formname + '_' + fieldname + '_table';
477 if(document.getElementById(table_element_id)) {
478 var input_elements = YAHOO.util.Selector.query('input[type=radio]', document.getElementById(table_element_id));
479 for(t in input_elements) {
480 var primary_field_id = 'id_' + document.forms[formname][fieldname].name + '_collection_' + input_elements[t].value;
481 if(input_elements[t].type && input_elements[t].type == 'radio' && input_elements[t].checked == true) {
482 if(document.forms[formname].elements[primary_field_id].value != '') {
483 return document.forms[formname].elements[primary_field_id].value;
491 * Change the primary row onchange of the radio button.
493 changePrimary: function(noAdd){
494 var old_primary = this.primary_field;
495 var radios=this.get_radios();
496 for (var k = 0; k < radios.length; k++) {
497 var qs_id = radios[k].id.replace('primary_','');
498 if (radios[k].checked) {
499 this.primary_field = qs_id;
501 qs_id = qs_id + '_' + k;
504 qs_id = this.form + '_' + qs_id;
506 if(typeof sqs_objects[qs_id] != 'undefined' && sqs_objects[qs_id]['primary_field_list']){
507 for (var ii = 0; ii < sqs_objects[qs_id]['primary_field_list'].length; ii++) {
508 if (radios[k].checked && qs_id != old_primary) {
509 sqs_objects[qs_id]['field_list'].push(sqs_objects[qs_id]['primary_field_list'][ii]);
510 sqs_objects[qs_id]['populate_list'].push(sqs_objects[qs_id]['primary_populate_list'][ii]);
511 }else if(old_primary == qs_id && !radios[k].checked){
512 sqs_objects[qs_id]['field_list'].pop();
513 sqs_objects[qs_id]['populate_list'].pop();
525 * Collapse or expand the rows to show for the detailview.
527 js_more_detail: function(id){
528 var more_img = document.getElementById('more_img_'+id);
529 if(more_img.style.display == 'inline'){
530 more_img.src = "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=advanced_search.gif";
532 more_img.src = "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=basic_search.gif";
536 * Replace the first field with the specified values
538 replace_first: function(values){
539 for (var i = 0; i <= this.fields_count; i++) {
540 var div_el = document.getElementById(this.field_element_name + '_input_div_' + i);
542 var name_field = document.getElementById(this.field+"_collection_" + i);
543 var id_field = document.getElementById("id_"+this.field+"_collection_" + i);
544 name_field.value = values['name'];
545 id_field.value = values['id'];
551 * Remove all empty fields from the widget.
553 clean_up: function(){
554 //clean up any rows that have been added but do not contain any data
555 var divsToClean = new Array();
556 var isFirstFieldEmpty = false;
558 for (var i = 0; i <= this.fields_count; i++) {
559 var div_el = document.getElementById(this.field_element_name + '_input_div_' + i);
561 input_els = div_el.getElementsByTagName('input');
562 for ( var x = 0; x < input_els.length; x++ ){
563 if(input_els[x].id && input_els[x].id == (this.field + '_collection_' + i) && trim(input_els[x].value) == '') {
565 isFirstFieldEmpty = true;
576 for(var j = 0; j < divsToClean.length; j++){
577 this.remove(divsToClean[j]);
579 return isFirstFieldEmpty;
582 show_arrow_label: function(show) {
583 var more_div = document.getElementById('more_div_'+this.field_element_name);
585 more_div.style.display = show ? '' : 'none';
591 * helper function to determine whether or not the widget is expanded (all teams are shown)
593 is_expanded: function() {
594 var more_div = document.getElementById('more_div_'+this.field_element_name);
596 return more_div.style.display == '';
602 SUGAR.collection.safe_clone = function(e, recursive)
604 if (e.nodeName == "#text")
606 return document.createTextNode(e.data);
608 if(!e.tagName) return false;
610 var newNode = document.createElement(e.tagName);
611 if (!newNode) return false;
613 var properties = ['class', 'style', 'name', 'type', 'valign', 'border', 'width', 'height', 'top', 'bottom', 'left', 'right', 'scope', 'row', 'columns', 'src', 'href', 'className', 'align', 'nowrap'];
614 for (var i in properties)
616 if (e[properties[i]])
618 if ((properties[i] != 'style' || !SUGAR.isIE) &&
619 //Only <a> and <iframe> tags can have hrefs
620 (properties[i] != 'href' || e.tagName == 'a' || e.tagName == 'iframe'))
621 newNode[properties[i]] = e[properties[i]];
626 for (var i in e.childNodes)
628 if(e.childNodes[i].nodeName && (!e.className || e.className != "yui-ac-container"))
630 var child = SUGAR.collection.safe_clone(e.childNodes[i], true);
631 if (child) newNode.appendChild(child);