📄 datatable.js
字号:
$('#exportToFormat img', domElem).click(function(){ $(this).siblings('#linksExportToFormat').toggle(); }); $('.exportToFormat', domElem).attr( 'href', function(){ var format = $(this).attr('format'); var method = $(this).attr('methodToCall'); var filter_limit = $(this).attr('filter_limit'); var param_date = self.param.date; var date = $(this).attr('date'); if(typeof date != 'undefined') { param_date = date; } var str = '?module=API' +'&method='+method +'&format='+format +'&idSite='+self.param.idSite +'&period='+self.param.period +'&date='+param_date +'&token_auth='+piwik.token_auth; if( filter_limit ) { str += '&filter_limit=' + filter_limit; } return str; } ); }, truncate: function(domElemToTruncate, truncationOffset) { var self = this; if(typeof truncationOffset == 'undefined') { truncationOffset = 0; } var truncationLimit = 30; // in a subtable if(typeof self.param.idSubtable != 'undefined') { truncationLimit = 25; } // when showing all columns if(typeof self.param.idSubtable == 'undefined' && self.param.viewDataTable == 'tableAllColumns') { truncationLimit = 17; } // when showing all columns in a subtable, space is restricted else if(self.param.viewDataTable == 'tableAllColumns') { truncationLimit = 10; } truncationLimit += truncationOffset; $(domElemToTruncate).truncate(truncationLimit); $('.truncated', domElemToTruncate) .Tooltip(); }, //Apply some miscelleaneous style to the DataTable applyCosmetics: function(domElem) { var self = this; var urlLinkFoundDom = $("tr:not('.subDataTable') td:first-child:has('#urlLink')", domElem); if(urlLinkFoundDom.length == 0) { self.truncate( $("table tr td:first-child", domElem) ); } else { var imageLinkWidth = 10; var imageLinkHeight = 9; urlLinkFoundDom.each( function(){ // we add a link based on the <span id="urlLink"> present in the column label (the first column) // if this span is there, we add the link around the HTML in the TD // but we add this link only for the rows that are not clickable already (subDataTable) var imgToPrepend = ''; if( $(this).find('img').length == 0 ) { imgToPrepend = '<img width="'+imageLinkWidth+'" height="'+imageLinkHeight+'" src="'+piwik.piwik_url+'themes/default/images/link.gif" /> '; } var urlLinkDom = $('#urlLink',this); var urlToLink = $(urlLinkDom).html(); $(urlLinkDom).remove(); var truncationOffsetBecauseImageIsPrepend = -2; //website subtable needs -9. self.truncate( $(this), truncationOffsetBecauseImageIsPrepend ); if( urlToLink.match("javascript:") ) { $(this).html( '<a href="#" onClick="' + urlToLink.replace("javascript:","") + '">' + imgToPrepend + $(this).html() + '</a>'); } else { $(this).html( '<a target="_blank" href="' + urlToLink + '">' + imgToPrepend + $(this).html() + '</a>'); } }); } // Add some styles on the cells even/odd // label (first column of a data row) or not $("th:first-child", domElem).addClass('label'); $("td:first-child:odd", domElem).addClass('label labeleven'); $("td:first-child:even", domElem).addClass('label labelodd'); $("tr:odd td", domElem).slice(1).addClass('columnodd'); $("tr:even td", domElem).slice(1).addClass('columneven'); // Change cursor on mouse hover if sort is enabled if( self.param.enable_sort ) { $("th.sortable", domElem) .hover( function() { $(this).css({ cursor: "pointer"}); }, function() { $(this).css({ cursor: "auto"}); }); } }, //behaviour for 'nested DataTable' (DataTable loaded on a click on a row) handleSubDataTable: function(domElem) { var self = this; // When the TR has a subDataTable class it means that this row has a link to a subDataTable $('tr.subDataTable', domElem) .click( function() { // get the idSubTable var idSubTable = $(this).attr('id'); var divIdToReplaceWithSubTable = 'subDataTable_'+idSubTable; // if the subDataTable is not already loaded if (typeof self.loadedSubDataTable[divIdToReplaceWithSubTable] == "undefined") { var numberOfColumns = $(this).children().length; // at the end of the query it will replace the ID matching the new HTML table #ID // we need to create this ID first $(this).after( '\ <tr>\ <td colspan="'+numberOfColumns+'" class="cellSubDataTable">\ <div id="'+divIdToReplaceWithSubTable+'">\ <span id="loadingDataTable" style="display:inline"><img src="'+piwik.piwik_url+'themes/default/images/loading-blue.gif" />'+ _pk_translate('CoreHome_Loading') +'</span>\ </div>\ </td>\ </tr>\ '); var savedActionVariable = self.param.action; // reset all the filters from the Parent table var filtersToRestore = self.resetAllFilters(); self.param.idSubtable = idSubTable; self.param.action = self.param.controllerActionCalledWhenRequestSubTable; self.reloadAjaxDataTable(false); self.param.action = savedActionVariable; delete self.param.idSubtable; self.restoreAllFilters(filtersToRestore); self.loadedSubDataTable[divIdToReplaceWithSubTable] = true; $(this).next().toggle(); } $(this).next().toggle(); } ); }};// Helper function :// returns true if the event keypress passed in parameter is the ENTER keyfunction submitOnEnter(e){ var key=e.keyCode || e.which; if (key==13) { return true; }}//-----------------------------------------------------------------------------// Action Data Table//-----------------------------------------------------------------------------//inheritance declaration//actionDataTable is a child of dataTableactionDataTable.prototype = new dataTable;actionDataTable.prototype.constructor = actionDataTable;//A list of all our actionDataTables//Test if the object have already been initialized (multiple includes)if(typeof actionDataTables == "undefined"){ var actionDataTables = new Object;}//actionDataTable constructorfunction actionDataTable(){ dataTable.call(this); this.parentAttributeParent = ''; this.parentId = ''; this.disabledRowDom = new Object; //to handle double click on '+' row}//Prototype of the actionDataTable objectactionDataTable.prototype ={ //method inheritance cleanParams: dataTable.prototype.cleanParams, reloadAjaxDataTable: dataTable.prototype.reloadAjaxDataTable, buildAjaxRequest: dataTable.prototype.buildAjaxRequest, handleLowPopulationLink: dataTable.prototype.handleLowPopulationLink, handleSearchBox: dataTable.prototype.handleSearchBox, //initialisation of the actionDataTable init: function(workingDivId, domElem) { if(typeof domElem == "undefined") { domElem = $('#'+workingDivId); } this.workingDivId = workingDivId; this.bindEventsAndApplyStyle(domElem); this.initialized = true; }, //see dataTable::bindEventsAndApplyStyle bindEventsAndApplyStyle: function(domElem) { var self = this; self.cleanParams(); // we dont display the link on the row with subDataTable when we are already // printing all the subTables (case of recursive search when the content is // including recursively all the subtables if(!self.param.filter_pattern_recursive) { $('tr.subActionsDataTable.rowToProcess') .click( function() { self.onClickActionSubDataTable(this) }) .hover( function() { $(this).css({ cursor: "pointer"}); }, function() { $(this).css({ cursor: "auto"}); } ); } self.applyCosmetics(domElem); if( self.workingDivId != undefined) { self.handleSearchBox(domElem, self.actionsDataTableLoaded ); self.handleLowPopulationLink(domElem, self.actionsDataTableLoaded ); } }, //see dataTable::applyCosmetics applyCosmetics: function(domElem) { var self = this; $('tr.subActionsDataTable.rowToProcess') .css('font-weight','bold'); $("th:first-child", domElem).addClass('label'); var imagePlusMinusWidth = 12; var imagePlusMinusHeight = 12; $('tr.subActionsDataTable.rowToProcess td:first-child') .each( function(){ $(this).prepend('<img width="'+imagePlusMinusWidth+'" height="'+imagePlusMinusHeight+'" class="plusMinus" src="" />'); if(self.param.filter_pattern_recursive) { setImageMinus(this, piwik.piwik_url); } else { setImagePlus(this, piwik.piwik_url); } }); $('tr.rowToProcess') .each( function() { // we add the CSS style depending on the level of the current loading category // we look at the style of the parent row var style = $(this).prev().attr('class'); var currentStyle = $(this).attr('class'); if( (typeof currentStyle != 'undefined') && currentStyle.indexOf('level') >= 0 ) { } else { var level = getNextLevelFromClass( style ); $(this).addClass('level'+ level); } // we add an attribute parent that contains the ID of all the parent categories // this ID is used when collapsing a parent row, it searches for all children rows // which 'parent' attribute's value contains the collapsed row ID $(this).attr('parent', function(){ return self.parentAttributeParent + ' ' + self.parentId; } ); // Add some styles on the cells even/odd // label (first column of a data row) or not $("td:first-child:odd", this).addClass('label labeleven'); $("td:first-child:even", this).addClass('label labelodd'); // we truncate the labels columns from the second row $("td:first-child", this).truncate(30); $('.truncated', this).Tooltip(); }) .removeClass('rowToProcess'); }, // Called when the user click on an actionDataTable row onClickActionSubDataTable: function(domElem) { var self = this; // get the idSubTable var idSubTable = $(domElem).attr('id'); var divIdToReplaceWithSubTable = 'subDataTable_'+idSubTable; var NextStyle = $(domElem).next().attr('class'); var CurrentStyle = $(domElem).attr('class'); var currentRowLevel = getLevelFromClass(CurrentStyle); var nextRowLevel = getLevelFromClass(NextStyle); // if the row has not been clicked // which is the same as saying that the next row level is equal or less than the current row // because when we click a row the level of the next rows is higher (level2 row gives level3 rows) if(currentRowLevel >= nextRowLevel) { //unbind click to avoid double click problem $(domElem).unbind('click'); self.disabledRowDom = $(domElem); var numberOfColumns = $(domElem).children().length; $(domElem).after( '\ <tr id="'+divIdToReplaceWithSubTable+'" class="cellSubDataTable">\ <td colspan="'+numberOfColumns+'">\ <span id="loadingDataTable" style="display:inline"><img src="'+piwik.piwik_url+'themes/default/images/loading-blue.gif" /> Loading...</span>\ </td>\ </tr>\ '); var savedActionVariable = self.param.action; // reset search for subcategories delete self.param.filter_column; delete self.param.filter_pattern; self.param.idSubtable = idSubTable; self.param.action = self.param.controllerActionCalledWhenRequestSubTable; self.reloadAjaxDataTable(false, function(resp){self.actionsSubDataTableLoaded(resp)}); self.param.action = savedActionVariable; delete self.param.idSubtable; } // else we toggle all these rows else { var plusDetected = $('td img', domElem).attr('src').indexOf('plus') >= 0; $(domElem).siblings().each( function(){ var parents = $(this).attr('parent'); if(parents) { if(parents.indexOf(idSubTable) >= 0 || parents.indexOf('subDataTable_'+idSubTable) >= 0) { if(plusDetected) { $(this).css('display',''); //unroll everything and display '-' sign //if the row is already opened var NextStyle = $(this).next().attr('class'); var CurrentStyle = $(this).attr('class'); var currentRowLevel = getLevelFromClass(CurrentStyle); var nextRowLevel = getLevelFromClass(NextStyle); if(currentRowLevel < nextRowLevel) setImageMinus(this, piwik.piwik_url); } else { $(this).css('display','none'); } } } }); } // toggle the +/- image var plusDetected = $('td img', domElem).attr('src').indexOf('plus') >= 0; if(plusDetected) { setImageMinus(domElem, piwik.piwik_url); } else { setImagePlus(domElem, piwik.piwik_url); } }, //called when the full table actions is loaded actionsDataTableLoaded: function(response) { var content = $(response); var idToReplace = $(content).attr('id'); //reset parents id self.parentAttributeParent = ''; self.parentId = ''; var dataTableSel = $('#'+idToReplace); dataTableSel.html($(content).html()); lazyScrollTo(dataTableSel[0], 400); }, // Called when a set of rows for a category of actions is loaded actionsSubDataTableLoaded: function(response) { var self = this; var idToReplace = $(response).attr('id'); // remove the first row of results which is only used to get the Id var response = $(response).filter('tr').slice(1).addClass('rowToProcess'); self.parentAttributeParent = $('tr#'+idToReplace).prev().attr('parent'); self.parentId = idToReplace; $('tr#'+idToReplace).after( response ).remove(); var re = /subDataTable_(\d+)/; ok = re.exec(self.parentId); if(ok) { self.parentId = ok[1]; } // we execute the bindDataTableEvent function for the new DIV self.init(self.workingDivId, $('#'+idToReplace)); //bind back the click event (disabled to avoid double-click problem) self.disabledRowDom.click( function() { self.onClickActionSubDataTable(this) }); }};//helper function for actionDataTablefunction getLevelFromClass( style) { if (typeof style == "undefined") return 0; var currentLevelIndex = style.indexOf('level'); var currentLevel = 0; if( currentLevelIndex >= 0) { currentLevel = Number(style.substr(currentLevelIndex+5,1)); } return currentLevel;}//helper function for actionDataTablefunction getNextLevelFromClass( style ){ if (typeof style == "undefined") return 0; currentLevel = getLevelFromClass(style); newLevel = currentLevel; // if this is not a row to process so if( style.indexOf('rowToProcess') < 0 ) { newLevel = currentLevel + 1; } return newLevel;}//helper function for actionDataTablefunction setImageMinus( domElem, pathToPiwik ){ $('img',domElem).attr('src', pathToPiwik + 'themes/default/images/minus.png');}//helper function for actionDataTablefunction setImagePlus( domElem, pathToPiwik ){ $('img',domElem).attr('src', pathToPiwik + 'themes/default/images/plus.png');}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -