/*! * 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=''; } 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('
').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