/*! * Copyright (C) 2012 Hewlett-Packard Development Company, L.P. * * The contents of this software are proprietary and confidential to the * Hewlett-Packard Development Company, L.P. No part of this program may be * photocopied, reproduced or translated into another programming language * without prior written consent of the Hewlett-Packard Dev. Co., L.P. * */ /** * @summary jQuery utils for MSM * @description Some useful functions * @file incl/js/jquery-utils.msm.js * @author Javier Albornoz * @contact ajavier@hp.com */ // Global variables function fnCreateSelect( aoOptions, sId, sClassName ) { var r=''; for(var i=0; i' + aoOptions[i].option + ''; } return r+''; } function fnDownloadFile(url, data, method) { // URL and data options required if( url && data ) { // Data can be string of parameters or array/object data = typeof data == 'string' ? data : $j.param(data); // Split params into form inputs var inputs = ''; $j.each(data.split('&'), function(){ var pair = this.split('='); inputs+=''; }); // Send request $j('
'+inputs+'
').appendTo('body').submit().remove(); }; } /** * Returns a element with the left-truncated form of the given string value and the full string value as tooltip. * * Example: fnLeftTruncateString("abcdef", 4) -> "...cdef" * * @param {String} value A DataTables cell value. * @param {Number} maxLength The maximum number of characters allowed. * @param {String} ellipsisString (optional) The string to use as ellipsis value (default is '...'). * * @returns {String} A element with the left-truncated form of the given string value and the full string value as tooltip. */ function fnLeftTruncateString(value, maxLength, ellipsisString) { return fnTruncateString(value, 0, maxLength, ellipsisString); } /** * Returns a element with the right-truncated form of the given string value and the full string value as tooltip. * * Example: fnRightTruncateString("abcdef", 4) -> "abcd..." * * @param {String} value A DataTables cell value. * @param {Number} maxLength The maximum number of characters allowed. * @param {String} ellipsisString (optional) The string to use as ellipsis value (default is '...'). * * @returns {String} A element with the right-truncated form of the given string value and the full string value as tooltip. */ function fnRightTruncateString(value, maxLength, ellipsisString) { return fnTruncateString(value, maxLength, 0, ellipsisString); } /** * Returns a element with the middle-truncated form of the given string value and the full string value as tooltip. * * Example: fnMiddleTruncateString("abcdef", 4) -> "ab...ef" * * @param {String} value A DataTables cell value. * @param {Number} maxLength The maximum number of characters allowed. * @param {String} ellipsisString (optional) The string to use as ellipsis value (default is '...'). * * @returns {String} A element with the middle-truncated form of the given string value and the full string value as tooltip. */ function fnMiddleTruncateString(value, maxLength, ellipsisString) { return fnTruncateString(value, Math.ceil(maxLength / 2), Math.floor(maxLength / 2), ellipsisString); } /** * Returns a element with the truncated form of the given string value and the full string value as tooltip. * No truncation will take place if the full string fits within the limits specified by maxPreEllipsisLength and maxPostEllipsisLength. * * @param {String} value A DataTables cell value. * @param {Number} maxPreEllipsisLength The maximum number of characters allowed before the ellipsis string (0 for left truncation). * @param {Number} maxPostEllipsisLength The maximum number of characters allowed after the ellipsis string (0 for right truncation). * @param {String} ellipsisString (optional) The string to use as ellipsis value (default is '...'). * * @returns {String} A element with the truncated form of the given string value and the full string value as tooltip. */ function fnTruncateString(value, maxPreEllipsisLength, maxPostEllipsisLength, ellipsisString) { if (!ellipsisString) { var ellipsisString = "..."; } // value already has the HTML entities escaped // the line below reverts the escaped characters to be able to work with the full text. var fullString = $j("").html(value).text(); var truncatedString = ''; var escapedTruncatedString = value; if (fullString.length <= maxPreEllipsisLength || (maxPreEllipsisLength == 0 && fullString.length <= maxPostEllipsisLength)) { truncatedString = fullString; } else { escapedTruncatedString = $j("") .append(document.createTextNode(fullString.substring(0, maxPreEllipsisLength))) .append(ellipsisString) .append(document.createTextNode(fullString.substring(Math.max(maxPreEllipsisLength, (fullString.length - maxPostEllipsisLength))))) .html(); } return "" + escapedTruncatedString + ""; } function fnSetCheckboxBehavior(checkBox) { $j(checkBox).parent().parent().toggleClass('row_selected'); $j("#selectAll").removeAttr('checked'); } jQuery(function($) { // This code will be invoked after the page is loaded // The specific pages can include or not the dataTable library if ($.fn.dataTableExt) { /* * Adding custom filter to the datatables */ $.fn.dataTableExt.afnFiltering.push( /* Brief: Custom filter for the datatables. * Purpose: Add support for filtering using numeric intervals * Returns: bool:bDisplayEntry - true if filter matches (show in results) * Inputs: object:oSettings - dataTable settings object * array:aData - the data for the current row * int:iDataIndex - the index of the row in aoData * string:sSearchRange - the string use as the filter (it can be an interval). Taken from local storage * int:iColumnIndex - the id of the column that is currently selected as the filter. Taken from local storage * Note: For the moment this custom filter works only for one column of the table. * This filter is intended for the VLANs table, but it could be used in other tables */ function (oSettings, aData, iDataIndex) { var bDisplayEntry = false; // false: filter doesn't match (don't show in results) // Get the filter string and the filtered column index from local storage var sFilter = localStorage.getItem(oSettings.nTable.id + "sSearchRange"); var iColumn = localStorage.getItem(oSettings.nTable.id + "iColumnIndex"); if (iColumn == null) { // don't do anything if no filter is selected return true; } if (sFilter == null || sFilter.length == 0) { // if the filter is empty bDisplayEntry = true; } else { var asLimits = sFilter.split("-"); // Get the interval limits, if it is not a interval iMax will be NaN var iMin = Number(asLimits[0]); var iMax = Number(asLimits[1]); var iValue = Number(aData[iColumn]); if (!isNaN(iValue)) { // If the data is a number if (iMin == iValue && isNaN(iMax)) { bDisplayEntry = true; } else if (iMin <= iValue && iValue <= iMax) { bDisplayEntry = true; } } else { // If the data is not a number (it can be another interval) var asRange = aData[iColumn].split("-"); // Get the interval var iBegin = Number(asRange[0]); var iEnd = Number(asRange[1]); if (isNaN(iMax) && iBegin <= iMin && iMin <= iEnd) { bDisplayEntry = true; } else if (iMin <= iEnd && iBegin <= iMax) { bDisplayEntry = true; } } } return bDisplayEntry; }); $.fn.dataTableExt.oApi.fnCreateActions = function ( oSettings, aoActions, sDivName, sActionLabel, sTarget, fnCallback ) { if(aoActions.length) { var oTable = this; $(sDivName).append($('
')); $(".dataTables_actionDiv").append("" + sActionLabel + " ") .append(fnCreateSelect(aoActions, "availableActions", "filterObject")) .append(''); $("#availableActions").change(function() { $("#applyAction").attr("disabled", $("#availableActions").prop("selectedIndex") <= 0); }); $("#applyAction").click(function() { var selectedElements = $(".row_selected"); $("#local_validation_table").hide(); if(selectedElements.length > 0) { var inputElements = $("input:hidden", selectedElements); $.get(sTarget, "action=" + $("#availableActions").val() + "&" + inputElements.serialize(), fnCallback, "json"); } else alert("You should select at least one element."); }); } } $.fn.dataTableExt.oApi.fnCreateFilters = function ( oSettings, sDiv, sFilterLabel, bAllColumnsFilter ) { // by default we include the 'All Columns' filter if ( typeof bAllColumnsFilter == "undefined" ) bAllColumnsFilter = true; // IE hack: Form element is not able to get the focus when it is hidden. // That's why we create a form without action to set the focus to its first element. $(sDiv).append($('
')); $(".dataTables_filterDiv").append('' + sFilterLabel + ''); if (bAllColumnsFilter == true) { $(".dataTables_filterDiv").append(''); $(".dataTables_filterDiv").append(''); } else { $(".dataTables_filterDiv").append(''); $(".dataTables_filterDiv").append(''); } $(".dataTables_filterDiv").append(''); var oFilterSpan = $("#filterSpan"), oFilterColumn = $("#filterColumns"); for(var i=0; i' + filterName + '').val(i)); if(oSettings.aoColumns[i].sFilterColumnType=='input') { oFilterSpan.append($('').hide()); } else { var sValues = this.fnGetColumnData(i, true, false); var aoOptions = []; for (var j = 0; j < sValues.length; ++j) { aoOptions.push({ 'option': sValues[j], 'value': sValues[j] }); } oFilterSpan.append($(fnCreateSelect(aoOptions, "select"+i, "filterObject")).hide()); } }; $(oFilterColumn).change(function(){ var iChild = oFilterColumn.prop("selectedIndex"); $(".filterObject", oFilterSpan).hide(); $(".filterObject", oFilterSpan).eq(iChild).css('display','inline'); }); var oTable = this; $("#selectAll").click(function() { $('input:checkbox').not("#show-controlled-aps").not(".export-checkbox").not(".inheritProtected").not(":disabled").not("#selectAll").parent().parent().toggleClass("row_selected", $("#selectAll").is(":checked")); $('input:checkbox').not("#show-controlled-aps").not(".export-checkbox").not(".inheritProtected").not(":disabled").prop("checked", $("#selectAll").is(':checked')); }); $("#applyFilter").click(function(){ //oTable.fnFilterClear(); if($("#filterColumns").val()==-1) { oTable.fnFilter($(".filterObject:nth-child(1)", "#filterSpan").val()); $(".filterObject:nth-child(1)", "#filterSpan").prop("value", ""); }else { var iChild = $("#filterColumns").prop("selectedIndex"); var iFilterByColumn = $("#filterColumns").val(); if (oSettings.aoColumns[iFilterByColumn].bFilterRange) { // If the column supports filter by range // The parameters for the custom filter are passing using the local storage localStorage.setItem(oSettings.nTable.id + "sSearchRange", $(".filterObject", "#filterSpan").eq(iChild).val()); localStorage.setItem(oSettings.nTable.id + "iColumnIndex", iFilterByColumn); oTable.fnDraw(); // Call the custom filter function } else { oTable.fnFilter($(".filterObject", "#filterSpan").eq(iChild).val(), iFilterByColumn); } $(".filterObject", "#filterSpan").prop("value", ""); } oTable.fnUpdateFilterTooltip(); }); oFilterColumn.change(); } $.fn.dataTableExt.oApi.fnUpdateFilterTooltip = function ( oSettings ) { var oTable = this; var tooltipText = ''; // Check for the global filter if(oSettings.oPreviousSearch.sSearch) { tooltipText += 'All columns:' + '' + oSettings.oPreviousSearch.sSearch + '' + ''; } for (col = 0; col < oSettings.aoColumns.length; ++col) { if (oSettings.aoColumns[col].sTitle != '') { var filterName = (typeof oSettings.aoColumns[col].sFilterColumnName == 'undefined' ? oSettings.aoColumns[col].sTitle : oSettings.aoColumns[col].sFilterColumnName); if (oSettings.aoColumns[col].bFilterRange) { var sSearch = localStorage.getItem(oSettings.nTable.id + "sSearchRange"); if (sSearch != null && sSearch != '') { tooltipText += '' + filterName + ':' + '' + sSearch + '' + ''; } } else { if (oSettings.aoPreSearchCols[col].sSearch != '') { tooltipText += '' + filterName + ':' + '' + oSettings.aoPreSearchCols[col].sSearch + '' + ''; } } } } var filterIcon = document.getElementById("qTipFilterIcon"); /* Create a tooltip table with all current filters */ if (filterIcon && tooltipText != '') { tooltipText = "" + tooltipText + "
"; /* Assign the tooltip to the image title and enable the qTip */ filterIcon.title = tooltipText; $j("img#qTipFilterIcon[title]").qtip($j.extend(qTipBaseAttributes, { content: { title: { text: "Current filters" } }, hide: { delay:300, fixed:true }, // hide the tooltip when it is not on focus show: { event: 'mouseover' } })); } } $.fn.dataTableExt.oApi.fnRemoveFilter = function ( oSettings, iCol ) { var oTable = this; if (iCol == -1) { oSettings.oPreviousSearch.sSearch = ''; } else { if (oSettings.aoColumns[iCol].bFilterRange) { localStorage.removeItem(oSettings.nTable.id + "sSearchRange"); localStorage.removeItem(oSettings.nTable.id + "iColumnIndex"); } else { oSettings.aoPreSearchCols[iCol].sSearch = ''; } var aeInputElement = $j('#search-action-box :input'); if (aeInputElement[iCol] != null) { if (aeInputElement[iCol].type == 'text') { aeInputElement[iCol].value = ""; } else if (aeInputElement[iCol].type == 'select-one') { aeInputElement[iCol].selectedIndex = 0; } } } oTable.fnDraw(); oTable.fnUpdateFilterTooltip(); } $.fn.dataTableExt.oApi.fnClearFilters = function () { var oTable = this; // Remove the text from the filtering text fields and set the select index of the selects controls to 0 $j('#search-action-box :input').each(function() { if (this.type == 'text') { this.value = ""; } else if (this.type == 'select-one') { this.selectedIndex = 0; } }); ResetAllDataTablesFilters(oTable); $("#filterColumns").change(); } $.fn.dataTableExt.oApi.fnLoadFiltersData = function ( oSettings ) { for(var i=0; i" + columnData[k] + ""); } } }; } /* * Function: fnGetColumnData * Purpose: Return an array of table values from a particular column. * Returns: array string: 1d data array * Inputs: object:oSettings - dataTable settings object. This is always the last argument past to the function * int:iColumn - the id of the column to extract the data from * bool:bUnique - optional - if set to false duplicated values are not filtered out * bool:bFiltered - optional - if set to false all the table data is used (not only the filtered) * bool:bIgnoreEmpty - optional - if set to false empty values are not filtered from the result array * Author: Benedikt Forchhammer */ $.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) { // check that we have a column id if ( typeof iColumn == "undefined" ) return new Array(); // by default we only wany unique data if ( typeof bUnique == "undefined" ) bUnique = true; // by default we do want to only look at filtered data if ( typeof bFiltered == "undefined" ) bFiltered = true; // by default we do not wany to include empty values if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true; // list of rows which we're going to loop through var aiRows; // use only filtered rows if (bFiltered == true) aiRows = oSettings.aiDisplay; // use all rows else aiRows = oSettings.aiDisplayMaster; // all row numbers // set up data array var asResultData = new Array(); for (var i=0,c=aiRows.length; i -1) continue; // else push the value onto the result data array else asResultData.push(sValue); } return asResultData; } $.fn.dataTableExt.oApi.fnFilterClear = function ( oSettings ) { /* Remove global filter */ oSettings.oPreviousSearch.sSearch = ""; /* Remove the text of the global filter in the input boxes */ if ( typeof oSettings.aanFeatures.f != 'undefined' ) { var n = oSettings.aanFeatures.f; for ( var i=0, iLen=n.length ; i