| 1 | /* |
| 2 | +--------------------------------------------------------------------+ |
| 3 | | CiviCRM version 4.4 | |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | This file is a part of CiviCRM. | |
| 8 | | | |
| 9 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 10 | | under the terms of the GNU Affero General Public License | |
| 11 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 12 | | | |
| 13 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 16 | | See the GNU Affero General Public License for more details. | |
| 17 | | | |
| 18 | | You should have received a copy of the GNU Affero General Public | |
| 19 | | License and the CiviCRM Licensing Exception along | |
| 20 | | with this program; if not, contact CiviCRM LLC | |
| 21 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 22 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 24 | +--------------------------------------------------------------------+ |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * @file: global functions for CiviCRM |
| 29 | * FIXME: We are moving away from using global functions. DO NOT ADD MORE. |
| 30 | * @see CRM object - the better alternative to adding global functions |
| 31 | */ |
| 32 | |
| 33 | var CRM = CRM || {}; |
| 34 | var cj = jQuery; |
| 35 | |
| 36 | /** |
| 37 | * Short-named function for string translation, defined in global scope so it's available everywhere. |
| 38 | * |
| 39 | * @param $text string string for translating |
| 40 | * @param $params object key:value of additional parameters |
| 41 | * |
| 42 | * @return string the translated string |
| 43 | */ |
| 44 | function ts(text, params) { |
| 45 | "use strict"; |
| 46 | text = CRM.strings[text] || text; |
| 47 | if (typeof(params) === 'object') { |
| 48 | for (var i in params) { |
| 49 | if (typeof(params[i]) === 'string' || typeof(params[i]) === 'number') { |
| 50 | // sprintf emulation: escape % characters in the replacements to avoid conflicts |
| 51 | text = text.replace(new RegExp('%' + i, 'g'), String(params[i]).replace(/%/g, '%-crmescaped-')); |
| 52 | } |
| 53 | } |
| 54 | return text.replace(/%-crmescaped-/g, '%'); |
| 55 | } |
| 56 | return text; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * This function is called by default at the bottom of template files which have forms that have |
| 61 | * conditionally displayed/hidden sections and elements. The PHP is responsible for generating |
| 62 | * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to |
| 63 | * this function. |
| 64 | * |
| 65 | * @access public |
| 66 | * @param showBlocks Array of element Id's to be displayed |
| 67 | * @param hideBlocks Array of element Id's to be hidden |
| 68 | * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...) |
| 69 | * @return none |
| 70 | */ |
| 71 | function on_load_init_blocks(showBlocks, hideBlocks, elementType) { |
| 72 | if (elementType == null) { |
| 73 | var elementType = 'block'; |
| 74 | } |
| 75 | |
| 76 | /* This loop is used to display the blocks whose IDs are present within the showBlocks array */ |
| 77 | for (var i = 0; i < showBlocks.length; i++) { |
| 78 | var myElement = document.getElementById(showBlocks[i]); |
| 79 | /* getElementById returns null if element id doesn't exist in the document */ |
| 80 | if (myElement != null) { |
| 81 | myElement.style.display = elementType; |
| 82 | } |
| 83 | else { |
| 84 | alert('showBlocks array item not in .tpl = ' + showBlocks[i]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */ |
| 89 | for (var i = 0; i < hideBlocks.length; i++) { |
| 90 | var myElement = document.getElementById(hideBlocks[i]); |
| 91 | /* getElementById returns null if element id doesn't exist in the document */ |
| 92 | if (myElement != null) { |
| 93 | myElement.style.display = 'none'; |
| 94 | } |
| 95 | else { |
| 96 | alert('showBlocks array item not in .tpl = ' + hideBlocks[i]); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * This function is called when we need to show or hide a related form element (target_element) |
| 103 | * based on the value (trigger_value) of another form field (trigger_field). |
| 104 | * |
| 105 | * @access public |
| 106 | * @param trigger_field_id HTML id of field whose onchange is the trigger |
| 107 | * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field |
| 108 | * @param target_element_id HTML id of element to be shown or hidden |
| 109 | * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row') |
| 110 | * @param field_type Type of element radio/select |
| 111 | * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match |
| 112 | * @return none |
| 113 | */ |
| 114 | function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert) { |
| 115 | if (target_element_type == null) { |
| 116 | var target_element_type = 'block'; |
| 117 | } |
| 118 | else { |
| 119 | if (target_element_type == 'table-row') { |
| 120 | var target_element_type = ''; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (field_type == 'select') { |
| 125 | var trigger = trigger_value.split("|"); |
| 126 | var selectedOptionValue = document.getElementById(trigger_field_id).options[document.getElementById(trigger_field_id).selectedIndex].value; |
| 127 | |
| 128 | var target = target_element_id.split("|"); |
| 129 | for (var j = 0; j < target.length; j++) { |
| 130 | if (invert) { |
| 131 | cj('#' + target[j]).show(); |
| 132 | } |
| 133 | else { |
| 134 | cj('#' + target[j]).hide(); |
| 135 | } |
| 136 | for (var i = 0; i < trigger.length; i++) { |
| 137 | if (selectedOptionValue == trigger[i]) { |
| 138 | if (invert) { |
| 139 | cj('#' + target[j]).hide(); |
| 140 | } |
| 141 | else { |
| 142 | cj('#' + target[j]).show(); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | else { |
| 150 | if (field_type == 'radio') { |
| 151 | var target = target_element_id.split("|"); |
| 152 | for (var j = 0; j < target.length; j++) { |
| 153 | if (document.getElementsByName(trigger_field_id)[0].checked) { |
| 154 | if (invert) { |
| 155 | cj('#' + target[j]).hide(); |
| 156 | } |
| 157 | else { |
| 158 | cj('#' + target[j]).show(); |
| 159 | } |
| 160 | } |
| 161 | else { |
| 162 | if (invert) { |
| 163 | cj('#' + target[j]).show(); |
| 164 | } |
| 165 | else { |
| 166 | cj('#' + target[j]).hide(); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * |
| 176 | * Function for checking ALL or unchecking ALL check boxes in a resultset page. |
| 177 | * |
| 178 | * @access public |
| 179 | * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as |
| 180 | * belonging to the resultset's checkbox collection |
| 181 | * @param object - checkbox |
| 182 | * Sample usage: onClick="javascript:changeCheckboxValues('chk_', cj(this) );" |
| 183 | * |
| 184 | * @return |
| 185 | */ |
| 186 | function toggleCheckboxVals(fldPrefix, object) { |
| 187 | if (object.id == 'toggleSelect' && cj(object).is(':checked')) { |
| 188 | cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', true); |
| 189 | } |
| 190 | else { |
| 191 | cj('Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', false); |
| 192 | } |
| 193 | // change the class of selected rows |
| 194 | on_load_init_checkboxes(object.form.name); |
| 195 | } |
| 196 | |
| 197 | function countSelectedCheckboxes(fldPrefix, form) { |
| 198 | fieldCount = 0; |
| 199 | for (i = 0; i < form.elements.length; i++) { |
| 200 | fpLen = fldPrefix.length; |
| 201 | if (form.elements[i].type == 'checkbox' && form.elements[i].name.slice(0, fpLen) == fldPrefix && form.elements[i].checked == true) { |
| 202 | fieldCount++; |
| 203 | } |
| 204 | } |
| 205 | return fieldCount; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Function to enable task action select |
| 210 | */ |
| 211 | function toggleTaskAction(status) { |
| 212 | var radio_ts = document.getElementsByName('radio_ts'); |
| 213 | if (!radio_ts[1]) { |
| 214 | radio_ts[0].checked = true; |
| 215 | } |
| 216 | if (radio_ts[0].checked || radio_ts[1].checked) { |
| 217 | status = true; |
| 218 | } |
| 219 | |
| 220 | var formElements = ['task', 'Go', 'Print']; |
| 221 | for (var i = 0; i < formElements.length; i++) { |
| 222 | var element = document.getElementById(formElements[i]); |
| 223 | if (element) { |
| 224 | if (status) { |
| 225 | element.disabled = false; |
| 226 | } |
| 227 | else { |
| 228 | element.disabled = true; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * This function is used to check if any actio is selected and also to check if any contacts are checked. |
| 236 | * |
| 237 | * @access public |
| 238 | * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as |
| 239 | * belonging to the resultset's checkbox collection |
| 240 | * @param form - name of form that checkboxes are part of |
| 241 | * Sample usage: onClick="javascript:checkPerformAction('chk_', myForm );" |
| 242 | * |
| 243 | */ |
| 244 | function checkPerformAction(fldPrefix, form, taskButton, selection) { |
| 245 | var cnt; |
| 246 | var gotTask = 0; |
| 247 | |
| 248 | // taskButton TRUE means we don't need to check the 'task' field - it's a button-driven task |
| 249 | if (taskButton == 1) { |
| 250 | gotTask = 1; |
| 251 | } |
| 252 | else { |
| 253 | if (document.forms[form].task.selectedIndex) { |
| 254 | //force user to select all search contacts, CRM-3711 |
| 255 | if (document.forms[form].task.value == 13 || document.forms[form].task.value == 14) { |
| 256 | var toggleSelect = document.getElementsByName('toggleSelect'); |
| 257 | if (toggleSelect[0].checked || document.forms[form].radio_ts[0].checked) { |
| 258 | return true; |
| 259 | } |
| 260 | else { |
| 261 | alert("Please select all contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button."); |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | gotTask = 1; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (gotTask == 1) { |
| 270 | // If user wants to perform action on ALL records and we have a task, return (no need to check further) |
| 271 | if (document.forms[form].radio_ts[0].checked) { |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | cnt = (selection == 1) ? countSelections() : countSelectedCheckboxes(fldPrefix, document.forms[form]); |
| 276 | if (!cnt) { |
| 277 | alert("Please select one or more contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button."); |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | else { |
| 282 | alert("Please select an action from the drop-down menu."); |
| 283 | return false; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * This function changes the style for a checkbox block when it is selected. |
| 289 | * |
| 290 | * @access public |
| 291 | * @param chkName - it is name of the checkbox |
| 292 | * @return null |
| 293 | */ |
| 294 | function checkSelectedBox(chkName) { |
| 295 | var checkElement = cj('#' + chkName); |
| 296 | if (checkElement.attr('checked')) { |
| 297 | cj('input[value=ts_sel]:radio').attr('checked', true); |
| 298 | checkElement.parents('tr').addClass('crm-row-selected'); |
| 299 | } |
| 300 | else { |
| 301 | checkElement.parents('tr').removeClass('crm-row-selected'); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * This function is to show the row with selected checkbox in different color |
| 307 | * @param form - name of form that checkboxes are part of |
| 308 | * |
| 309 | * @access public |
| 310 | * @return null |
| 311 | */ |
| 312 | function on_load_init_checkboxes(form) { |
| 313 | var formName = form; |
| 314 | var fldPrefix = 'mark_x'; |
| 315 | for (i = 0; i < document.forms[formName].elements.length; i++) { |
| 316 | fpLen = fldPrefix.length; |
| 317 | if (document.forms[formName].elements[i].type == 'checkbox' && document.forms[formName].elements[i].name.slice(0, fpLen) == fldPrefix) { |
| 318 | checkSelectedBox(document.forms[formName].elements[i].name, formName); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Function to change the color of the class |
| 325 | * |
| 326 | * @param form - name of the form |
| 327 | * @param rowid - id of the <tr>, <div> you want to change |
| 328 | * |
| 329 | * @access public |
| 330 | * @return null |
| 331 | */ |
| 332 | function changeRowColor(rowid, form) { |
| 333 | switch (document.getElementById(rowid).className) { |
| 334 | case 'even-row' : |
| 335 | document.getElementById(rowid).className = 'selected even-row'; |
| 336 | break; |
| 337 | case 'odd-row' : |
| 338 | document.getElementById(rowid).className = 'selected odd-row'; |
| 339 | break; |
| 340 | case 'selected even-row' : |
| 341 | document.getElementById(rowid).className = 'even-row'; |
| 342 | break; |
| 343 | case 'selected odd-row' : |
| 344 | document.getElementById(rowid).className = 'odd-row'; |
| 345 | break; |
| 346 | case 'form-item' : |
| 347 | document.getElementById(rowid).className = 'selected'; |
| 348 | break; |
| 349 | case 'selected' : |
| 350 | document.getElementById(rowid).className = 'form-item'; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * This function is to show the row with selected checkbox in different color |
| 356 | * @param form - name of form that checkboxes are part of |
| 357 | * |
| 358 | * @access public |
| 359 | * @return null |
| 360 | */ |
| 361 | function on_load_init_check(form) { |
| 362 | for (i = 0; i < document.forms[form].elements.length; i++) { |
| 363 | if (( document.forms[form].elements[i].type == 'checkbox' |
| 364 | && document.forms[form].elements[i].checked == true ) |
| 365 | || ( document.forms[form].elements[i].type == 'hidden' |
| 366 | && document.forms[form].elements[i].value == 1 )) { |
| 367 | var ss = document.forms[form].elements[i].id; |
| 368 | var row = 'rowid' + ss; |
| 369 | changeRowColor(row, form); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * reset all the radio buttons with a given name |
| 376 | * |
| 377 | * @param string fieldName |
| 378 | * @param object form |
| 379 | * @return null |
| 380 | */ |
| 381 | function unselectRadio(fieldName, form) { |
| 382 | for (i = 0; i < document.forms[form].elements.length; i++) { |
| 383 | if (document.forms[form].elements[i].name == fieldName) { |
| 384 | document.forms[form].elements[i].checked = false; |
| 385 | } |
| 386 | } |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Function to change button text and disable one it is clicked |
| 392 | * |
| 393 | * @param obj object - the button clicked |
| 394 | * @param formID string - the id of the form being submitted |
| 395 | * @param string procText - button text after user clicks it |
| 396 | * @return null |
| 397 | */ |
| 398 | var submitcount = 0; |
| 399 | /* Changes button label on submit, and disables button after submit for newer browsers. |
| 400 | Puts up alert for older browsers. */ |
| 401 | function submitOnce(obj, formId, procText) { |
| 402 | // if named button clicked, change text |
| 403 | if (obj.value != null) { |
| 404 | obj.value = procText + " ..."; |
| 405 | } |
| 406 | if (document.getElementById) { // disable submit button for newer browsers |
| 407 | obj.disabled = true; |
| 408 | document.getElementById(formId).submit(); |
| 409 | return true; |
| 410 | } |
| 411 | else { // for older browsers |
| 412 | if (submitcount == 0) { |
| 413 | submitcount++; |
| 414 | return true; |
| 415 | } |
| 416 | else { |
| 417 | alert("Your request is currently being processed ... Please wait."); |
| 418 | return false; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | function popUp(URL) { |
| 424 | day = new Date(); |
| 425 | id = day.getTime(); |
| 426 | eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=420,left = 202,top = 184');"); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Function to show / hide the row in optionFields |
| 431 | * |
| 432 | * @param element name index, that whose innerHTML is to hide else will show the hidden row. |
| 433 | */ |
| 434 | function showHideRow(index) { |
| 435 | if (index) { |
| 436 | cj('tr#optionField_' + index).hide(); |
| 437 | if (cj('table#optionField tr:hidden:first').length) { |
| 438 | cj('div#optionFieldLink').show(); |
| 439 | } |
| 440 | } |
| 441 | else { |
| 442 | cj('table#optionField tr:hidden:first').show(); |
| 443 | if (!cj('table#optionField tr:hidden:last').length) { |
| 444 | cj('div#optionFieldLink').hide(); |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Function to check activity status in relavent to activity date |
| 452 | * |
| 453 | * @param element message JSON object. |
| 454 | */ |
| 455 | function activityStatus(message) { |
| 456 | var d = new Date(), time = [], i; |
| 457 | var currentDateTime = d.getTime() |
| 458 | var activityTime = cj("input#activity_date_time_time").val().replace(":", ""); |
| 459 | |
| 460 | //chunk the time in bunch of 2 (hours,minutes,ampm) |
| 461 | for (i = 0; i < activityTime.length; i += 2) { |
| 462 | time.push(activityTime.slice(i, i + 2)); |
| 463 | } |
| 464 | var activityDate = new Date(cj("input#activity_date_time_hidden").val()); |
| 465 | |
| 466 | d.setFullYear(activityDate.getFullYear()); |
| 467 | d.setMonth(activityDate.getMonth()); |
| 468 | d.setDate(activityDate.getDate()); |
| 469 | var hours = time['0']; |
| 470 | var ampm = time['2']; |
| 471 | |
| 472 | if (ampm == "PM" && hours != 0 && hours != 12) { |
| 473 | // force arithmetic instead of string concatenation |
| 474 | hours = hours * 1 + 12; |
| 475 | } |
| 476 | else { |
| 477 | if (ampm == "AM" && hours == 12) { |
| 478 | hours = 0; |
| 479 | } |
| 480 | } |
| 481 | d.setHours(hours); |
| 482 | d.setMinutes(time['1']); |
| 483 | |
| 484 | var activity_date_time = d.getTime(); |
| 485 | |
| 486 | var activityStatusId = cj('#status_id').val(); |
| 487 | |
| 488 | if (activityStatusId == 2 && currentDateTime < activity_date_time) { |
| 489 | if (!confirm(message.completed)) { |
| 490 | return false; |
| 491 | } |
| 492 | } |
| 493 | else { |
| 494 | if (activity_date_time && activityStatusId == 1 && currentDateTime >= activity_date_time) { |
| 495 | if (!confirm(message.scheduled)) { |
| 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | CRM.strings = CRM.strings || {}; |
| 503 | CRM.validate = CRM.validate || { |
| 504 | params: {}, |
| 505 | functions: [] |
| 506 | }; |
| 507 | |
| 508 | (function ($, undefined) { |
| 509 | "use strict"; |
| 510 | $(document).ready(function () { |
| 511 | $().crmtooltip(); |
| 512 | $('.crm-container table.row-highlight').on('change', 'input.select-row, input.select-rows', function () { |
| 513 | var target, table = $(this).closest('table'); |
| 514 | if ($(this).hasClass('select-rows')) { |
| 515 | target = $('tbody tr', table); |
| 516 | $('input.select-row', table).prop('checked', $(this).prop('checked')); |
| 517 | } |
| 518 | else { |
| 519 | target = $(this).closest('tr'); |
| 520 | $('input.select-rows', table).prop('checked', $(".select-row:not(':checked')", table).length < 1); |
| 521 | } |
| 522 | target.toggleClass('crm-row-selected', $(this).is(':checked')); |
| 523 | }); |
| 524 | $('#crm-container').live('click', function (event) { |
| 525 | if ($(event.target).is('.btn-slide')) { |
| 526 | var currentActive = $('#crm-container .btn-slide-active'); |
| 527 | currentActive.children().hide(); |
| 528 | currentActive.removeClass('btn-slide-active'); |
| 529 | $(event.target).children().show(); |
| 530 | $(event.target).addClass('btn-slide-active'); |
| 531 | } |
| 532 | else { |
| 533 | $('.btn-slide .panel').hide(); |
| 534 | $('.btn-slide-active').removeClass('btn-slide-active'); |
| 535 | } |
| 536 | }); |
| 537 | }); |
| 538 | |
| 539 | /** |
| 540 | * Function to make multiselect boxes behave as fields in small screens |
| 541 | */ |
| 542 | function advmultiselectResize() { |
| 543 | var amswidth = $("#crm-container form:has(table.advmultiselect)").width(); |
| 544 | if (amswidth < 700) { |
| 545 | $("form table.advmultiselect td").css('display', 'block'); |
| 546 | } |
| 547 | else { |
| 548 | $("form table.advmultiselect td").css('display', 'table-cell'); |
| 549 | } |
| 550 | var contactwidth = $('#crm-container #mainTabContainer').width(); |
| 551 | if (contactwidth < 600) { |
| 552 | $('#crm-container #mainTabContainer').addClass('narrowpage'); |
| 553 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each(function (index) { |
| 554 | if (index > 1) { |
| 555 | if (index % 2 == 0) { |
| 556 | $(this).parent().after('<tr class="narrowadded"></tr>'); |
| 557 | } |
| 558 | var item = $(this); |
| 559 | $(this).parent().next().append(item); |
| 560 | } |
| 561 | }); |
| 562 | } |
| 563 | else { |
| 564 | $('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage'); |
| 565 | $('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each(function () { |
| 566 | var nitem = $(this); |
| 567 | var parent = $(this).parent(); |
| 568 | $(this).parent().prev().append(nitem); |
| 569 | if (parent.children().size() == 0) { |
| 570 | parent.remove(); |
| 571 | } |
| 572 | }); |
| 573 | $('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach(); |
| 574 | } |
| 575 | var cformwidth = $('#crm-container #Contact .contact_basic_information-section').width(); |
| 576 | |
| 577 | if (cformwidth < 720) { |
| 578 | $('#crm-container .contact_basic_information-section').addClass('narrowform'); |
| 579 | $('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon'); |
| 580 | if (cformwidth < 480) { |
| 581 | $('#crm-container .contact_basic_information-section').addClass('xnarrowform'); |
| 582 | } |
| 583 | else { |
| 584 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
| 585 | } |
| 586 | } |
| 587 | else { |
| 588 | $('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform'); |
| 589 | $('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform'); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | advmultiselectResize(); |
| 594 | $(window).resize(function () { |
| 595 | advmultiselectResize(); |
| 596 | }); |
| 597 | |
| 598 | $.fn.crmtooltip = function () { |
| 599 | $(document) |
| 600 | .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) { |
| 601 | $(this).addClass('crm-processed'); |
| 602 | $(this).addClass('crm-tooltip-active'); |
| 603 | var topDistance = e.pageY - $(window).scrollTop(); |
| 604 | if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) { |
| 605 | $(this).addClass('crm-tooltip-down'); |
| 606 | } |
| 607 | if (!$(this).children('.crm-tooltip-wrapper').length) { |
| 608 | $(this).append('<div class="crm-tooltip-wrapper"><div class="crm-tooltip"></div></div>'); |
| 609 | $(this).children().children('.crm-tooltip') |
| 610 | .html('<div class="crm-loading-element"></div>') |
| 611 | .load(this.href); |
| 612 | } |
| 613 | }) |
| 614 | .on('mouseout', 'a.crm-summary-link', function () { |
| 615 | $(this).removeClass('crm-processed'); |
| 616 | $(this).removeClass('crm-tooltip-active crm-tooltip-down'); |
| 617 | }) |
| 618 | .on('click', 'a.crm-summary-link', false); |
| 619 | }; |
| 620 | |
| 621 | var h; |
| 622 | CRM.help = function (title, params, url) { |
| 623 | h && h.close && h.close(); |
| 624 | var options = { |
| 625 | expires: 0 |
| 626 | }; |
| 627 | h = CRM.alert('...', title, 'crm-help crm-msg-loading', options); |
| 628 | params.class_name = 'CRM_Core_Page_Inline_Help'; |
| 629 | params.type = 'page'; |
| 630 | $.ajax(url || CRM.url('civicrm/ajax/inline'), |
| 631 | { |
| 632 | data: params, |
| 633 | dataType: 'html', |
| 634 | success: function (data) { |
| 635 | $('#crm-notification-container .crm-help .notify-content:last').html(data); |
| 636 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('info'); |
| 637 | }, |
| 638 | error: function () { |
| 639 | $('#crm-notification-container .crm-help .notify-content:last').html('Unable to load help file.'); |
| 640 | $('#crm-notification-container .crm-help').removeClass('crm-msg-loading').addClass('error'); |
| 641 | } |
| 642 | } |
| 643 | ); |
| 644 | }; |
| 645 | |
| 646 | /** |
| 647 | * @param string text Displayable message |
| 648 | * @param string title Displayable title |
| 649 | * @param string type 'alert'|'info'|'success'|'error' (default: 'alert') |
| 650 | * @param {object} options |
| 651 | * @return {*} |
| 652 | * @see http://wiki.civicrm.org/confluence/display/CRM/Notifications+in+CiviCRM |
| 653 | */ |
| 654 | CRM.alert = function (text, title, type, options) { |
| 655 | type = type || 'alert'; |
| 656 | title = title || ''; |
| 657 | options = options || {}; |
| 658 | if ($('#crm-notification-container').length) { |
| 659 | var params = { |
| 660 | text: text, |
| 661 | title: title, |
| 662 | type: type |
| 663 | }; |
| 664 | // By default, don't expire errors and messages containing links |
| 665 | var extra = { |
| 666 | expires: (type == 'error' || text.indexOf('<a ') > -1) ? 0 : (text ? 10000 : 5000), |
| 667 | unique: true |
| 668 | }; |
| 669 | options = $.extend(extra, options); |
| 670 | options.expires = options.expires === false ? 0 : parseInt(options.expires, 10); |
| 671 | if (options.unique && options.unique !== '0') { |
| 672 | $('#crm-notification-container .ui-notify-message').each(function () { |
| 673 | if (title === $('h1', this).html() && text === $('.notify-content', this).html()) { |
| 674 | $('.icon.ui-notify-close', this).click(); |
| 675 | } |
| 676 | }); |
| 677 | } |
| 678 | return $('#crm-notification-container').notify('create', params, options); |
| 679 | } |
| 680 | else { |
| 681 | if (title.length) { |
| 682 | text = title + "\n" + text; |
| 683 | } |
| 684 | alert(text); |
| 685 | return null; |
| 686 | } |
| 687 | }; |
| 688 | |
| 689 | /** |
| 690 | * Close whichever alert contains the given node |
| 691 | * |
| 692 | * @param node |
| 693 | */ |
| 694 | CRM.closeAlertByChild = function (node) { |
| 695 | $(node).closest('.ui-notify-message').find('.icon.ui-notify-close').click(); |
| 696 | }; |
| 697 | |
| 698 | /** |
| 699 | * Prompt the user for confirmation. |
| 700 | * |
| 701 | * @param buttons {object|function} key|value pairs where key == button label and value == callback function |
| 702 | * passing in a function instead of an object is a shortcut for a sinlgle button labeled "Continue" |
| 703 | * @param options {object|void} Override defaults, keys include 'title', 'message', |
| 704 | * see jQuery.dialog for full list of available params |
| 705 | */ |
| 706 | CRM.confirm = function (buttons, options, cancelLabel) { |
| 707 | var dialog, callbacks = {}; |
| 708 | cancelLabel = cancelLabel || ts('Cancel'); |
| 709 | var settings = { |
| 710 | title: ts('Confirm Action'), |
| 711 | message: ts('Are you sure you want to continue?'), |
| 712 | resizable: false, |
| 713 | modal: true, |
| 714 | width: 'auto', |
| 715 | close: function () { |
| 716 | $(dialog).remove(); |
| 717 | }, |
| 718 | buttons: {} |
| 719 | }; |
| 720 | |
| 721 | settings.buttons[cancelLabel] = function () { |
| 722 | dialog.dialog('close'); |
| 723 | }; |
| 724 | options = options || {}; |
| 725 | $.extend(settings, options); |
| 726 | if (typeof(buttons) === 'function') { |
| 727 | callbacks[ts('Continue')] = buttons; |
| 728 | } |
| 729 | else { |
| 730 | callbacks = buttons; |
| 731 | } |
| 732 | $.each(callbacks, function (label, callback) { |
| 733 | settings.buttons[label] = function () { |
| 734 | callback.call(dialog); |
| 735 | dialog.dialog('close'); |
| 736 | }; |
| 737 | }); |
| 738 | dialog = $('<div class="crm-container crm-confirm-dialog"></div>') |
| 739 | .html(options.message) |
| 740 | .appendTo('body') |
| 741 | .dialog(settings); |
| 742 | return dialog; |
| 743 | }; |
| 744 | |
| 745 | /** |
| 746 | * Sets an error message |
| 747 | * If called for a form item, title and removal condition will be handled automatically |
| 748 | */ |
| 749 | $.fn.crmError = function (text, title, options) { |
| 750 | title = title || ''; |
| 751 | text = text || ''; |
| 752 | options = options || {}; |
| 753 | |
| 754 | var extra = { |
| 755 | expires: 0 |
| 756 | }; |
| 757 | if ($(this).length) { |
| 758 | if (title == '') { |
| 759 | var label = $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]').not('[generated=true]'); |
| 760 | if (label.length) { |
| 761 | label.addClass('crm-error'); |
| 762 | var $label = label.clone(); |
| 763 | if (text == '' && $('.crm-marker', $label).length > 0) { |
| 764 | text = $('.crm-marker', $label).attr('title'); |
| 765 | } |
| 766 | $('.crm-marker', $label).remove(); |
| 767 | title = $label.text(); |
| 768 | } |
| 769 | } |
| 770 | $(this).addClass('error'); |
| 771 | } |
| 772 | var msg = CRM.alert(text, title, 'error', $.extend(extra, options)); |
| 773 | if ($(this).length) { |
| 774 | var ele = $(this); |
| 775 | setTimeout(function () { |
| 776 | ele.one('change', function () { |
| 777 | msg && msg.close && msg.close(); |
| 778 | ele.removeClass('error'); |
| 779 | label.removeClass('crm-error'); |
| 780 | }); |
| 781 | }, 1000); |
| 782 | } |
| 783 | return msg; |
| 784 | }; |
| 785 | |
| 786 | // Display system alerts through js notifications |
| 787 | function messagesFromMarkup() { |
| 788 | $('div.messages:visible', this).not('.help').not('.no-popup').each(function () { |
| 789 | var text, title = ''; |
| 790 | $(this).removeClass('status messages'); |
| 791 | var type = $(this).attr('class').split(' ')[0] || 'alert'; |
| 792 | type = type.replace('crm-', ''); |
| 793 | $('.icon', this).remove(); |
| 794 | if ($('.msg-text', this).length > 0) { |
| 795 | text = $('.msg-text', this).html(); |
| 796 | title = $('.msg-title', this).html(); |
| 797 | } |
| 798 | else { |
| 799 | text = $(this).html(); |
| 800 | } |
| 801 | var options = $(this).data('options') || {}; |
| 802 | $(this).remove(); |
| 803 | // Duplicates were already removed server-side |
| 804 | options.unique = false; |
| 805 | CRM.alert(text, title, type, options); |
| 806 | }); |
| 807 | // Handle qf form errors |
| 808 | $('form :input.error', this).one('blur', function() { |
| 809 | // ignore autocomplete fields |
| 810 | if ($(this).is('.ac_input')) { |
| 811 | return; |
| 812 | } |
| 813 | |
| 814 | $('.ui-notify-message.error a.ui-notify-close').click(); |
| 815 | $(this).removeClass('error'); |
| 816 | $(this).next('span.crm-error').remove(); |
| 817 | $('label[for="' + $(this).attr('name') + '"], label[for="' + $(this).attr('id') + '"]') |
| 818 | .removeClass('crm-error') |
| 819 | .find('.crm-error').removeClass('crm-error'); |
| 820 | }); |
| 821 | } |
| 822 | |
| 823 | $.widget('civicrm.crmSnippet', { |
| 824 | options: { |
| 825 | url: null, |
| 826 | block: true, |
| 827 | crmForm: null |
| 828 | }, |
| 829 | _create: function() { |
| 830 | this.element.addClass('crm-ajax-container'); |
| 831 | if (!this.element.is('.crm-container *')) { |
| 832 | this.element.addClass('crm-container'); |
| 833 | } |
| 834 | this.options.url ? this.refresh() : this.options.url = document.location.href; |
| 835 | }, |
| 836 | _onFailure: function(data) { |
| 837 | this.options.block && this.element.unblock(); |
| 838 | this.element.trigger('crmAjaxFail', data); |
| 839 | CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error'); |
| 840 | }, |
| 841 | _formatUrl: function(url) { |
| 842 | // Add snippet argument to url |
| 843 | if (url.search(/[&?]snippet=/) < 0) { |
| 844 | url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6'; |
| 845 | } |
| 846 | return url; |
| 847 | }, |
| 848 | refresh: function() { |
| 849 | var that = this; |
| 850 | var url = this._formatUrl(this.options.url); |
| 851 | this.options.block && this.element.block(); |
| 852 | $.getJSON(url, function(data) { |
| 853 | if (typeof(data) != 'object' || typeof(data.content) != 'string') { |
| 854 | that._onFailure(data); |
| 855 | return; |
| 856 | } |
| 857 | data.url = url; |
| 858 | that.element.html(data.content).trigger('crmLoad', data); |
| 859 | // This will also trigger crmFormLoad for forms |
| 860 | that.options.crmForm && that.element.trigger('crmFormLoad', data); |
| 861 | }).fail(function() { |
| 862 | that._onFailure(); |
| 863 | }); |
| 864 | } |
| 865 | }); |
| 866 | |
| 867 | var dialogCount = 0; |
| 868 | CRM.loadPage = function(url, options) { |
| 869 | var settings = { |
| 870 | target: '#crm-ajax-dialog-' + (dialogCount++), |
| 871 | dialog: { |
| 872 | modal: true, |
| 873 | width: '65%', |
| 874 | height: parseInt($(window).height() * .75), |
| 875 | close: function() { |
| 876 | $(this).dialog('destroy'); |
| 877 | $(this).remove(); |
| 878 | } |
| 879 | } |
| 880 | }; |
| 881 | options && $.extend(true, settings, options); |
| 882 | settings.url = url; |
| 883 | // Create new dialog |
| 884 | if (settings.dialog !== false && settings.target[0] == '#') { |
| 885 | $('<div id="'+ settings.target.substring(1) +'"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog); |
| 886 | } |
| 887 | if (settings.dialog && !settings.dialog.title) { |
| 888 | $(settings.target).on('crmLoad', function(event, data) { |
| 889 | data.title && $(this).dialog('option', 'title', data.title); |
| 890 | }); |
| 891 | } |
| 892 | return $(settings.target).crmSnippet(settings); |
| 893 | }; |
| 894 | |
| 895 | CRM.loadForm = function(url, options) { |
| 896 | var settings = { |
| 897 | crmForm: { |
| 898 | ajaxForm: {}, |
| 899 | block: true, |
| 900 | autoClose: true, |
| 901 | validate: true, |
| 902 | refreshAction: ['next_new', 'submit_savenext'], |
| 903 | cancelButton: '.cancel.form-submit', |
| 904 | openInline: 'a.button', |
| 905 | onCancel: function(event) {}, |
| 906 | onError: function(data) { |
| 907 | var $el = $(this); |
| 908 | $el.html(data.content).trigger('crmLoad', data).trigger('crmFormLoad', data); |
| 909 | if (typeof(data.errors) == 'object') { |
| 910 | $.each(data.errors, function(formElement, msg) { |
| 911 | $('[name="'+formElement+'"]', $el).crmError(msg); |
| 912 | }); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | }; |
| 917 | // Move options that belong to crmForm. Others will be passed through to crmSnippet |
| 918 | options && $.each(options, function(key, value) { |
| 919 | if (typeof(settings.crmForm[key]) !== 'undefined') { |
| 920 | settings.crmForm[key] = value; |
| 921 | } |
| 922 | else { |
| 923 | settings[key] = value; |
| 924 | } |
| 925 | }); |
| 926 | settings.type = 'Form'; |
| 927 | |
| 928 | var widget = CRM.loadPage(url, settings); |
| 929 | |
| 930 | widget.on('crmFormLoad', function(event, data) { |
| 931 | var $el = $(this); |
| 932 | var settings = $el.data('crmSnippet').options.crmForm; |
| 933 | var dialog = $el.data('dialog'); |
| 934 | settings.cancelButton && $(settings.cancelButton, this).click(function(event) { |
| 935 | var returnVal = settings.onCancel.call($el, event); |
| 936 | if (returnVal !== false) { |
| 937 | $el.trigger('crmFormCancel', event); |
| 938 | dialog && settings.autoClose && $el.dialog('close'); |
| 939 | } |
| 940 | return returnVal === false; |
| 941 | }); |
| 942 | settings.openInline && $(settings.openInline, this).click(function(event) { |
| 943 | $el.crmSnippet('option', 'url', this.href).crmSnippet('refresh'); |
| 944 | return false; |
| 945 | }); |
| 946 | if (settings.validate) { |
| 947 | $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); |
| 948 | } |
| 949 | $("form", this).ajaxForm($.extend({ |
| 950 | url: data.url, |
| 951 | dataType: 'json', |
| 952 | success: function(response) { |
| 953 | if (response.status == 'success') { |
| 954 | settings.block && $el.unblock(); |
| 955 | $el.trigger('crmFormSuccess', response); |
| 956 | // Reset form for e.g. "save and new" |
| 957 | if (settings.refreshAction && $.inArray(response.buttonName, settings.refreshAction) >= 0) { |
| 958 | $el.crmSnippet('option', 'url', response.userContext).crmSnippet('refresh'); |
| 959 | } |
| 960 | else if (dialog && settings.autoClose) { |
| 961 | $el.dialog('close'); |
| 962 | } |
| 963 | } |
| 964 | else { |
| 965 | settings.onError.call($el, response); |
| 966 | } |
| 967 | }, |
| 968 | beforeSubmit: function(submission) { |
| 969 | settings.block && $el.block(); |
| 970 | $el.trigger('crmFormSubmit', submission); |
| 971 | } |
| 972 | }, settings.ajaxForm)); |
| 973 | }); |
| 974 | return widget; |
| 975 | }; |
| 976 | |
| 977 | // Preprocess all cj ajax calls to display messages |
| 978 | $(document).ajaxSuccess(function(event, xhr, settings) { |
| 979 | try { |
| 980 | if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) { |
| 981 | var response = $.parseJSON(xhr.responseText); |
| 982 | if (typeof(response.crmMessages) == 'object') { |
| 983 | $.each(response.crmMessages, function(n, msg) { |
| 984 | CRM.alert(msg.text, msg.title, msg.type, msg.options); |
| 985 | }) |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | // Suppress errors |
| 990 | catch (e) {} |
| 991 | }); |
| 992 | |
| 993 | $(function () { |
| 994 | // Trigger crmLoad on initial content for consistency. It will also be triggered for ajax-loaded content. |
| 995 | $('#crm-container').trigger('crmLoad'); |
| 996 | |
| 997 | if ($('#crm-notification-container').length) { |
| 998 | // Initialize notifications |
| 999 | $('#crm-notification-container').notify(); |
| 1000 | messagesFromMarkup.call($('#crm-container')); |
| 1001 | $('#crm-container').on('crmFormLoad', '*', messagesFromMarkup); |
| 1002 | } |
| 1003 | |
| 1004 | // bind the event for image popup |
| 1005 | $('body').on('click', 'a.crm-image-popup', function() { |
| 1006 | var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>'); |
| 1007 | |
| 1008 | CRM.confirm('', |
| 1009 | { |
| 1010 | title: ts('Preview'), |
| 1011 | message: o |
| 1012 | }, |
| 1013 | ts('Done') |
| 1014 | ); |
| 1015 | return false; |
| 1016 | }); |
| 1017 | }); |
| 1018 | |
| 1019 | $.fn.crmAccordions = function (speed) { |
| 1020 | var container = $('#crm-container'); |
| 1021 | if (speed === undefined) { |
| 1022 | speed = 200; |
| 1023 | } |
| 1024 | if ($(this).length > 0) { |
| 1025 | container = $(this); |
| 1026 | } |
| 1027 | if (container.length > 0 && !container.hasClass('crm-accordion-processed')) { |
| 1028 | // Allow normal clicking of links |
| 1029 | container.on('click', 'div.crm-accordion-header a', function (e) { |
| 1030 | e.stopPropagation && e.stopPropagation(); |
| 1031 | }); |
| 1032 | container.on('click', '.crm-accordion-header, .crm-collapsible .collapsible-title', function () { |
| 1033 | if ($(this).parent().hasClass('collapsed')) { |
| 1034 | $(this).next().css('display', 'none').slideDown(speed); |
| 1035 | } |
| 1036 | else { |
| 1037 | $(this).next().css('display', 'block').slideUp(speed); |
| 1038 | } |
| 1039 | $(this).parent().toggleClass('collapsed'); |
| 1040 | return false; |
| 1041 | }); |
| 1042 | container.addClass('crm-accordion-processed'); |
| 1043 | } |
| 1044 | }; |
| 1045 | $.fn.crmAccordionToggle = function (speed) { |
| 1046 | $(this).each(function () { |
| 1047 | if ($(this).hasClass('collapsed')) { |
| 1048 | $('.crm-accordion-body', this).first().css('display', 'none').slideDown(speed); |
| 1049 | } |
| 1050 | else { |
| 1051 | $('.crm-accordion-body', this).first().css('display', 'block').slideUp(speed); |
| 1052 | } |
| 1053 | $(this).toggleClass('collapsed'); |
| 1054 | }); |
| 1055 | }; |
| 1056 | |
| 1057 | /** |
| 1058 | * Clientside currency formatting |
| 1059 | * @param value |
| 1060 | * @param format - currency representation of the number 1234.56 |
| 1061 | * @return string |
| 1062 | * @see CRM_Core_Resources::addCoreResources |
| 1063 | */ |
| 1064 | var currencyTemplate; |
| 1065 | CRM.formatMoney = function(value, format) { |
| 1066 | var decimal, separator, sign, i, j, result; |
| 1067 | if (value === 'init' && format) { |
| 1068 | currencyTemplate = format; |
| 1069 | return; |
| 1070 | } |
| 1071 | format = format || currencyTemplate; |
| 1072 | result = /1(.?)234(.?)56/.exec(format); |
| 1073 | if (result === null) { |
| 1074 | return 'Invalid format passed to CRM.formatMoney'; |
| 1075 | } |
| 1076 | separator = result[1]; |
| 1077 | decimal = result[2]; |
| 1078 | sign = (value < 0) ? '-' : ''; |
| 1079 | //extracting the absolute value of the integer part of the number and converting to string |
| 1080 | i = parseInt(value = Math.abs(value).toFixed(2)) + ''; |
| 1081 | j = ((j = i.length) > 3) ? j % 3 : 0; |
| 1082 | result = sign + (j ? i.substr(0, j) + separator : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + separator) + (2 ? decimal + Math.abs(value - i).toFixed(2).slice(2) : ''); |
| 1083 | return format.replace(/1.*234.*56/, result); |
| 1084 | }; |
| 1085 | })(jQuery); |