| 1 | {* |
| 2 | +--------------------------------------------------------------------+ |
| 3 | | CiviCRM version 4.7 | |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 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 | {literal} |
| 27 | <script type="text/javascript"> |
| 28 | CRM.$(function($) { |
| 29 | |
| 30 | function getElementClass(element) { |
| 31 | return $(element).attr('class') || ''; |
| 32 | } |
| 33 | |
| 34 | // fetch the occurrence of element |
| 35 | function getRowId(row, str) { |
| 36 | var optionId; |
| 37 | $.each(row, function(i, n) { |
| 38 | if (str === $(n).attr('class')) { |
| 39 | optionId = i; |
| 40 | } |
| 41 | }); |
| 42 | return optionId; |
| 43 | } |
| 44 | |
| 45 | // for date sorting see http://wiki.civicrm.org/confluence/display/CRMDOC/Sorting+Date+Fields+in+dataTables+Widget |
| 46 | var useAjax = {/literal}{if $useAjax}1{else}0{/if}{literal}, |
| 47 | sourceUrl = '', |
| 48 | useClass = 'display', |
| 49 | tcount = 1, |
| 50 | tableId = []; |
| 51 | |
| 52 | if ( useAjax ) { |
| 53 | {/literal}{if isset($sourceUrl)}sourceUrl = "{$sourceUrl}";{/if}{literal} |
| 54 | useClass = 'pagerDisplay'; |
| 55 | tcount = 5; |
| 56 | } |
| 57 | |
| 58 | CRM.dataTableCount = CRM.dataTableCount || 1; |
| 59 | |
| 60 | // FIXME: Rewriting DOM ids is probably a bad idea, and could be avoided |
| 61 | $('table.' + useClass).not('.dataTable').each(function() { |
| 62 | $(this).attr('id','option' + tcount + CRM.dataTableCount); |
| 63 | tableId.push(CRM.dataTableCount); |
| 64 | CRM.dataTableCount++; |
| 65 | }); |
| 66 | |
| 67 | $.each(tableId, function(i,n){ |
| 68 | var tabId = '#option' + tcount + n; |
| 69 | //get the object of first tr data row. |
| 70 | var tdObject = $(tabId + ' tr:nth(1) td'); |
| 71 | var id = -1; var count = 0; var columns=''; var sortColumn = ''; |
| 72 | //build columns array for sorting or not sorting |
| 73 | $(tabId + ' th').each( function( ) { |
| 74 | var option = $(this).prop('id').split("_"); |
| 75 | option = ( option.length > 1 ) ? option[1] : option[0]; |
| 76 | var stype = 'numeric'; |
| 77 | switch( option ) { |
| 78 | case 'sortable': |
| 79 | sortColumn += '[' + count + ', "asc" ],'; |
| 80 | columns += '{"sClass": "'+ getElementClass( this ) +'"},'; |
| 81 | break; |
| 82 | case 'date': |
| 83 | stype = 'date'; |
| 84 | case 'order': |
| 85 | if ( $(this).attr('class') == 'sortable' ){ |
| 86 | sortColumn += '[' + count + ', "asc" ],'; |
| 87 | } |
| 88 | var sortId = getRowId(tdObject, $(this).attr('id') +' hiddenElement' ); |
| 89 | columns += '{ "render": function ( data, type, row ) { return "<div style=\'display:none\'>"+ data +"</div>" + row[sortId] ; }, "targets": sortColumn,"bUseRendered": false},'; |
| 90 | break; |
| 91 | case 'nosort': |
| 92 | columns += '{ "bSortable": false, "sClass": "'+ getElementClass( this ) +'"},'; |
| 93 | break; |
| 94 | case 'currency': |
| 95 | columns += '{ "sType": "currency" },'; |
| 96 | break; |
| 97 | case 'link': |
| 98 | columns += '{"sType": "html"},'; |
| 99 | break; |
| 100 | default: |
| 101 | if ( $(this).text() ) { |
| 102 | columns += '{"sClass": "'+ getElementClass( this ) +'"},'; |
| 103 | } else { |
| 104 | columns += '{ "bSortable": false },'; |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | count++; |
| 109 | }); |
| 110 | // Fixme: this could be done without eval |
| 111 | columns = columns.substring(0, columns.length - 1 ); |
| 112 | sortColumn = sortColumn.substring(0, sortColumn.length - 1 ); |
| 113 | eval('sortColumn =[' + sortColumn + ']'); |
| 114 | eval('columns =[' + columns + ']'); |
| 115 | |
| 116 | var noRecordFoundMsg = {/literal}'{ts escape="js"}None found.{/ts}'{literal}; |
| 117 | |
| 118 | var oTable; |
| 119 | if ( useAjax ) { |
| 120 | oTable = $(tabId).dataTable({ |
| 121 | "bFilter": false, |
| 122 | "bAutoWidth": false, |
| 123 | "aaSorting": sortColumn, |
| 124 | "aoColumns": columns, |
| 125 | "bProcessing": true, |
| 126 | "bJQueryUI": true, |
| 127 | "asStripClasses": [ "odd-row", "even-row" ], |
| 128 | "sPaginationType": "full_numbers", |
| 129 | "sDom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>', |
| 130 | "bServerSide": true, |
| 131 | "sAjaxSource": sourceUrl, |
| 132 | "oLanguage":{ |
| 133 | "sEmptyTable": noRecordFoundMsg, |
| 134 | "sZeroRecords": noRecordFoundMsg |
| 135 | }, |
| 136 | "fnServerData": function ( sSource, aoData, fnCallback ) { |
| 137 | $.ajax( { |
| 138 | "dataType": 'json', |
| 139 | "type": "POST", |
| 140 | "url": sSource, |
| 141 | "data": aoData, |
| 142 | "success": fnCallback |
| 143 | }); |
| 144 | } |
| 145 | }); |
| 146 | } else { |
| 147 | oTable = $(tabId).dataTable({ |
| 148 | "aaSorting": sortColumn, |
| 149 | "bPaginate": false, |
| 150 | "bLengthChange": true, |
| 151 | "bFilter": false, |
| 152 | "bInfo": false, |
| 153 | "asStripClasses": [ "odd-row", "even-row" ], |
| 154 | "bAutoWidth": false, |
| 155 | "aoColumns": columns, |
| 156 | "bSort": true, |
| 157 | "sDom": 'ti', |
| 158 | "oLanguage":{ |
| 159 | "sEmptyTable": noRecordFoundMsg, |
| 160 | "sZeroRecords": noRecordFoundMsg |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | //plugin to sort on currency |
| 168 | cj.fn.dataTableExt.oSort['currency-asc'] = function(a,b) { |
| 169 | var symbol = "{/literal}{$config->defaultCurrencySymbol()}{literal}"; |
| 170 | var x = (a == "-") ? 0 : a.replace( symbol, "" ); |
| 171 | var y = (b == "-") ? 0 : b.replace( symbol, "" ); |
| 172 | x = parseFloat( x ); |
| 173 | y = parseFloat( y ); |
| 174 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); |
| 175 | }; |
| 176 | |
| 177 | cj.fn.dataTableExt.oSort['currency-desc'] = function(a,b) { |
| 178 | var symbol = "{/literal}{$config->defaultCurrencySymbol()}{literal}"; |
| 179 | var x = (a == "-") ? 0 : a.replace( symbol, "" ); |
| 180 | var y = (b == "-") ? 0 : b.replace( symbol, "" ); |
| 181 | x = parseFloat( x ); |
| 182 | y = parseFloat( y ); |
| 183 | return ((x < y) ? 1 : ((x > y) ? -1 : 0)); |
| 184 | }; |
| 185 | </script> |
| 186 | {/literal} |