Advertisement
Guest User

gridmvc.js

a guest
May 26th, 2014
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***
  2. * Grid.Mvc
  3. * Examples and documentation at: http://gridmvc.codeplex.com
  4. * Version: 3.0.0
  5. * Requires: window.jQuery v1.3+
  6. * LGPL license: http://gridmvc.codeplex.com/license
  7. */
  8. window.pageGrids = window.pageGrids || {};
  9. $.fn.extend({
  10.     gridmvc: function () {
  11.         var aObj = [];
  12.         $(this).each(function () {
  13.             if (!$(this).data("gridmvc")) {
  14.                 var options = { lang: $(this).attr("data-lang"), selectable: $(this).attr("data-selectable") == "true", multiplefilters: $(this).attr("data-multiplefilters") == "true" };
  15.                 var grid = new GridMvc(this, options);
  16.                 var name = $(this).attr("data-gridname");
  17.                 if (name.length > 0)
  18.                     window.pageGrids[$(this).attr("data-gridname")] = grid;
  19.  
  20.                 aObj.push(grid);
  21.                 $(this).data("gridmvc", grid);
  22.             } else {
  23.                 aObj.push($(this).data("gridmvc"));
  24.             }
  25.         });
  26.         if (aObj.length == 1)
  27.             return aObj[0];
  28.         return aObj;
  29.     }
  30. });
  31.  
  32. GridMvc = (function ($) {
  33.     function gridMvc(container, options) {
  34.         this.jqContainer = $(container);
  35.         options = options || {};
  36.         this.options = $.extend({}, this.defaults(), options);
  37.         this.init();
  38.     }
  39.  
  40.     gridMvc.prototype.init = function () {
  41.         //load current lang options:
  42.         this.lang = GridMvc.lang[this.options.lang];
  43.         if (typeof (this.lang) == 'undefined')
  44.             this.lang = GridMvc.lang.en;
  45.         this.events = [];
  46.         if (this.options.selectable)
  47.             this.initGridRowsEvents();
  48.         //Register default filter widgets:
  49.         this.filterWidgets = [];
  50.         this.addFilterWidget(new TextFilterWidget());
  51.         this.addFilterWidget(new NumberFilterWidget());
  52.         this.addFilterWidget(new DateTimeFilterWidget());
  53.         this.addFilterWidget(new BooleanFilterWidget());
  54.  
  55.         this.openedMenuBtn = null;
  56.         this.initFilters();
  57.     };
  58.     /***
  59.     * Handle Grid row events
  60.     */
  61.     gridMvc.prototype.initGridRowsEvents = function () {
  62.         var $this = this;
  63.         this.jqContainer.on("click", ".grid-row", function () {
  64.             $this.rowClicked.call(this, $this);
  65.         });
  66.     };
  67.     /***
  68.     * Trigger on Grid row click
  69.     */
  70.     gridMvc.prototype.rowClicked = function ($context) {
  71.         if (!$context.options.selectable)
  72.             return;
  73.         var row = $(this).closest(".grid-row");
  74.         if (row.length <= 0)
  75.             return;
  76.         var gridRow = {};
  77.         row.find(".grid-cell").each(function () {
  78.             var columnName = $(this).attr("data-name");
  79.             if (columnName.length > 0)
  80.                 gridRow[columnName] = $(this).text();
  81.         });
  82.         var evt = $.Event("RowClicked");
  83.         $context.notifyOnRowSelect(gridRow, evt);
  84.         if (!evt.isDefaultPrevented())
  85.             $context.markRowSelected(row);
  86.     };
  87.     /***
  88.     * Mark Grid row as selected
  89.     */
  90.     gridMvc.prototype.markRowSelected = function (row) {
  91.         this.jqContainer.find(".grid-row.grid-row-selected").removeClass("grid-row-selected");
  92.         row.addClass("grid-row-selected");
  93.     };
  94.     /***
  95.     * Default Grid.Mvc options
  96.     */
  97.     gridMvc.prototype.defaults = function () {
  98.         return {
  99.             selectable: true,
  100.             multiplefilters: false,
  101.             lang: 'en'
  102.         };
  103.     };
  104.     /***
  105.     * ============= EVENTS =============
  106.     * Methods that provides functionality for grid events
  107.     */
  108.     gridMvc.prototype.onRowSelect = function (func) {
  109.         this.events.push({ name: "onRowSelect", callback: func });
  110.     };
  111.  
  112.     gridMvc.prototype.notifyOnRowSelect = function (row, e) {
  113.         e.row = row;
  114.         this.notifyEvent("onRowSelect", e);
  115.     };
  116.  
  117.     gridMvc.prototype.notifyEvent = function (eventName, e) {
  118.         for (var i = 0; i < this.events.length; i++) {
  119.             if (this.events[i].name == eventName)
  120.                 if (!this.events[i].callback(e)) break;
  121.         }
  122.     };
  123.     /***
  124.     * ============= FILTERS =============
  125.     * Methods that provides functionality for filtering
  126.     */
  127.     /***
  128.     * Method search all filter buttons and register 'onclick' event handlers:
  129.     */
  130.     gridMvc.prototype.initFilters = function () {
  131.         var filterHtml = this.filterMenuHtml();
  132.         var self = this;
  133.         this.jqContainer.find(".grid-filter").each(function () {
  134.             $(this).click(function () {
  135.                 return self.openFilterPopup.call(this, self, filterHtml);
  136.             });
  137.         });
  138.     };
  139.     /***
  140.     * Shows filter popup window and render filter widget
  141.     */
  142.     gridMvc.prototype.openFilterPopup = function (self, html) {
  143.         //retrive all column filter parameters from html attrs:
  144.         var columnType = $(this).attr("data-type") || "";
  145.         //determine widget
  146.         var widget = self.getFilterWidgetForType(columnType);
  147.         //if widget for specified column type not found - do nothing
  148.         if (widget == null)
  149.             return false;
  150.  
  151.         //if widget allready rendered - just open popup menu:
  152.         if (this.hasAttribute("data-rendered")) {
  153.             var or = self.openMenuOnClick.call(this, self);
  154.             self.setupPopupInitialPosition($(this));
  155.             if (!or && typeof (widget.onShow) != 'undefined')
  156.                 widget.onShow();
  157.             return or;
  158.         }
  159.  
  160.         var columnName = $(this).attr("data-name") || "";
  161.         var filterData = $(this).attr("data-filterdata") || "";
  162.         var widgetData = $(this).attr("data-widgetdata") || "{}";
  163.         var filterDataObj = self.parseFilterValues(filterData) || {};
  164.         var filterUrl = $(this).attr("data-url") || "";
  165.  
  166.         //mark filter as rendered
  167.         $(this).attr("data-rendered", "1");
  168.         //append base popup layout:
  169.         $(this).append(html);
  170.  
  171.         //determine widget container:
  172.         var widgetContainer = $(this).find(".grid-popup-widget");
  173.         //onRender target widget
  174.         if (typeof (widget.onRender) != 'undefined')
  175.             widget.onRender(widgetContainer, self.lang, columnType, filterDataObj, function (values) {
  176.                 self.closeOpenedPopups();
  177.                 self.applyFilterValues(filterUrl, columnName, values, false);
  178.             }, $.parseJSON(widgetData));
  179.         //adding 'clear filter' button if needed:
  180.         if ($(this).find(".grid-filter-btn").hasClass("filtered") && widget.showClearFilterButton()) {
  181.             var inner = $(this).find(".grid-popup-additional");
  182.             inner.append(self.getClearFilterButton(filterUrl));
  183.             inner.find(".grid-filter-clear").click(function () {
  184.                 self.applyFilterValues(filterUrl, columnName, "", true);
  185.             });
  186.         }
  187.         var openResult = self.openMenuOnClick.call(this, self);
  188.         if (typeof (widget.onShow) != 'undefined')
  189.             widget.onShow();
  190.         self.setupPopupInitialPosition($(this));
  191.         return openResult;
  192.     };
  193.  
  194.     gridMvc.prototype.setupPopupInitialPosition = function (popup) {
  195.         var drop = popup.find(".grid-dropdown");
  196.         function getInfo() {
  197.             var arrow = popup.find(".grid-dropdown-arrow");
  198.             return { arrow: arrow, currentDropLeft: parseInt(drop.css("left")), currentArrowLeft: parseInt(arrow.css("left")) };
  199.         }
  200.         var dropLeft = drop.offset().left;
  201.         if (dropLeft < 0) {
  202.             var info = getInfo();
  203.             info.arrow.css({ left: (info.currentArrowLeft + dropLeft - 10) + "px" });
  204.             drop.css({ left: (info.currentDropLeft - dropLeft + 10) + "px" });
  205.             return;
  206.         }
  207.         var dropWidth = drop.width();
  208.         var offsetRight = $(window).width() + $(window).scrollLeft() - (dropLeft + dropWidth);
  209.         if (offsetRight < 0) {
  210.             var info = getInfo();
  211.             info.arrow.css({ left: (info.currentArrowLeft - offsetRight + 5) + "px" });
  212.             drop.css({ left: (info.currentDropLeft + offsetRight - 5) + "px" });
  213.         }
  214.     };
  215.     /***
  216.     * Returns layout of filter popup menu
  217.     */
  218.     gridMvc.prototype.filterMenuHtml = function () {
  219.         return '<div class="dropdown dropdown-menu grid-dropdown" style="display: none;">\
  220.                    <div class="grid-dropdown-arrow"></div>\
  221.                    <div class="grid-dropdown-inner">\
  222.                            <div class="grid-popup-widget"></div>\
  223.                            <div class="grid-popup-additional"></div>\
  224.                    </div>\
  225.                </div>';
  226.     };
  227.     /***
  228.     * Returns layout of 'clear filter' button
  229.     */
  230.     gridMvc.prototype.getClearFilterButton = function () {
  231.         return '<ul class="menu-list">\
  232.                    <li><a class="grid-filter-clear" href="javascript:void(0);">' + this.lang.clearFilterLabel + '</a></li>\
  233.                </ul>';
  234.     };
  235.     /***
  236.     * Register filter widget in widget collection
  237.     */
  238.     gridMvc.prototype.addFilterWidget = function (widget) {
  239.         this.filterWidgets.push(widget);
  240.     };
  241.     /***
  242.     * Parse filter settings from data attribute
  243.     */
  244.     gridMvc.prototype.parseFilterValues = function (filterData) {
  245.         var opt = $.parseJSON(filterData);
  246.         var filters = [];
  247.         for (var i = 0; i < opt.length; i++) {
  248.             filters.push({ filterValue: this.urldecode(opt[i].FilterValue), filterType: opt[i].FilterType, columnName: opt[i].ColumnName });
  249.         }
  250.         return filters;
  251.     };
  252.  
  253.     gridMvc.prototype.urldecode = function (str) {
  254.         return decodeURIComponent((str + '').replace(/\+/g, '%20'));
  255.     };
  256.  
  257.     /***
  258.     * Return registred widget for specific column type name
  259.     */
  260.     gridMvc.prototype.getFilterWidgetForType = function (typeName) {
  261.         for (var i = 0; i < this.filterWidgets.length; i++) {
  262.             if ($.inArray(typeName, this.filterWidgets[i].getAssociatedTypes()) >= 0)
  263.                 return this.filterWidgets[i];
  264.         }
  265.         return null;
  266.     };
  267.     /***
  268.     * Replace existed filter widget
  269.     */
  270.     gridMvc.prototype.replaceFilterWidget = function (typeNameToReplace, widget) {
  271.         for (var i = 0; i < this.filterWidgets.length; i++) {
  272.             if ($.inArray(typeNameToReplace, this.filterWidgets[i].getAssociatedTypes()) >= 0) {
  273.                 this.filterWidgets.splice(i, 1);
  274.                 this.addFilterWidget(widget);
  275.                 return true;
  276.             }
  277.         }
  278.         return false;
  279.     };
  280.     /***
  281.     * Applies selected filter value by redirecting to another url:
  282.     */
  283.     gridMvc.prototype.applyFilterValues = function (initialUrl, columnName, values, skip) {
  284.         var filters = this.jqContainer.find(".grid-filter");
  285.         if (initialUrl.length > 0)
  286.             initialUrl += "&";
  287.  
  288.         var url = "";
  289.         if (!skip) {
  290.             url += this.getFilterQueryData(columnName, values);
  291.         }
  292.  
  293.         if (this.options.multiplefilters) { //multiple filters enabled
  294.             for (var i = 0; i < filters.length; i++) {
  295.                 if ($(filters[i]).attr("data-name") != columnName) {
  296.                     var filterData = this.parseFilterValues($(filters[i]).attr("data-filterdata"));
  297.                     if (filterData.length == 0) continue;
  298.                     if (url.length > 0) url += "&";
  299.                     url += this.getFilterQueryData($(filters[i]).attr("data-name"), filterData);
  300.                 } else {
  301.                     continue;
  302.                 }
  303.             }
  304.         }
  305.         //alert(document.URL + initialUrl + url + "#gridDiv");
  306.         $("#gridDiv").slideUp();
  307.         $("#gridDiv").load(document.URL +"?"+ initialUrl + url + " #gridDiv", initGrid);
  308.  
  309.         //window.location.search = initialUrl + url;
  310.  
  311.     };
  312.     gridMvc.prototype.getFilterQueryData = function (columnName, values) {
  313.         var url = "";
  314.         for (var i = 0; i < values.length; i++) {
  315.             url += "grid-filter=" + encodeURIComponent(columnName) + "__" + values[i].filterType + "__" + encodeURIComponent(values[i].filterValue);
  316.             if (i != values.length - 1)
  317.                 url += "&";
  318.         }
  319.         return url;
  320.     };
  321.     /***
  322.     * ============= POPUP MENU =============
  323.     * Methods that provides base functionality for popup menus
  324.     */
  325.     gridMvc.prototype.openMenuOnClick = function (self) {
  326.         if ($(this).hasClass("clicked")) return true;
  327.         self.closeOpenedPopups();
  328.         $(this).addClass("clicked");
  329.         var popup = $(this).find(".dropdown-menu");
  330.         if (popup.length == 0) return true;
  331.         popup.show();
  332.         popup.addClass("opened");
  333.         self.openedMenuBtn = $(this);
  334.         $(document).bind("click.gridmvc", function (e) {
  335.             self.documentCallback(e, self);
  336.         });
  337.         return false;
  338.     };
  339.  
  340.     gridMvc.prototype.documentCallback = function (e, $context) {
  341.         e = e || event;
  342.         var target = e.target || e.srcElement;
  343.         var box = $(".dropdown-menu.opened").get(0);
  344.         var html = $("html").get(0);
  345.         if (typeof box != "undefined") {
  346.             do {
  347.                 if (box == target) {
  348.                     // Click occured inside the box, do nothing.
  349.                     return;
  350.                 }
  351.                 if (html == target) {
  352.                     box.style.display = "none";
  353.                     $(box).removeClass("opened");
  354.                 }
  355.                 target = target.parentNode;
  356.             } while (target); // Click was outside the box, hide it.
  357.  
  358.         }
  359.         if ($context.openedMenuBtn != null)
  360.             $context.openedMenuBtn.removeClass("clicked");
  361.         $(document).unbind("click.gridmvc");
  362.     };
  363.  
  364.     gridMvc.prototype.closeOpenedPopups = function () {
  365.         var openedPopup = $(".dropdown-menu.opened");
  366.         openedPopup.hide();
  367.         openedPopup.removeClass("opened");
  368.         if (this.openedMenuBtn != null)
  369.             this.openedMenuBtn.removeClass("clicked");
  370.     };
  371.  
  372.     /* Grid.Mvc clients functions */
  373.  
  374.     gridMvc.prototype.selectable = function (enable) {
  375.         this.options.selectable = enable;
  376.     };
  377.  
  378.     return gridMvc;
  379. })(window.jQuery);
  380.  
  381. /***
  382. * ============= LOCALIZATION =============
  383. * You can localize Grid.Mvc by creating localization files and include it on the page after this script file
  384. * This script file provides localization only for english language.
  385. * For more documentation see: http://gridmvc.codeplex.com/documentation
  386. */
  387. if (typeof (GridMvc.lang) == 'undefined')
  388.     GridMvc.lang = {};
  389. GridMvc.lang.en = {
  390.     filterTypeLabel: "Type: ",
  391.     filterValueLabel: "Value:",
  392.     applyFilterButtonText: "Apply",
  393.     filterSelectTypes: {
  394.         Equals: "Equals",
  395.         StartsWith: "StartsWith",
  396.         Contains: "Contains",
  397.         EndsWith: "EndsWith",
  398.         GreaterThan: "Greater than",
  399.         LessThan: "Less than",
  400.         GreaterThanOrEquals: "Greater than or equals",
  401.         LessThanOrEquals: "Less than or equals"
  402.     },
  403.     code: 'en',
  404.     boolTrueLabel: "Yes",
  405.     boolFalseLabel: "No",
  406.     clearFilterLabel: "Clear filter"
  407. };
  408. /***
  409. * ============= FILTER WIDGETS =============
  410. * Filter widget allows onRender custom filter user interface for different columns.
  411. * For example if your added column is of type "DateTime" - widget can onRender calendar for picking filter value.
  412. * This script provider base widget for default .Net types: System.String, System.Int32, System.Decimal etc.
  413. * If you want to provide custom filter functionality - you can assign your own widget type for column and write widget for this types.
  414. * For more documentation see: http://gridmvc.codeplex.com/documentation
  415. */
  416.  
  417. /***
  418. * TextFilterWidget - Provides filter interface for text columns (of type "System.String")
  419. * This widget onRenders filter interface with conditions, which specific for text types: contains, starts with etc.
  420. */
  421. TextFilterWidget = (function ($) {
  422.     function textFilterWidget() { }
  423.     /***
  424.     * This method must return type of columns that must be associated with current widget
  425.     * If you not specify your own type name for column (see 'SetFilterWidgetType' method), GridMvc setup column type name from .Net type ("System.DateTime etc.)
  426.     */
  427.     textFilterWidget.prototype.getAssociatedTypes = function () { return ["System.String"]; };
  428.     /***
  429.     * This method invokes when filter widget was shown on the page
  430.     */
  431.     textFilterWidget.prototype.onShow = function () {
  432.         var textBox = this.container.find(".grid-filter-input");
  433.         if (textBox.length <= 0) return;
  434.         textBox.focus();
  435.     };
  436.     /***
  437.     * This method specify whether onRender 'Clear filter' button for this widget.
  438.     */
  439.     textFilterWidget.prototype.showClearFilterButton = function () { return true; };
  440.     /***
  441.     * This method will invoke when user first clicked on filter button.
  442.     * container - html element, which must contain widget layout;
  443.     * lang - current language settings;
  444.     * typeName - current column type (if widget assign to multipile types, see: getAssociatedTypes);
  445.     * filterType - current filter type (like equals, starts with etc);
  446.     * filterValue - current filter value;
  447.     * cb - callback function that must invoked when user want to filter this column. Widget must pass filter type and filter value.
  448.     */
  449.     textFilterWidget.prototype.onRender = function (container, lang, typeName, values, cb) {
  450.         this.cb = cb;
  451.         this.container = container;
  452.         this.lang = lang;
  453.         this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
  454.         this.renderWidget();
  455.         this.registerEvents();
  456.     };
  457.     /***
  458.     * Internal method that build widget layout and append it to the widget container
  459.     */
  460.     textFilterWidget.prototype.renderWidget = function () {
  461.         var html = '<div class="form-group">\
  462.                        <label>' + this.lang.filterTypeLabel + '</label>\
  463.                        <select class="grid-filter-type form-control">\
  464.                            <option value="1" ' + (this.value.filterType == "1" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Equals + '</option>\
  465.                            <option value="2" ' + (this.value.filterType == "2" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Contains + '</option>\
  466.                            <option value="3" ' + (this.value.filterType == "3" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.StartsWith + '</option>\
  467.                            <option value="4" ' + (this.value.filterType == "4" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.EndsWith + '</option>\
  468.                        </select>\
  469.                    </div>\
  470.                    <div class="form-group">\
  471.                        <label>' + this.lang.filterValueLabel + '</label>\
  472.                        <input type="text" class="grid-filter-input form-control" value="' + this.value.filterValue + '" />\
  473.                    </div>\
  474.                    <div class="grid-filter-buttons">\
  475.                        <button type="button" class="btn btn-primary grid-apply" >' + this.lang.applyFilterButtonText + '</button>\
  476.                    </div>';
  477.         this.container.append(html);
  478.     };
  479.     /***
  480.     * Internal method that register event handlers for 'apply' button.
  481.     */
  482.     textFilterWidget.prototype.registerEvents = function () {
  483.         //get apply button from:
  484.         var applyBtn = this.container.find(".grid-apply");
  485.         //save current context:
  486.         var $context = this;
  487.         //register onclick event handler
  488.         applyBtn.click(function () {
  489.             //get selected filter type:
  490.             var type = $context.container.find(".grid-filter-type").val();
  491.             //get filter value:
  492.             var value = $context.container.find(".grid-filter-input").val();
  493.             //invoke callback with selected filter values:
  494.             var filterValues = [{ filterType: type, filterValue: value }];
  495.             $context.cb(filterValues);
  496.         });
  497.         //register onEnter event for filter text box:
  498.         this.container.find(".grid-filter-input").keyup(function (event) {
  499.             if (event.keyCode == 13) { applyBtn.click(); }
  500.             if (event.keyCode == 27) { GridMvc.closeOpenedPopups(); }
  501.         });
  502.     };
  503.  
  504.     return textFilterWidget;
  505. })(window.jQuery);
  506.  
  507. /***
  508. * NumberFilterWidget - Provides filter interface for number columns
  509. * This widget onRenders filter interface with conditions, which specific for number types: great than, less that etc.
  510. * Also validates user's input for digits
  511. */
  512. NumberFilterWidget = (function ($) {
  513.  
  514.     function numberFilterWidget() { }
  515.  
  516.     numberFilterWidget.prototype.showClearFilterButton = function () { return true; };
  517.  
  518.     numberFilterWidget.prototype.getAssociatedTypes = function () {
  519.         return ["System.Int32", "System.Double", "System.Decimal", "System.Byte", "System.Single", "System.Float", "System.Int64", "System.Int16"];
  520.     };
  521.  
  522.     numberFilterWidget.prototype.onShow = function () {
  523.         var textBox = this.container.find(".grid-filter-input");
  524.         if (textBox.length <= 0) return;
  525.         textBox.focus();
  526.     };
  527.  
  528.     numberFilterWidget.prototype.onRender = function (container, lang, typeName, values, cb) {
  529.         this.cb = cb;
  530.         this.container = container;
  531.         this.lang = lang;
  532.         this.typeName = typeName;
  533.         this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
  534.         this.renderWidget();
  535.         this.registerEvents();
  536.     };
  537.  
  538.     numberFilterWidget.prototype.renderWidget = function () {
  539.         var html = '<div class="form-group">\
  540.                        <label>' + this.lang.filterTypeLabel + '</label>\
  541.                        <select class="grid-filter-type form-control">\
  542.                            <option value="1" ' + (this.value.filterType == "1" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Equals + '</option>\
  543.                            <option value="5" ' + (this.value.filterType == "5" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.GreaterThan + '</option>\
  544.                            <option value="6" ' + (this.value.filterType == "6" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.LessThan + '</option>\
  545.                        </select>\
  546.                    </div>\
  547.                    <div class="form-group">\
  548.                        <label>' + this.lang.filterValueLabel + '</label>\
  549.                        <input type="text" class="grid-filter-input form-control" value="' + this.value.filterValue + '" />\
  550.                    </div>\
  551.                    <div class="grid-filter-buttons">\
  552.                        <button type="button" class="btn btn-primary grid-apply">' + this.lang.applyFilterButtonText + '</button>\
  553.                    </div>';
  554.         this.container.append(html);
  555.     };
  556.  
  557.     numberFilterWidget.prototype.registerEvents = function () {
  558.         var $context = this;
  559.         var applyBtn = this.container.find(".grid-apply");
  560.         applyBtn.click(function () {
  561.             var type = $context.container.find(".grid-filter-type").val();
  562.             var value = $context.container.find(".grid-filter-input").val();
  563.             var filters = [{ filterType: type, filterValue: value }];
  564.             $context.cb(filters);
  565.         });
  566.         var txt = this.container.find(".grid-filter-input");
  567.         txt.keyup(function (event) {
  568.             if (event.keyCode == 13) { applyBtn.click(); }
  569.             if (event.keyCode == 27) { GridMvc.closeOpenedPopups(); }
  570.         })
  571.             .keypress(function (event) { return $context.validateInput.call($context, event); });
  572.         if (this.typeName == "System.Byte")
  573.             txt.attr("maxlength", "3");
  574.     };
  575.  
  576.     numberFilterWidget.prototype.validateInput = function (evt) {
  577.         var $event = evt || window.event;
  578.         var key = $event.keyCode || $event.which;
  579.         key = String.fromCharCode(key);
  580.         var regex;
  581.         switch (this.typeName) {
  582.             case "System.Byte":
  583.             case "System.Int32":
  584.             case "System.Int64":
  585.                 regex = /[0-9]/;
  586.                 break;
  587.             default:
  588.                 regex = /[0-9]|\.|\,/;
  589.         }
  590.         if (!regex.test(key)) {
  591.             $event.returnValue = false;
  592.             if ($event.preventDefault) $event.preventDefault();
  593.         }
  594.     };
  595.  
  596.     return numberFilterWidget;
  597. })(window.jQuery);
  598.  
  599. /***
  600. * DateTimeFilterWidget - Provides filter interface for date columns (of type "System.DateTime").
  601. * If datepicker script included, this widget will render calendar for pick filter values
  602. * In other case he onRender textbox field for specifing date value (more info at http://window.jQueryui.com/)
  603. */
  604. DateTimeFilterWidget = (function ($) {
  605.  
  606.     function dateTimeFilterWidget() { }
  607.  
  608.     dateTimeFilterWidget.prototype.getAssociatedTypes = function () { return ["System.DateTime", "System.Date", "System.DateTimeOffset"]; };
  609.  
  610.     dateTimeFilterWidget.prototype.showClearFilterButton = function () { return true; };
  611.  
  612.     dateTimeFilterWidget.prototype.onRender = function (container, lang, typeName, values, applycb, data) {
  613.         this.datePickerIncluded = typeof ($.fn.datepicker) != 'undefined';
  614.         this.cb = applycb;
  615.         this.data = data;
  616.         this.typeName = typeName;
  617.         this.container = container;
  618.         this.lang = lang;
  619.         this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
  620.         this.renderWidget();
  621.         this.registerEvents();
  622.     };
  623.  
  624.     dateTimeFilterWidget.prototype.renderWidget = function () {
  625.         var html = '<div class="form-group">\
  626.                        <label>' + this.lang.filterTypeLabel + '</label>\
  627.                        <select class="grid-filter-type form-control">\
  628.                            <option value="1" ' + (this.value.filterType == "1" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.Equals + '</option>\
  629.                            <option value="5" ' + (this.value.filterType == "5" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.GreaterThan + '</option>\
  630.                            <option value="6" ' + (this.value.filterType == "6" ? "selected=\"selected\"" : "") + '>' + this.lang.filterSelectTypes.LessThan + '</option>\
  631.                        </select>\
  632.                    </div>' +
  633.                         (this.datePickerIncluded ?
  634.                             '<div class="grid-filter-datepicker"></div>'
  635.                             :
  636.                             '<div class="form-group">\
  637.                                <label>' + this.lang.filterValueLabel + '</label>\
  638.                                <input type="text" class="grid-filter-input form-control" value="' + this.value.filterValue + '" />\
  639.                             </div>\
  640.                             <div class="grid-filter-buttons">\
  641.                                <input type="button" class="btn btn-primary grid-apply" value="' + this.lang.applyFilterButtonText + '" />\
  642.                             </div>');
  643.         this.container.append(html);
  644.         //if window.jQueryUi included:
  645.         if (this.datePickerIncluded) {
  646.             var datePickerOptions = this.data || {};
  647.             datePickerOptions.format = datePickerOptions.format || "yyyy-mm-dd";
  648.             datePickerOptions.language = datePickerOptions.language || this.lang.code;
  649.  
  650.             var $context = this;
  651.             var dateContainer = this.container.find(".grid-filter-datepicker");
  652.             dateContainer.datepicker(datePickerOptions).on('changeDate', function (ev) {
  653.                 var type = $context.container.find(".grid-filter-type").val();
  654.                 //if (type == "1") {
  655.                 //    var tomorrow = new Date(ev.getTime());
  656.                 //    tomorrow.setDate(ev.getDate() + 1);
  657.                 //    var filterValues = [{ filterType: type, filterValue: ev.format() }];
  658.                 //}
  659.                 //else{
  660.                     var filterValues = [{ filterType: type, filterValue: ev.format() }];
  661.                 //}
  662.                 $context.cb(filterValues);
  663.             });
  664.             if (this.value.filterValue)
  665.                 dateContainer.datepicker('update', this.value.filterValue);
  666.         }
  667.     };
  668.  
  669.     dateTimeFilterWidget.prototype.registerEvents = function () {
  670.         var $context = this;
  671.         var applyBtn = this.container.find(".grid-apply");
  672.         applyBtn.click(function () {
  673.             var type = $context.container.find(".grid-filter-type").val();
  674.             var value = $context.container.find(".grid-filter-input").val();
  675.             var filterValues = [{ filterType: type, filterValue: value }];
  676.             $context.cb(filterValues);
  677.         });
  678.         this.container.find(".grid-filter-input").keyup(function (event) {
  679.             if (event.keyCode == 13) {
  680.                 applyBtn.click();
  681.             }
  682.         });
  683.     };
  684.  
  685.     return dateTimeFilterWidget;
  686. })(window.jQuery);
  687.  
  688. /***
  689. * BooleanFilterWidget - Provides filter interface for boolean columns (of type "System.Boolean").
  690. * Renders two button for filter - true and false
  691. */
  692. BooleanFilterWidget = (function ($) {
  693.  
  694.     function booleanFilterWidget() { }
  695.  
  696.     booleanFilterWidget.prototype.getAssociatedTypes = function () { return ["System.Boolean"]; };
  697.  
  698.     booleanFilterWidget.prototype.showClearFilterButton = function () { return true; };
  699.  
  700.     booleanFilterWidget.prototype.onRender = function (container, lang, typeName, values, cb) {
  701.         this.cb = cb;
  702.         this.container = container;
  703.         this.lang = lang;
  704.         this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };//support only one filter value
  705.         this.renderWidget();
  706.         this.registerEvents();
  707.     };
  708.  
  709.     booleanFilterWidget.prototype.renderWidget = function () {
  710.         var html = '<label>' + this.lang.filterValueLabel + '</label>\
  711.                    <ul class="menu-list">\
  712.                        <li><a class="grid-filter-choose ' + (this.value.filterValue == "true" ? "choose-selected" : "") + '" data-value="true" href="javascript:void(0);">' + this.lang.boolTrueLabel + '</a></li>\
  713.                        <li><a class="grid-filter-choose ' + (this.value.filterValue == "false" ? "choose-selected" : "") + '" data-value="false" href="javascript:void(0);">' + this.lang.boolFalseLabel + '</a></li>\
  714.                    </ul>';
  715.         this.container.append(html);
  716.     };
  717.  
  718.     booleanFilterWidget.prototype.registerEvents = function () {
  719.         var $context = this;
  720.         var applyBtn = this.container.find(".grid-filter-choose");
  721.         applyBtn.click(function () {
  722.             var filterValues = [{ filterType: "1", filterValue: $(this).attr("data-value") }];
  723.             $context.cb(filterValues);
  724.         });
  725.     };
  726.  
  727.     return booleanFilterWidget;
  728. })(window.jQuery);
  729.  
  730. //startup init:
  731. (function ($) {
  732.     if (!$) return;//jquery not referenced
  733.     $(function () {
  734.  
  735.  
  736.     initGrid();
  737. //        $(".grid-mvc").each(function () {
  738.   //          $(".grid-mvc").gridmvc();
  739.     //    });
  740.     });
  741. })(window.jQuery);
  742.  
  743. initGrid = function(){
  744. $("#gridDiv").slideDown();
  745.     $(".grid-mvc").each(function () {
  746.         $(".grid-mvc").gridmvc();
  747.     });
  748.             $(".grid-mvc .pagination a ").click(function(e){
  749.                                                 e.preventDefault();
  750.                                                  $("#gridDiv").slideUp();
  751.                                                  $("#gridDiv").load(document.URL +$(this).attr('href') + " #gridDiv", initGrid);
  752.                                                
  753.  
  754.                                                 });
  755.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement