Showing posts with label extensibility. Show all posts
Showing posts with label extensibility. Show all posts

Wednesday, October 15, 2014

Some useful jQuery Extentions


  • Extended ':contains'-selector that ignores case while comparing text
$.expr[':'].containsIgnoreCase = function (n, i, m) {
    /// <summary>
    /// This is an overridden selector of Jquery selector - ":contains".
    /// This selector works as it is alike ":contains" with additional ability of case insensitivity.
    /// </summary>
    return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};

Update:

In jQuery 1.8 the API for this changed, the jQuery 1.8+ version of this would be:
jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {      return function( elem ) {            return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;      };});
Reference: http://stackoverflow.com/a/2196683
  • Extended 'attr'-function that appends value, instead replace
/*
 * .attrExtend(attrName, attrValue)
 * Adds the specified attrValue for specified attrName to each of the set of matched HTML elements.
 */
$.fn.attrExtend = function (attrName, attrValue) {
    /// <summary>
    /// Adds the specified attrValue for specified attrName to each of the set of matched HTML elements.
    /// </summary>
    /// <param name="attrName" type="string">Attribute name, e.g. class</param>
    /// <param name="attrValue" type="string">Attribute value, e.g. Testclass</param>
    $(this).attr(attrName, function (index, existingAttrValues) {
        var re = new RegExp(attrValue, 'g');
        if (existingAttrValues
            && typeof (existingAttrValues) == 'string') {
            if (existingAttrValues.match(re) != null
            && existingAttrValues.match(re).length > 0) {
                return existingAttrValues;
            }
            else {
                return existingAttrValues + ' ' + attrValue;
            }
        }
        else {
            return attrValue;
        }
    });
    return this;
};
  • Extended 'removeAttr'-function that removes value, instead remove whole attribute
/*
 * .removeAttrExtend(attrName, attrValue)
 * Removes the specified attrValue for specified attrName to each of the set of matched HTML elements.
 */
$.fn.removeAttrExtend = function (attrName, attrValue) {
    /// <summary>
    /// Removes the specified attrValue for specified attrName to each of the set of matched HTML elements.
    /// </summary>
    /// <param name="attrName" type="string">Attribute name, e.g. class</param>
    /// <param name="attrValue" type="string">Attribute value, e.g. Testclass</param>
    $(this).attr(attrName, function (index, existingAttrValues) {
        var re = new RegExp(attrValue, 'g');
        if (existingAttrValues
            && typeof (existingAttrValues) == 'string') {
            return existingAttrValues.replace(re, '');
        }
        else {
            return existingAttrValues;
        }
    });
    return this;
};

Update:

  • Extended version of JavaScript method - "hasOwnProperty", that checks if JavaScript object has property that contains string specified by "likeString".
$.hasOwnPropertyLike = function (obj, likeString) {
    /// <summary>
    /// This is an extended version of JavaScript method - "hasOwnProperty".
    /// This method checks if JavaScript object has property that contains string specified by "likeString"
    /// </summary>
    /// <remarks>
    /// Creator:    Vihang Shah
    /// Created on: 21-Apr-2015
    /// Modification History:
    /// Modified by     |       Date        |   Description
    ///
    /// </remarks>
    return ($.propertiesLike(obj, likeString).length > 0);
}

  •  Method enlists property, JavaScript object has and those contains string specified by "likeString",  and returns the list of property-names.
$.propertiesLike = function (obj, likeString) {
    /// <summary>
    /// This method enlists property, JavaScript object has and those contains string specified by "likeString",
    /// and returns the list of property-names
    /// </summary>
    /// <remarks>
    /// Creator:    Vihang Shah
    /// Created on: 21-Apr-2015
    /// Modification History:
    /// Modified by     |       Date        |   Description
    ///
    /// </remarks>
    var properties = [];
    $.each(obj, function (propertyKey, property) {
        if (obj.hasOwnProperty(property)
            && propertyKey.toLowerCase().indexOf(likeString.toLowerCase()) >= 0) {
            properties.push(propertyKey);
        }
    });
    return properties;
}

  •  Javascript : Getting and Setting Caret Position in Textarea
$.fn.getCaret = function () { // adapted from http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea
    var ctrl = this[0];
    var CaretPos = 0; // IE Support
    if (document.selection) {
        ctrl.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    } else if (ctrl.selectionStart || ctrl.selectionStart == '0') { // Firefox support
        CaretPos = ctrl.selectionStart;
    }
    return (CaretPos);
};

  •  priceField

$.fn.priceField = function () {
    $(this).keydown(function (e) {
        var val = $(this).val();
        var code = (e.keyCode ? e.keyCode : e.which);
        var nums = ((code >= 96) && (code <= 105)) || ((code >= 48) && (code <= 57)); //keypad || regular
        var backspace = (code == 8);
        var specialkey = (e.metaKey || e.altKey || e.shiftKey);
        var arrowkey = ((code >= 37) && (code <= 40));
        if (specialkey && !arrowkey)
            return false;
        var Fkey = ((code >= 112) && (code <= 123));
        var decimal = ((code == 110 || code == 190) && val.indexOf('.') == -1);
        // UGLY!!
        var misckey = (code == 9) || (code == 144) || (code == 145) || (code == 45) || (code == 46) || (code == 33) || (code == 34) || (code == 35) || (code == 36) || (code == 19) || (code == 20) || (code == 92) || (code == 93) || (code == 27);
        var properKey = (nums || decimal || backspace || specialkey || arrowkey || Fkey || misckey);
        var properFormatting = backspace || specialkey || arrowkey || Fkey || misckey || ((val.indexOf('.') == -1) || (val.length - val.indexOf('.') < 3) || ($(this).getCaret() < val.length - 2));
        if (!(properKey && properFormatting)) {
            return false;
        }
    });
    $(this).blur(function () {
        var val = $(this).val();
        if (val === '') {
            $(this).val('0.00');
        } else if (val.indexOf('.') == -1) {
            $(this).val(val + '.00');
        } else if (val.length - val.indexOf('.') == 1) {
            $(this).val(val + '00');
        } else if (val.length - val.indexOf('.') == 2) {
            $(this).val(val + '0');
        }
    });
    return $(this);
};

Tuesday, November 20, 2012

Collapse Selection in Solution Explorer extension

After much feedback asking for a feature that would recursively collapse nodes in the solution explorer. Visual Studio Project Team are happy to announce the Collapse Selection in Solution Explorer extension!


What is the Collapse Selection in Solution Explorer extension?
Many Visual Studio users have a solution tree structure with multiple projects and solution folders that are organized as a deep hierarchy. To avoid visual pollution, users want only those projects expanded that are being actively developed (often with many of the project’s child nodes recursively collapsed). To accomplish this task, they have to manually collapse each node recursively – an onerous chore if one has a deeply nested solution tree. The Collapse Selection in Solution Explorer extension addresses this particular issue by enabling the user to recursively collapse multiple nodes with a single click.
How do I install the extension?
1. You can download the extension from the VS Gallery. Double-click the downloaded VSIX file and the extension will be installed.
2. You can navigate to Tools –> Extension Manager, and search for “Collapse Selection in Solution Explorer” in the Online Gallery. Simply download and click Install.
How does the extension work?
Once the extension is installed, you will see a button appear in the Solution Explorer bar. Choose any collapsible node (any node with children nodes; i.e. parent node) in the Solution Explorer and click the Collapse Selection button. All the children nodes will be recursively collapsed to show only the parent node. When you click the expand/collapse icon next to the just collapsed parent node, the parent node will expand to list all the children nodes which will appear collapsed if they have their own children nodes (see figure below). You can select the solution node and click the Collapse Selection button to recursively collapse all nodes in the solution tree. You can also select multiple collapsible nodes and click the Collapse Selection button to recursively collapse each of them.

I prefer using my keyboard. Is it possible to use this extension through keyboard shortcuts?
Definitely. Ensure that the focus is on the Solution Explorer and press Alt + Num(-) to collapse selected nodes. You can also use the Command Window by entering the following command: View.CollapseSelectionInSolutionExplorer.
Is this extension limited to certain project types?
No. This extension should work with all project types.

Source: From Here



Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps