LaravelTest
2516 строк · 74.2 Кб
1/*
2* jsGrid v1.5.3 (http://js-grid.com)
3* (c) 2016 Artem Tabalin
4* Licensed under MIT (https://github.com/tabalinas/jsgrid/blob/master/LICENSE)
5*/
6
7(function(window, $, undefined) {8
9var JSGRID = "JSGrid",10JSGRID_DATA_KEY = JSGRID,11JSGRID_ROW_DATA_KEY = "JSGridItem",12JSGRID_EDIT_ROW_DATA_KEY = "JSGridEditRow",13
14SORT_ORDER_ASC = "asc",15SORT_ORDER_DESC = "desc",16
17FIRST_PAGE_PLACEHOLDER = "{first}",18PAGES_PLACEHOLDER = "{pages}",19PREV_PAGE_PLACEHOLDER = "{prev}",20NEXT_PAGE_PLACEHOLDER = "{next}",21LAST_PAGE_PLACEHOLDER = "{last}",22PAGE_INDEX_PLACEHOLDER = "{pageIndex}",23PAGE_COUNT_PLACEHOLDER = "{pageCount}",24ITEM_COUNT_PLACEHOLDER = "{itemCount}",25
26EMPTY_HREF = "javascript:void(0);";27
28var getOrApply = function(value, context) {29if($.isFunction(value)) {30return value.apply(context, $.makeArray(arguments).slice(2));31}32return value;33};34
35var normalizePromise = function(promise) {36var d = $.Deferred();37
38if(promise && promise.then) {39promise.then(function() {40d.resolve.apply(d, arguments);41}, function() {42d.reject.apply(d, arguments);43});44} else {45d.resolve(promise);46}47
48return d.promise();49};50
51var defaultController = {52loadData: $.noop,53insertItem: $.noop,54updateItem: $.noop,55deleteItem: $.noop56};57
58
59function Grid(element, config) {60var $element = $(element);61
62$element.data(JSGRID_DATA_KEY, this);63
64this._container = $element;65
66this.data = [];67this.fields = [];68
69this._editingRow = null;70this._sortField = null;71this._sortOrder = SORT_ORDER_ASC;72this._firstDisplayingPage = 1;73
74this._init(config);75this.render();76}77
78Grid.prototype = {79width: "auto",80height: "auto",81updateOnResize: true,82
83rowClass: $.noop,84rowRenderer: null,85
86rowClick: function(args) {87if(this.editing) {88this.editItem($(args.event.target).closest("tr"));89}90},91rowDoubleClick: $.noop,92
93noDataContent: "Not found",94noDataRowClass: "jsgrid-nodata-row",95
96heading: true,97headerRowRenderer: null,98headerRowClass: "jsgrid-header-row",99headerCellClass: "jsgrid-header-cell",100
101filtering: false,102filterRowRenderer: null,103filterRowClass: "jsgrid-filter-row",104
105inserting: false,106insertRowRenderer: null,107insertRowClass: "jsgrid-insert-row",108
109editing: false,110editRowRenderer: null,111editRowClass: "jsgrid-edit-row",112
113confirmDeleting: true,114deleteConfirm: "Are you sure?",115
116selecting: true,117selectedRowClass: "jsgrid-selected-row",118oddRowClass: "jsgrid-row",119evenRowClass: "jsgrid-alt-row",120cellClass: "jsgrid-cell",121
122sorting: false,123sortableClass: "jsgrid-header-sortable",124sortAscClass: "jsgrid-header-sort jsgrid-header-sort-asc",125sortDescClass: "jsgrid-header-sort jsgrid-header-sort-desc",126
127paging: false,128pagerContainer: null,129pageIndex: 1,130pageSize: 20,131pageButtonCount: 15,132pagerFormat: "Pages: {first} {prev} {pages} {next} {last} {pageIndex} of {pageCount}",133pagePrevText: "Prev",134pageNextText: "Next",135pageFirstText: "First",136pageLastText: "Last",137pageNavigatorNextText: "...",138pageNavigatorPrevText: "...",139pagerContainerClass: "jsgrid-pager-container",140pagerClass: "jsgrid-pager",141pagerNavButtonClass: "jsgrid-pager-nav-button",142pagerNavButtonInactiveClass: "jsgrid-pager-nav-inactive-button",143pageClass: "jsgrid-pager-page",144currentPageClass: "jsgrid-pager-current-page",145
146customLoading: false,147pageLoading: false,148
149autoload: false,150controller: defaultController,151
152loadIndication: true,153loadIndicationDelay: 500,154loadMessage: "Please, wait...",155loadShading: true,156
157invalidMessage: "Invalid data entered!",158
159invalidNotify: function(args) {160var messages = $.map(args.errors, function(error) {161return error.message || null;162});163
164window.alert([this.invalidMessage].concat(messages).join("\n"));165},166
167onInit: $.noop,168onRefreshing: $.noop,169onRefreshed: $.noop,170onPageChanged: $.noop,171onItemDeleting: $.noop,172onItemDeleted: $.noop,173onItemInserting: $.noop,174onItemInserted: $.noop,175onItemEditing: $.noop,176onItemUpdating: $.noop,177onItemUpdated: $.noop,178onItemInvalid: $.noop,179onDataLoading: $.noop,180onDataLoaded: $.noop,181onOptionChanging: $.noop,182onOptionChanged: $.noop,183onError: $.noop,184
185invalidClass: "jsgrid-invalid",186
187containerClass: "jsgrid",188tableClass: "jsgrid-table",189gridHeaderClass: "jsgrid-grid-header",190gridBodyClass: "jsgrid-grid-body",191
192_init: function(config) {193$.extend(this, config);194this._initLoadStrategy();195this._initController();196this._initFields();197this._attachWindowLoadResize();198this._attachWindowResizeCallback();199this._callEventHandler(this.onInit)200},201
202loadStrategy: function() {203return this.pageLoading204? new jsGrid.loadStrategies.PageLoadingStrategy(this)205: new jsGrid.loadStrategies.DirectLoadingStrategy(this);206},207
208_initLoadStrategy: function() {209this._loadStrategy = getOrApply(this.loadStrategy, this);210},211
212_initController: function() {213this._controller = $.extend({}, defaultController, getOrApply(this.controller, this));214},215
216renderTemplate: function(source, context, config) {217args = [];218for(var key in config) {219args.push(config[key]);220}221
222args.unshift(source, context);223
224source = getOrApply.apply(null, args);225return (source === undefined || source === null) ? "" : source;226},227
228loadIndicator: function(config) {229return new jsGrid.LoadIndicator(config);230},231
232validation: function(config) {233return jsGrid.Validation && new jsGrid.Validation(config);234},235
236_initFields: function() {237var self = this;238self.fields = $.map(self.fields, function(field) {239if($.isPlainObject(field)) {240var fieldConstructor = (field.type && jsGrid.fields[field.type]) || jsGrid.Field;241field = new fieldConstructor(field);242}243field._grid = self;244return field;245});246},247
248_attachWindowLoadResize: function() {249$(window).on("load", $.proxy(this._refreshSize, this));250},251
252_attachWindowResizeCallback: function() {253if(this.updateOnResize) {254$(window).on("resize", $.proxy(this._refreshSize, this));255}256},257
258_detachWindowResizeCallback: function() {259$(window).off("resize", this._refreshSize);260},261
262option: function(key, value) {263var optionChangingEventArgs,264optionChangedEventArgs;265
266if(arguments.length === 1)267return this[key];268
269optionChangingEventArgs = {270option: key,271oldValue: this[key],272newValue: value273};274this._callEventHandler(this.onOptionChanging, optionChangingEventArgs);275
276this._handleOptionChange(optionChangingEventArgs.option, optionChangingEventArgs.newValue);277
278optionChangedEventArgs = {279option: optionChangingEventArgs.option,280value: optionChangingEventArgs.newValue281};282this._callEventHandler(this.onOptionChanged, optionChangedEventArgs);283},284
285fieldOption: function(field, key, value) {286field = this._normalizeField(field);287
288if(arguments.length === 2)289return field[key];290
291field[key] = value;292this._renderGrid();293},294
295_handleOptionChange: function(name, value) {296this[name] = value;297
298switch(name) {299case "width":300case "height":301this._refreshSize();302break;303case "rowClass":304case "rowRenderer":305case "rowClick":306case "rowDoubleClick":307case "noDataRowClass":308case "noDataContent":309case "selecting":310case "selectedRowClass":311case "oddRowClass":312case "evenRowClass":313this._refreshContent();314break;315case "pageButtonCount":316case "pagerFormat":317case "pagePrevText":318case "pageNextText":319case "pageFirstText":320case "pageLastText":321case "pageNavigatorNextText":322case "pageNavigatorPrevText":323case "pagerClass":324case "pagerNavButtonClass":325case "pageClass":326case "currentPageClass":327case "pagerRenderer":328this._refreshPager();329break;330case "fields":331this._initFields();332this.render();333break;334case "data":335case "editing":336case "heading":337case "filtering":338case "inserting":339case "paging":340this.refresh();341break;342case "loadStrategy":343case "pageLoading":344this._initLoadStrategy();345this.search();346break;347case "pageIndex":348this.openPage(value);349break;350case "pageSize":351this.refresh();352this.search();353break;354case "editRowRenderer":355case "editRowClass":356this.cancelEdit();357break;358case "updateOnResize":359this._detachWindowResizeCallback();360this._attachWindowResizeCallback();361break;362case "invalidNotify":363case "invalidMessage":364break;365default:366this.render();367break;368}369},370
371destroy: function() {372this._detachWindowResizeCallback();373this._clear();374this._container.removeData(JSGRID_DATA_KEY);375},376
377render: function() {378this._renderGrid();379return this.autoload ? this.loadData() : $.Deferred().resolve().promise();380},381
382_renderGrid: function() {383this._clear();384
385this._container.addClass(this.containerClass)386.css("position", "relative")387.append(this._createHeader())388.append(this._createBody());389
390this._pagerContainer = this._createPagerContainer();391this._loadIndicator = this._createLoadIndicator();392this._validation = this._createValidation();393
394this.refresh();395},396
397_createLoadIndicator: function() {398return getOrApply(this.loadIndicator, this, {399message: this.loadMessage,400shading: this.loadShading,401container: this._container402});403},404
405_createValidation: function() {406return getOrApply(this.validation, this);407},408
409_clear: function() {410this.cancelEdit();411
412clearTimeout(this._loadingTimer);413
414this._pagerContainer && this._pagerContainer.empty();415
416this._container.empty()417.css({ position: "", width: "", height: "" });418},419
420_createHeader: function() {421var $headerRow = this._headerRow = this._createHeaderRow(),422$filterRow = this._filterRow = this._createFilterRow(),423$insertRow = this._insertRow = this._createInsertRow();424
425var $headerGrid = this._headerGrid = $("<table>").addClass(this.tableClass)426.append($headerRow)427.append($filterRow)428.append($insertRow);429
430var $header = this._header = $("<div>").addClass(this.gridHeaderClass)431.addClass(this._scrollBarWidth() ? "jsgrid-header-scrollbar" : "")432.append($headerGrid);433
434return $header;435},436
437_createBody: function() {438var $content = this._content = $("<tbody>");439
440var $bodyGrid = this._bodyGrid = $("<table>").addClass(this.tableClass)441.append($content);442
443var $body = this._body = $("<div>").addClass(this.gridBodyClass)444.append($bodyGrid)445.on("scroll", $.proxy(function(e) {446this._header.scrollLeft(e.target.scrollLeft);447}, this));448
449return $body;450},451
452_createPagerContainer: function() {453var pagerContainer = this.pagerContainer || $("<div>").appendTo(this._container);454return $(pagerContainer).addClass(this.pagerContainerClass);455},456
457_eachField: function(callBack) {458var self = this;459$.each(this.fields, function(index, field) {460if(field.visible) {461callBack.call(self, field, index);462}463});464},465
466_createHeaderRow: function() {467if($.isFunction(this.headerRowRenderer))468return $(this.renderTemplate(this.headerRowRenderer, this));469
470var $result = $("<tr>").addClass(this.headerRowClass);471
472this._eachField(function(field, index) {473var $th = this._prepareCell("<th>", field, "headercss", this.headerCellClass)474.append(this.renderTemplate(field.headerTemplate, field))475.appendTo($result);476
477if(this.sorting && field.sorting) {478$th.addClass(this.sortableClass)479.on("click", $.proxy(function() {480this.sort(index);481}, this));482}483});484
485return $result;486},487
488_prepareCell: function(cell, field, cssprop, cellClass) {489return $(cell).css("width", field.width)490.addClass(cellClass || this.cellClass)491.addClass((cssprop && field[cssprop]) || field.css)492.addClass(field.align ? ("jsgrid-align-" + field.align) : "");493},494
495_createFilterRow: function() {496if($.isFunction(this.filterRowRenderer))497return $(this.renderTemplate(this.filterRowRenderer, this));498
499var $result = $("<tr>").addClass(this.filterRowClass);500
501this._eachField(function(field) {502this._prepareCell("<td>", field, "filtercss")503.append(this.renderTemplate(field.filterTemplate, field))504.appendTo($result);505});506
507return $result;508},509
510_createInsertRow: function() {511if($.isFunction(this.insertRowRenderer))512return $(this.renderTemplate(this.insertRowRenderer, this));513
514var $result = $("<tr>").addClass(this.insertRowClass);515
516this._eachField(function(field) {517this._prepareCell("<td>", field, "insertcss")518.append(this.renderTemplate(field.insertTemplate, field))519.appendTo($result);520});521
522return $result;523},524
525_callEventHandler: function(handler, eventParams) {526handler.call(this, $.extend(eventParams, {527grid: this528}));529
530return eventParams;531},532
533reset: function() {534this._resetSorting();535this._resetPager();536return this._loadStrategy.reset();537},538
539_resetPager: function() {540this._firstDisplayingPage = 1;541this._setPage(1);542},543
544_resetSorting: function() {545this._sortField = null;546this._sortOrder = SORT_ORDER_ASC;547this._clearSortingCss();548},549
550refresh: function() {551this._callEventHandler(this.onRefreshing);552
553this.cancelEdit();554
555this._refreshHeading();556this._refreshFiltering();557this._refreshInserting();558this._refreshContent();559this._refreshPager();560this._refreshSize();561
562this._callEventHandler(this.onRefreshed);563},564
565_refreshHeading: function() {566this._headerRow.toggle(this.heading);567},568
569_refreshFiltering: function() {570this._filterRow.toggle(this.filtering);571},572
573_refreshInserting: function() {574this._insertRow.toggle(this.inserting);575},576
577_refreshContent: function() {578var $content = this._content;579$content.empty();580
581if(!this.data.length) {582$content.append(this._createNoDataRow());583return this;584}585
586var indexFrom = this._loadStrategy.firstDisplayIndex();587var indexTo = this._loadStrategy.lastDisplayIndex();588
589for(var itemIndex = indexFrom; itemIndex < indexTo; itemIndex++) {590var item = this.data[itemIndex];591$content.append(this._createRow(item, itemIndex));592}593},594
595_createNoDataRow: function() {596var amountOfFields = 0;597this._eachField(function() {598amountOfFields++;599});600
601return $("<tr>").addClass(this.noDataRowClass)602.append($("<td>").addClass(this.cellClass).attr("colspan", amountOfFields)603.append(this.renderTemplate(this.noDataContent, this)));604},605
606_createRow: function(item, itemIndex) {607var $result;608
609if($.isFunction(this.rowRenderer)) {610$result = this.renderTemplate(this.rowRenderer, this, { item: item, itemIndex: itemIndex });611} else {612$result = $("<tr>");613this._renderCells($result, item);614}615
616$result.addClass(this._getRowClasses(item, itemIndex))617.data(JSGRID_ROW_DATA_KEY, item)618.on("click", $.proxy(function(e) {619this.rowClick({620item: item,621itemIndex: itemIndex,622event: e623});624}, this))625.on("dblclick", $.proxy(function(e) {626this.rowDoubleClick({627item: item,628itemIndex: itemIndex,629event: e630});631}, this));632
633if(this.selecting) {634this._attachRowHover($result);635}636
637return $result;638},639
640_getRowClasses: function(item, itemIndex) {641var classes = [];642classes.push(((itemIndex + 1) % 2) ? this.oddRowClass : this.evenRowClass);643classes.push(getOrApply(this.rowClass, this, item, itemIndex));644return classes.join(" ");645},646
647_attachRowHover: function($row) {648var selectedRowClass = this.selectedRowClass;649$row.hover(function() {650$(this).addClass(selectedRowClass);651},652function() {653$(this).removeClass(selectedRowClass);654}655);656},657
658_renderCells: function($row, item) {659this._eachField(function(field) {660$row.append(this._createCell(item, field));661});662return this;663},664
665_createCell: function(item, field) {666var $result;667var fieldValue = this._getItemFieldValue(item, field);668
669var args = { value: fieldValue, item : item };670if($.isFunction(field.cellRenderer)) {671$result = this.renderTemplate(field.cellRenderer, field, args);672} else {673$result = $("<td>").append(this.renderTemplate(field.itemTemplate || fieldValue, field, args));674}675
676return this._prepareCell($result, field);677},678
679_getItemFieldValue: function(item, field) {680var props = field.name.split('.');681var result = item[props.shift()];682
683while(result && props.length) {684result = result[props.shift()];685}686
687return result;688},689
690_setItemFieldValue: function(item, field, value) {691var props = field.name.split('.');692var current = item;693var prop = props[0];694
695while(current && props.length) {696item = current;697prop = props.shift();698current = item[prop];699}700
701if(!current) {702while(props.length) {703item = item[prop] = {};704prop = props.shift();705}706}707
708item[prop] = value;709},710
711sort: function(field, order) {712if($.isPlainObject(field)) {713order = field.order;714field = field.field;715}716
717this._clearSortingCss();718this._setSortingParams(field, order);719this._setSortingCss();720return this._loadStrategy.sort();721},722
723_clearSortingCss: function() {724this._headerRow.find("th")725.removeClass(this.sortAscClass)726.removeClass(this.sortDescClass);727},728
729_setSortingParams: function(field, order) {730field = this._normalizeField(field);731order = order || ((this._sortField === field) ? this._reversedSortOrder(this._sortOrder) : SORT_ORDER_ASC);732
733this._sortField = field;734this._sortOrder = order;735},736
737_normalizeField: function(field) {738if($.isNumeric(field)) {739return this.fields[field];740}741
742if(typeof field === "string") {743return $.grep(this.fields, function(f) {744return f.name === field;745})[0];746}747
748return field;749},750
751_reversedSortOrder: function(order) {752return (order === SORT_ORDER_ASC ? SORT_ORDER_DESC : SORT_ORDER_ASC);753},754
755_setSortingCss: function() {756var fieldIndex = this._visibleFieldIndex(this._sortField);757
758this._headerRow.find("th").eq(fieldIndex)759.addClass(this._sortOrder === SORT_ORDER_ASC ? this.sortAscClass : this.sortDescClass);760},761
762_visibleFieldIndex: function(field) {763return $.inArray(field, $.grep(this.fields, function(f) { return f.visible; }));764},765
766_sortData: function() {767var sortFactor = this._sortFactor(),768sortField = this._sortField;769
770if(sortField) {771this.data.sort(function(item1, item2) {772return sortFactor * sortField.sortingFunc(item1[sortField.name], item2[sortField.name]);773});774}775},776
777_sortFactor: function() {778return this._sortOrder === SORT_ORDER_ASC ? 1 : -1;779},780
781_itemsCount: function() {782return this._loadStrategy.itemsCount();783},784
785_pagesCount: function() {786var itemsCount = this._itemsCount(),787pageSize = this.pageSize;788return Math.floor(itemsCount / pageSize) + (itemsCount % pageSize ? 1 : 0);789},790
791_refreshPager: function() {792var $pagerContainer = this._pagerContainer;793$pagerContainer.empty();794
795if(this.paging) {796$pagerContainer.append(this._createPager());797}798
799var showPager = this.paging && this._pagesCount() > 1;800$pagerContainer.toggle(showPager);801},802
803_createPager: function() {804var $result;805
806if($.isFunction(this.pagerRenderer)) {807$result = $(this.pagerRenderer({808pageIndex: this.pageIndex,809pageCount: this._pagesCount()810}));811} else {812$result = $("<div>").append(this._createPagerByFormat());813}814
815$result.addClass(this.pagerClass);816
817return $result;818},819
820_createPagerByFormat: function() {821var pageIndex = this.pageIndex,822pageCount = this._pagesCount(),823itemCount = this._itemsCount(),824pagerParts = this.pagerFormat.split(" ");825
826return $.map(pagerParts, $.proxy(function(pagerPart) {827var result = pagerPart;828
829if(pagerPart === PAGES_PLACEHOLDER) {830result = this._createPages();831} else if(pagerPart === FIRST_PAGE_PLACEHOLDER) {832result = this._createPagerNavButton(this.pageFirstText, 1, pageIndex > 1);833} else if(pagerPart === PREV_PAGE_PLACEHOLDER) {834result = this._createPagerNavButton(this.pagePrevText, pageIndex - 1, pageIndex > 1);835} else if(pagerPart === NEXT_PAGE_PLACEHOLDER) {836result = this._createPagerNavButton(this.pageNextText, pageIndex + 1, pageIndex < pageCount);837} else if(pagerPart === LAST_PAGE_PLACEHOLDER) {838result = this._createPagerNavButton(this.pageLastText, pageCount, pageIndex < pageCount);839} else if(pagerPart === PAGE_INDEX_PLACEHOLDER) {840result = pageIndex;841} else if(pagerPart === PAGE_COUNT_PLACEHOLDER) {842result = pageCount;843} else if(pagerPart === ITEM_COUNT_PLACEHOLDER) {844result = itemCount;845}846
847return $.isArray(result) ? result.concat([" "]) : [result, " "];848}, this));849},850
851_createPages: function() {852var pageCount = this._pagesCount(),853pageButtonCount = this.pageButtonCount,854firstDisplayingPage = this._firstDisplayingPage,855pages = [];856
857if(firstDisplayingPage > 1) {858pages.push(this._createPagerPageNavButton(this.pageNavigatorPrevText, this.showPrevPages));859}860
861for(var i = 0, pageNumber = firstDisplayingPage; i < pageButtonCount && pageNumber <= pageCount; i++, pageNumber++) {862pages.push(pageNumber === this.pageIndex863? this._createPagerCurrentPage()864: this._createPagerPage(pageNumber));865}866
867if((firstDisplayingPage + pageButtonCount - 1) < pageCount) {868pages.push(this._createPagerPageNavButton(this.pageNavigatorNextText, this.showNextPages));869}870
871return pages;872},873
874_createPagerNavButton: function(text, pageIndex, isActive) {875return this._createPagerButton(text, this.pagerNavButtonClass + (isActive ? "" : " " + this.pagerNavButtonInactiveClass),876isActive ? function() { this.openPage(pageIndex); } : $.noop);877},878
879_createPagerPageNavButton: function(text, handler) {880return this._createPagerButton(text, this.pagerNavButtonClass, handler);881},882
883_createPagerPage: function(pageIndex) {884return this._createPagerButton(pageIndex, this.pageClass, function() {885this.openPage(pageIndex);886});887},888
889_createPagerButton: function(text, css, handler) {890var $link = $("<a>").attr("href", EMPTY_HREF)891.html(text)892.on("click", $.proxy(handler, this));893
894return $("<span>").addClass(css).append($link);895},896
897_createPagerCurrentPage: function() {898return $("<span>")899.addClass(this.pageClass)900.addClass(this.currentPageClass)901.text(this.pageIndex);902},903
904_refreshSize: function() {905this._refreshHeight();906this._refreshWidth();907},908
909_refreshWidth: function() {910var width = (this.width === "auto") ? this._getAutoWidth() : this.width;911
912this._container.width(width);913},914
915_getAutoWidth: function() {916var $headerGrid = this._headerGrid,917$header = this._header;918
919$headerGrid.width("auto");920
921var contentWidth = $headerGrid.outerWidth();922var borderWidth = $header.outerWidth() - $header.innerWidth();923
924$headerGrid.width("");925
926return contentWidth + borderWidth;927},928
929_scrollBarWidth: (function() {930var result;931
932return function() {933if(result === undefined) {934var $ghostContainer = $("<div style='width:50px;height:50px;overflow:hidden;position:absolute;top:-10000px;left:-10000px;'></div>");935var $ghostContent = $("<div style='height:100px;'></div>");936$ghostContainer.append($ghostContent).appendTo("body");937var width = $ghostContent.innerWidth();938$ghostContainer.css("overflow-y", "auto");939var widthExcludingScrollBar = $ghostContent.innerWidth();940$ghostContainer.remove();941result = width - widthExcludingScrollBar;942}943return result;944};945})(),946
947_refreshHeight: function() {948var container = this._container,949pagerContainer = this._pagerContainer,950height = this.height,951nonBodyHeight;952
953container.height(height);954
955if(height !== "auto") {956height = container.height();957
958nonBodyHeight = this._header.outerHeight(true);959if(pagerContainer.parents(container).length) {960nonBodyHeight += pagerContainer.outerHeight(true);961}962
963this._body.outerHeight(height - nonBodyHeight);964}965},966
967showPrevPages: function() {968var firstDisplayingPage = this._firstDisplayingPage,969pageButtonCount = this.pageButtonCount;970
971this._firstDisplayingPage = (firstDisplayingPage > pageButtonCount) ? firstDisplayingPage - pageButtonCount : 1;972
973this._refreshPager();974},975
976showNextPages: function() {977var firstDisplayingPage = this._firstDisplayingPage,978pageButtonCount = this.pageButtonCount,979pageCount = this._pagesCount();980
981this._firstDisplayingPage = (firstDisplayingPage + 2 * pageButtonCount > pageCount)982? pageCount - pageButtonCount + 1983: firstDisplayingPage + pageButtonCount;984
985this._refreshPager();986},987
988openPage: function(pageIndex) {989if(pageIndex < 1 || pageIndex > this._pagesCount())990return;991
992this._setPage(pageIndex);993this._loadStrategy.openPage(pageIndex);994},995
996_setPage: function(pageIndex) {997var firstDisplayingPage = this._firstDisplayingPage,998pageButtonCount = this.pageButtonCount;999
1000this.pageIndex = pageIndex;1001
1002if(pageIndex < firstDisplayingPage) {1003this._firstDisplayingPage = pageIndex;1004}1005
1006if(pageIndex > firstDisplayingPage + pageButtonCount - 1) {1007this._firstDisplayingPage = pageIndex - pageButtonCount + 1;1008}1009
1010this._callEventHandler(this.onPageChanged, {1011pageIndex: pageIndex1012});1013},1014
1015_controllerCall: function(method, param, isCanceled, doneCallback) {1016if(isCanceled)1017return $.Deferred().reject().promise();1018
1019this._showLoading();1020
1021var controller = this._controller;1022if(!controller || !controller[method]) {1023throw Error("controller has no method '" + method + "'");1024}1025
1026return normalizePromise(controller[method](param))1027.done($.proxy(doneCallback, this))1028.fail($.proxy(this._errorHandler, this))1029.always($.proxy(this._hideLoading, this));1030},1031
1032_errorHandler: function() {1033this._callEventHandler(this.onError, {1034args: $.makeArray(arguments)1035});1036},1037
1038_showLoading: function() {1039if(!this.loadIndication)1040return;1041
1042clearTimeout(this._loadingTimer);1043
1044this._loadingTimer = setTimeout($.proxy(function() {1045this._loadIndicator.show();1046}, this), this.loadIndicationDelay);1047},1048
1049_hideLoading: function() {1050if(!this.loadIndication)1051return;1052
1053clearTimeout(this._loadingTimer);1054this._loadIndicator.hide();1055},1056
1057search: function(filter) {1058this._resetSorting();1059this._resetPager();1060return this.loadData(filter);1061},1062
1063loadData: function(filter) {1064filter = filter || (this.filtering ? this.getFilter() : {});1065
1066$.extend(filter, this._loadStrategy.loadParams(), this._sortingParams());1067
1068var args = this._callEventHandler(this.onDataLoading, {1069filter: filter1070});1071
1072return this._controllerCall("loadData", filter, args.cancel, function(loadedData) {1073if(!loadedData)1074return;1075
1076this._loadStrategy.finishLoad(loadedData);1077
1078this._callEventHandler(this.onDataLoaded, {1079data: loadedData1080});1081});1082},1083
1084getFilter: function() {1085var result = {};1086this._eachField(function(field) {1087if(field.filtering) {1088this._setItemFieldValue(result, field, field.filterValue());1089}1090});1091return result;1092},1093
1094_sortingParams: function() {1095if(this.sorting && this._sortField) {1096return {1097sortField: this._sortField.name,1098sortOrder: this._sortOrder1099};1100}1101return {};1102},1103
1104getSorting: function() {1105var sortingParams = this._sortingParams();1106return {1107field: sortingParams.sortField,1108order: sortingParams.sortOrder1109};1110},1111
1112clearFilter: function() {1113var $filterRow = this._createFilterRow();1114this._filterRow.replaceWith($filterRow);1115this._filterRow = $filterRow;1116return this.search();1117},1118
1119insertItem: function(item) {1120var insertingItem = item || this._getValidatedInsertItem();1121
1122if(!insertingItem)1123return $.Deferred().reject().promise();1124
1125var args = this._callEventHandler(this.onItemInserting, {1126item: insertingItem1127});1128
1129return this._controllerCall("insertItem", insertingItem, args.cancel, function(insertedItem) {1130insertedItem = insertedItem || insertingItem;1131this._loadStrategy.finishInsert(insertedItem);1132
1133this._callEventHandler(this.onItemInserted, {1134item: insertedItem1135});1136});1137},1138
1139_getValidatedInsertItem: function() {1140var item = this._getInsertItem();1141return this._validateItem(item, this._insertRow) ? item : null;1142},1143
1144_getInsertItem: function() {1145var result = {};1146this._eachField(function(field) {1147if(field.inserting) {1148this._setItemFieldValue(result, field, field.insertValue());1149}1150});1151return result;1152},1153
1154_validateItem: function(item, $row) {1155var validationErrors = [];1156
1157var args = {1158item: item,1159itemIndex: this._rowIndex($row),1160row: $row1161};1162
1163this._eachField(function(field) {1164if(!field.validate ||1165($row === this._insertRow && !field.inserting) ||1166($row === this._getEditRow() && !field.editing))1167return;1168
1169var fieldValue = this._getItemFieldValue(item, field);1170
1171var errors = this._validation.validate($.extend({1172value: fieldValue,1173rules: field.validate1174}, args));1175
1176this._setCellValidity($row.children().eq(this._visibleFieldIndex(field)), errors);1177
1178if(!errors.length)1179return;1180
1181validationErrors.push.apply(validationErrors,1182$.map(errors, function(message) {1183return { field: field, message: message };1184}));1185});1186
1187if(!validationErrors.length)1188return true;1189
1190var invalidArgs = $.extend({1191errors: validationErrors1192}, args);1193this._callEventHandler(this.onItemInvalid, invalidArgs);1194this.invalidNotify(invalidArgs);1195
1196return false;1197},1198
1199_setCellValidity: function($cell, errors) {1200$cell
1201.toggleClass(this.invalidClass, !!errors.length)1202.attr("title", errors.join("\n"));1203},1204
1205clearInsert: function() {1206var insertRow = this._createInsertRow();1207this._insertRow.replaceWith(insertRow);1208this._insertRow = insertRow;1209this.refresh();1210},1211
1212editItem: function(item) {1213var $row = this.rowByItem(item);1214if($row.length) {1215this._editRow($row);1216}1217},1218
1219rowByItem: function(item) {1220if(item.jquery || item.nodeType)1221return $(item);1222
1223return this._content.find("tr").filter(function() {1224return $.data(this, JSGRID_ROW_DATA_KEY) === item;1225});1226},1227
1228_editRow: function($row) {1229if(!this.editing)1230return;1231
1232var item = $row.data(JSGRID_ROW_DATA_KEY);1233
1234var args = this._callEventHandler(this.onItemEditing, {1235row: $row,1236item: item,1237itemIndex: this._itemIndex(item)1238});1239
1240if(args.cancel)1241return;1242
1243if(this._editingRow) {1244this.cancelEdit();1245}1246
1247var $editRow = this._createEditRow(item);1248
1249this._editingRow = $row;1250$row.hide();1251$editRow.insertBefore($row);1252$row.data(JSGRID_EDIT_ROW_DATA_KEY, $editRow);1253},1254
1255_createEditRow: function(item) {1256if($.isFunction(this.editRowRenderer)) {1257return $(this.renderTemplate(this.editRowRenderer, this, { item: item, itemIndex: this._itemIndex(item) }));1258}1259
1260var $result = $("<tr>").addClass(this.editRowClass);1261
1262this._eachField(function(field) {1263var fieldValue = this._getItemFieldValue(item, field);1264
1265this._prepareCell("<td>", field, "editcss")1266.append(this.renderTemplate(field.editTemplate || "", field, { value: fieldValue, item: item }))1267.appendTo($result);1268});1269
1270return $result;1271},1272
1273updateItem: function(item, editedItem) {1274if(arguments.length === 1) {1275editedItem = item;1276}1277
1278var $row = item ? this.rowByItem(item) : this._editingRow;1279editedItem = editedItem || this._getValidatedEditedItem();1280
1281if(!editedItem)1282return;1283
1284return this._updateRow($row, editedItem);1285},1286
1287_getValidatedEditedItem: function() {1288var item = this._getEditedItem();1289return this._validateItem(item, this._getEditRow()) ? item : null;1290},1291
1292_updateRow: function($updatingRow, editedItem) {1293var updatingItem = $updatingRow.data(JSGRID_ROW_DATA_KEY),1294updatingItemIndex = this._itemIndex(updatingItem),1295updatedItem = $.extend(true, {}, updatingItem, editedItem);1296
1297var args = this._callEventHandler(this.onItemUpdating, {1298row: $updatingRow,1299item: updatedItem,1300itemIndex: updatingItemIndex,1301previousItem: updatingItem1302});1303
1304return this._controllerCall("updateItem", updatedItem, args.cancel, function(loadedUpdatedItem) {1305var previousItem = $.extend(true, {}, updatingItem);1306updatedItem = loadedUpdatedItem || $.extend(true, updatingItem, editedItem);1307
1308var $updatedRow = this._finishUpdate($updatingRow, updatedItem, updatingItemIndex);1309
1310this._callEventHandler(this.onItemUpdated, {1311row: $updatedRow,1312item: updatedItem,1313itemIndex: updatingItemIndex,1314previousItem: previousItem1315});1316});1317},1318
1319_rowIndex: function(row) {1320return this._content.children().index($(row));1321},1322
1323_itemIndex: function(item) {1324return $.inArray(item, this.data);1325},1326
1327_finishUpdate: function($updatingRow, updatedItem, updatedItemIndex) {1328this.cancelEdit();1329this.data[updatedItemIndex] = updatedItem;1330
1331var $updatedRow = this._createRow(updatedItem, updatedItemIndex);1332$updatingRow.replaceWith($updatedRow);1333return $updatedRow;1334},1335
1336_getEditedItem: function() {1337var result = {};1338this._eachField(function(field) {1339if(field.editing) {1340this._setItemFieldValue(result, field, field.editValue());1341}1342});1343return result;1344},1345
1346cancelEdit: function() {1347if(!this._editingRow)1348return;1349
1350this._getEditRow().remove();1351this._editingRow.show();1352this._editingRow = null;1353},1354
1355_getEditRow: function() {1356return this._editingRow && this._editingRow.data(JSGRID_EDIT_ROW_DATA_KEY);1357},1358
1359deleteItem: function(item) {1360var $row = this.rowByItem(item);1361
1362if(!$row.length)1363return;1364
1365if(this.confirmDeleting && !window.confirm(getOrApply(this.deleteConfirm, this, $row.data(JSGRID_ROW_DATA_KEY))))1366return;1367
1368return this._deleteRow($row);1369},1370
1371_deleteRow: function($row) {1372var deletingItem = $row.data(JSGRID_ROW_DATA_KEY),1373deletingItemIndex = this._itemIndex(deletingItem);1374
1375var args = this._callEventHandler(this.onItemDeleting, {1376row: $row,1377item: deletingItem,1378itemIndex: deletingItemIndex1379});1380
1381return this._controllerCall("deleteItem", deletingItem, args.cancel, function() {1382this._loadStrategy.finishDelete(deletingItem, deletingItemIndex);1383
1384this._callEventHandler(this.onItemDeleted, {1385row: $row,1386item: deletingItem,1387itemIndex: deletingItemIndex1388});1389});1390}1391};1392
1393$.fn.jsGrid = function(config) {1394var args = $.makeArray(arguments),1395methodArgs = args.slice(1),1396result = this;1397
1398this.each(function() {1399var $element = $(this),1400instance = $element.data(JSGRID_DATA_KEY),1401methodResult;1402
1403if(instance) {1404if(typeof config === "string") {1405methodResult = instance[config].apply(instance, methodArgs);1406if(methodResult !== undefined && methodResult !== instance) {1407result = methodResult;1408return false;1409}1410} else {1411instance._detachWindowResizeCallback();1412instance._init(config);1413instance.render();1414}1415} else {1416new Grid($element, config);1417}1418});1419
1420return result;1421};1422
1423var fields = {};1424
1425var setDefaults = function(config) {1426var componentPrototype;1427
1428if($.isPlainObject(config)) {1429componentPrototype = Grid.prototype;1430} else {1431componentPrototype = fields[config].prototype;1432config = arguments[1] || {};1433}1434
1435$.extend(componentPrototype, config);1436};1437
1438var locales = {};1439
1440var locale = function(lang) {1441var localeConfig = $.isPlainObject(lang) ? lang : locales[lang];1442
1443if(!localeConfig)1444throw Error("unknown locale " + lang);1445
1446setLocale(jsGrid, localeConfig);1447};1448
1449var setLocale = function(obj, localeConfig) {1450$.each(localeConfig, function(field, value) {1451if($.isPlainObject(value)) {1452setLocale(obj[field] || obj[field[0].toUpperCase() + field.slice(1)], value);1453return;1454}1455
1456if(obj.hasOwnProperty(field)) {1457obj[field] = value;1458} else {1459obj.prototype[field] = value;1460}1461});1462};1463
1464window.jsGrid = {1465Grid: Grid,1466fields: fields,1467setDefaults: setDefaults,1468locales: locales,1469locale: locale,1470version: '1.5.3'1471};1472
1473}(window, jQuery));1474
1475(function(jsGrid, $, undefined) {1476
1477function LoadIndicator(config) {1478this._init(config);1479}1480
1481LoadIndicator.prototype = {1482
1483container: "body",1484message: "Loading...",1485shading: true,1486
1487zIndex: 1000,1488shaderClass: "jsgrid-load-shader",1489loadPanelClass: "jsgrid-load-panel",1490
1491_init: function(config) {1492$.extend(true, this, config);1493
1494this._initContainer();1495this._initShader();1496this._initLoadPanel();1497},1498
1499_initContainer: function() {1500this._container = $(this.container);1501},1502
1503_initShader: function() {1504if(!this.shading)1505return;1506
1507this._shader = $("<div>").addClass(this.shaderClass)1508.hide()1509.css({1510position: "absolute",1511top: 0,1512right: 0,1513bottom: 0,1514left: 0,1515zIndex: this.zIndex1516})1517.appendTo(this._container);1518},1519
1520_initLoadPanel: function() {1521this._loadPanel = $("<div>").addClass(this.loadPanelClass)1522.text(this.message)1523.hide()1524.css({1525position: "absolute",1526top: "50%",1527left: "50%",1528zIndex: this.zIndex1529})1530.appendTo(this._container);1531},1532
1533show: function() {1534var $loadPanel = this._loadPanel.show();1535
1536var actualWidth = $loadPanel.outerWidth();1537var actualHeight = $loadPanel.outerHeight();1538
1539$loadPanel.css({1540marginTop: -actualHeight / 2,1541marginLeft: -actualWidth / 21542});1543
1544this._shader.show();1545},1546
1547hide: function() {1548this._loadPanel.hide();1549this._shader.hide();1550}1551
1552};1553
1554jsGrid.LoadIndicator = LoadIndicator;1555
1556}(jsGrid, jQuery));1557
1558(function(jsGrid, $, undefined) {1559
1560function DirectLoadingStrategy(grid) {1561this._grid = grid;1562}1563
1564DirectLoadingStrategy.prototype = {1565
1566firstDisplayIndex: function() {1567var grid = this._grid;1568return grid.option("paging") ? (grid.option("pageIndex") - 1) * grid.option("pageSize") : 0;1569},1570
1571lastDisplayIndex: function() {1572var grid = this._grid;1573var itemsCount = grid.option("data").length;1574
1575return grid.option("paging")1576? Math.min(grid.option("pageIndex") * grid.option("pageSize"), itemsCount)1577: itemsCount;1578},1579
1580itemsCount: function() {1581return this._grid.option("data").length;1582},1583
1584openPage: function(index) {1585this._grid.refresh();1586},1587
1588loadParams: function() {1589return {};1590},1591
1592sort: function() {1593this._grid._sortData();1594this._grid.refresh();1595return $.Deferred().resolve().promise();1596},1597
1598reset: function() {1599this._grid.refresh();1600return $.Deferred().resolve().promise();1601},1602
1603finishLoad: function(loadedData) {1604this._grid.option("data", loadedData);1605},1606
1607finishInsert: function(insertedItem) {1608var grid = this._grid;1609grid.option("data").push(insertedItem);1610grid.refresh();1611},1612
1613finishDelete: function(deletedItem, deletedItemIndex) {1614var grid = this._grid;1615grid.option("data").splice(deletedItemIndex, 1);1616grid.reset();1617}1618};1619
1620
1621function PageLoadingStrategy(grid) {1622this._grid = grid;1623this._itemsCount = 0;1624}1625
1626PageLoadingStrategy.prototype = {1627
1628firstDisplayIndex: function() {1629return 0;1630},1631
1632lastDisplayIndex: function() {1633return this._grid.option("data").length;1634},1635
1636itemsCount: function() {1637return this._itemsCount;1638},1639
1640openPage: function(index) {1641this._grid.loadData();1642},1643
1644loadParams: function() {1645var grid = this._grid;1646return {1647pageIndex: grid.option("pageIndex"),1648pageSize: grid.option("pageSize")1649};1650},1651
1652reset: function() {1653return this._grid.loadData();1654},1655
1656sort: function() {1657return this._grid.loadData();1658},1659
1660finishLoad: function(loadedData) {1661this._itemsCount = loadedData.itemsCount;1662this._grid.option("data", loadedData.data);1663},1664
1665finishInsert: function(insertedItem) {1666this._grid.search();1667},1668
1669finishDelete: function(deletedItem, deletedItemIndex) {1670this._grid.search();1671}1672};1673
1674jsGrid.loadStrategies = {1675DirectLoadingStrategy: DirectLoadingStrategy,1676PageLoadingStrategy: PageLoadingStrategy1677};1678
1679}(jsGrid, jQuery));1680
1681(function(jsGrid, $, undefined) {1682
1683var isDefined = function(val) {1684return typeof(val) !== "undefined" && val !== null;1685};1686
1687var sortStrategies = {1688string: function(str1, str2) {1689if(!isDefined(str1) && !isDefined(str2))1690return 0;1691
1692if(!isDefined(str1))1693return -1;1694
1695if(!isDefined(str2))1696return 1;1697
1698return ("" + str1).localeCompare("" + str2);1699},1700
1701number: function(n1, n2) {1702return n1 - n2;1703},1704
1705date: function(dt1, dt2) {1706return dt1 - dt2;1707},1708
1709numberAsString: function(n1, n2) {1710return parseFloat(n1) - parseFloat(n2);1711}1712};1713
1714jsGrid.sortStrategies = sortStrategies;1715
1716}(jsGrid, jQuery));1717
1718(function(jsGrid, $, undefined) {1719
1720function Validation(config) {1721this._init(config);1722}1723
1724Validation.prototype = {1725
1726_init: function(config) {1727$.extend(true, this, config);1728},1729
1730validate: function(args) {1731var errors = [];1732
1733$.each(this._normalizeRules(args.rules), function(_, rule) {1734if(rule.validator(args.value, args.item, rule.param))1735return;1736
1737var errorMessage = $.isFunction(rule.message) ? rule.message(args.value, args.item) : rule.message;1738errors.push(errorMessage);1739});1740
1741return errors;1742},1743
1744_normalizeRules: function(rules) {1745if(!$.isArray(rules))1746rules = [rules];1747
1748return $.map(rules, $.proxy(function(rule) {1749return this._normalizeRule(rule);1750}, this));1751},1752
1753_normalizeRule: function(rule) {1754if(typeof rule === "string")1755rule = { validator: rule };1756
1757if($.isFunction(rule))1758rule = { validator: rule };1759
1760if($.isPlainObject(rule))1761rule = $.extend({}, rule);1762else1763throw Error("wrong validation config specified");1764
1765if($.isFunction(rule.validator))1766return rule;1767
1768return this._applyNamedValidator(rule, rule.validator);1769},1770
1771_applyNamedValidator: function(rule, validatorName) {1772delete rule.validator;1773
1774var validator = validators[validatorName];1775if(!validator)1776throw Error("unknown validator \"" + validatorName + "\"");1777
1778if($.isFunction(validator)) {1779validator = { validator: validator };1780}1781
1782return $.extend({}, validator, rule);1783}1784};1785
1786jsGrid.Validation = Validation;1787
1788
1789var validators = {1790required: {1791message: "Field is required",1792validator: function(value) {1793return value !== undefined && value !== null && value !== "";1794}1795},1796
1797rangeLength: {1798message: "Field value length is out of the defined range",1799validator: function(value, _, param) {1800return value.length >= param[0] && value.length <= param[1];1801}1802},1803
1804minLength: {1805message: "Field value is too short",1806validator: function(value, _, param) {1807return value.length >= param;1808}1809},1810
1811maxLength: {1812message: "Field value is too long",1813validator: function(value, _, param) {1814return value.length <= param;1815}1816},1817
1818pattern: {1819message: "Field value is not matching the defined pattern",1820validator: function(value, _, param) {1821if(typeof param === "string") {1822param = new RegExp("^(?:" + param + ")$");1823}1824return param.test(value);1825}1826},1827
1828range: {1829message: "Field value is out of the defined range",1830validator: function(value, _, param) {1831return value >= param[0] && value <= param[1];1832}1833},1834
1835min: {1836message: "Field value is too small",1837validator: function(value, _, param) {1838return value >= param;1839}1840},1841
1842max: {1843message: "Field value is too large",1844validator: function(value, _, param) {1845return value <= param;1846}1847}1848};1849
1850jsGrid.validators = validators;1851
1852}(jsGrid, jQuery));1853
1854(function(jsGrid, $, undefined) {1855
1856function Field(config) {1857$.extend(true, this, config);1858this.sortingFunc = this._getSortingFunc();1859}1860
1861Field.prototype = {1862name: "",1863title: null,1864css: "",1865align: "",1866width: 100,1867
1868visible: true,1869filtering: true,1870inserting: true,1871editing: true,1872sorting: true,1873sorter: "string", // name of SortStrategy or function to compare elements1874
1875headerTemplate: function() {1876return (this.title === undefined || this.title === null) ? this.name : this.title;1877},1878
1879itemTemplate: function(value, item) {1880return value;1881},1882
1883filterTemplate: function() {1884return "";1885},1886
1887insertTemplate: function() {1888return "";1889},1890
1891editTemplate: function(value, item) {1892this._value = value;1893return this.itemTemplate(value, item);1894},1895
1896filterValue: function() {1897return "";1898},1899
1900insertValue: function() {1901return "";1902},1903
1904editValue: function() {1905return this._value;1906},1907
1908_getSortingFunc: function() {1909var sorter = this.sorter;1910
1911if($.isFunction(sorter)) {1912return sorter;1913}1914
1915if(typeof sorter === "string") {1916return jsGrid.sortStrategies[sorter];1917}1918
1919throw Error("wrong sorter for the field \"" + this.name + "\"!");1920}1921};1922
1923jsGrid.Field = Field;1924
1925}(jsGrid, jQuery));1926
1927(function(jsGrid, $, undefined) {1928
1929var Field = jsGrid.Field;1930
1931function TextField(config) {1932Field.call(this, config);1933}1934
1935TextField.prototype = new Field({1936
1937autosearch: true,1938readOnly: false,1939
1940filterTemplate: function() {1941if(!this.filtering)1942return "";1943
1944var grid = this._grid,1945$result = this.filterControl = this._createTextBox();1946
1947if(this.autosearch) {1948$result.on("keypress", function(e) {1949if(e.which === 13) {1950grid.search();1951e.preventDefault();1952}1953});1954}1955
1956return $result;1957},1958
1959insertTemplate: function() {1960if(!this.inserting)1961return "";1962
1963return this.insertControl = this._createTextBox();1964},1965
1966editTemplate: function(value) {1967if(!this.editing)1968return this.itemTemplate.apply(this, arguments);1969
1970var $result = this.editControl = this._createTextBox();1971$result.val(value);1972return $result;1973},1974
1975filterValue: function() {1976return this.filterControl.val();1977},1978
1979insertValue: function() {1980return this.insertControl.val();1981},1982
1983editValue: function() {1984return this.editControl.val();1985},1986
1987_createTextBox: function() {1988return $("<input>").attr("type", "text")1989.prop("readonly", !!this.readOnly);1990}1991});1992
1993jsGrid.fields.text = jsGrid.TextField = TextField;1994
1995}(jsGrid, jQuery));1996
1997(function(jsGrid, $, undefined) {1998
1999var TextField = jsGrid.TextField;2000
2001function NumberField(config) {2002TextField.call(this, config);2003}2004
2005NumberField.prototype = new TextField({2006
2007sorter: "number",2008align: "right",2009readOnly: false,2010
2011filterValue: function() {2012return this.filterControl.val()2013? parseInt(this.filterControl.val() || 0, 10)2014: undefined;2015},2016
2017insertValue: function() {2018return this.insertControl.val()2019? parseInt(this.insertControl.val() || 0, 10)2020: undefined;2021},2022
2023editValue: function() {2024return this.editControl.val()2025? parseInt(this.editControl.val() || 0, 10)2026: undefined;2027},2028
2029_createTextBox: function() {2030return $("<input>").attr("type", "number")2031.prop("readonly", !!this.readOnly);2032}2033});2034
2035jsGrid.fields.number = jsGrid.NumberField = NumberField;2036
2037}(jsGrid, jQuery));2038
2039(function(jsGrid, $, undefined) {2040
2041var TextField = jsGrid.TextField;2042
2043function TextAreaField(config) {2044TextField.call(this, config);2045}2046
2047TextAreaField.prototype = new TextField({2048
2049insertTemplate: function() {2050if(!this.inserting)2051return "";2052
2053return this.insertControl = this._createTextArea();2054},2055
2056editTemplate: function(value) {2057if(!this.editing)2058return this.itemTemplate.apply(this, arguments);2059
2060var $result = this.editControl = this._createTextArea();2061$result.val(value);2062return $result;2063},2064
2065_createTextArea: function() {2066return $("<textarea>").prop("readonly", !!this.readOnly);2067}2068});2069
2070jsGrid.fields.textarea = jsGrid.TextAreaField = TextAreaField;2071
2072}(jsGrid, jQuery));2073
2074(function(jsGrid, $, undefined) {2075
2076var NumberField = jsGrid.NumberField;2077var numberValueType = "number";2078var stringValueType = "string";2079
2080function SelectField(config) {2081this.items = [];2082this.selectedIndex = -1;2083this.valueField = "";2084this.textField = "";2085
2086if(config.valueField && config.items.length) {2087var firstItemValue = config.items[0][config.valueField];2088this.valueType = (typeof firstItemValue) === numberValueType ? numberValueType : stringValueType;2089}2090
2091this.sorter = this.valueType;2092
2093NumberField.call(this, config);2094}2095
2096SelectField.prototype = new NumberField({2097
2098align: "center",2099valueType: numberValueType,2100
2101itemTemplate: function(value) {2102var items = this.items,2103valueField = this.valueField,2104textField = this.textField,2105resultItem;2106
2107if(valueField) {2108resultItem = $.grep(items, function(item, index) {2109return item[valueField] === value;2110})[0] || {};2111}2112else {2113resultItem = items[value];2114}2115
2116var result = (textField ? resultItem[textField] : resultItem);2117
2118return (result === undefined || result === null) ? "" : result;2119},2120
2121filterTemplate: function() {2122if(!this.filtering)2123return "";2124
2125var grid = this._grid,2126$result = this.filterControl = this._createSelect();2127
2128if(this.autosearch) {2129$result.on("change", function(e) {2130grid.search();2131});2132}2133
2134return $result;2135},2136
2137insertTemplate: function() {2138if(!this.inserting)2139return "";2140
2141return this.insertControl = this._createSelect();2142},2143
2144editTemplate: function(value) {2145if(!this.editing)2146return this.itemTemplate.apply(this, arguments);2147
2148var $result = this.editControl = this._createSelect();2149(value !== undefined) && $result.val(value);2150return $result;2151},2152
2153filterValue: function() {2154var val = this.filterControl.val();2155return this.valueType === numberValueType ? parseInt(val || 0, 10) : val;2156},2157
2158insertValue: function() {2159var val = this.insertControl.val();2160return this.valueType === numberValueType ? parseInt(val || 0, 10) : val;2161},2162
2163editValue: function() {2164var val = this.editControl.val();2165return this.valueType === numberValueType ? parseInt(val || 0, 10) : val;2166},2167
2168_createSelect: function() {2169var $result = $("<select>"),2170valueField = this.valueField,2171textField = this.textField,2172selectedIndex = this.selectedIndex;2173
2174$.each(this.items, function(index, item) {2175var value = valueField ? item[valueField] : index,2176text = textField ? item[textField] : item;2177
2178var $option = $("<option>")2179.attr("value", value)2180.text(text)2181.appendTo($result);2182
2183$option.prop("selected", (selectedIndex === index));2184});2185
2186$result.prop("disabled", !!this.readOnly);2187
2188return $result;2189}2190});2191
2192jsGrid.fields.select = jsGrid.SelectField = SelectField;2193
2194}(jsGrid, jQuery));2195
2196(function(jsGrid, $, undefined) {2197
2198var Field = jsGrid.Field;2199
2200function CheckboxField(config) {2201Field.call(this, config);2202}2203
2204CheckboxField.prototype = new Field({2205
2206sorter: "number",2207align: "center",2208autosearch: true,2209
2210itemTemplate: function(value) {2211return this._createCheckbox().prop({2212checked: value,2213disabled: true2214});2215},2216
2217filterTemplate: function() {2218if(!this.filtering)2219return "";2220
2221var grid = this._grid,2222$result = this.filterControl = this._createCheckbox();2223
2224$result.prop({2225readOnly: true,2226indeterminate: true2227});2228
2229$result.on("click", function() {2230var $cb = $(this);2231
2232if($cb.prop("readOnly")) {2233$cb.prop({2234checked: false,2235readOnly: false2236});2237}2238else if(!$cb.prop("checked")) {2239$cb.prop({2240readOnly: true,2241indeterminate: true2242});2243}2244});2245
2246if(this.autosearch) {2247$result.on("click", function() {2248grid.search();2249});2250}2251
2252return $result;2253},2254
2255insertTemplate: function() {2256if(!this.inserting)2257return "";2258
2259return this.insertControl = this._createCheckbox();2260},2261
2262editTemplate: function(value) {2263if(!this.editing)2264return this.itemTemplate.apply(this, arguments);2265
2266var $result = this.editControl = this._createCheckbox();2267$result.prop("checked", value);2268return $result;2269},2270
2271filterValue: function() {2272return this.filterControl.get(0).indeterminate2273? undefined2274: this.filterControl.is(":checked");2275},2276
2277insertValue: function() {2278return this.insertControl.is(":checked");2279},2280
2281editValue: function() {2282return this.editControl.is(":checked");2283},2284
2285_createCheckbox: function() {2286return $("<input>").attr("type", "checkbox");2287}2288});2289
2290jsGrid.fields.checkbox = jsGrid.CheckboxField = CheckboxField;2291
2292}(jsGrid, jQuery));2293
2294(function(jsGrid, $, undefined) {2295
2296var Field = jsGrid.Field;2297
2298function ControlField(config) {2299Field.call(this, config);2300this._configInitialized = false;2301}2302
2303ControlField.prototype = new Field({2304css: "jsgrid-control-field",2305align: "center",2306width: 50,2307filtering: false,2308inserting: false,2309editing: false,2310sorting: false,2311
2312buttonClass: "jsgrid-button",2313modeButtonClass: "jsgrid-mode-button",2314
2315modeOnButtonClass: "jsgrid-mode-on-button",2316searchModeButtonClass: "jsgrid-search-mode-button",2317insertModeButtonClass: "jsgrid-insert-mode-button",2318editButtonClass: "jsgrid-edit-button",2319deleteButtonClass: "jsgrid-delete-button",2320searchButtonClass: "jsgrid-search-button",2321clearFilterButtonClass: "jsgrid-clear-filter-button",2322insertButtonClass: "jsgrid-insert-button",2323updateButtonClass: "jsgrid-update-button",2324cancelEditButtonClass: "jsgrid-cancel-edit-button",2325
2326searchModeButtonTooltip: "Switch to searching",2327insertModeButtonTooltip: "Switch to inserting",2328editButtonTooltip: "Edit",2329deleteButtonTooltip: "Delete",2330searchButtonTooltip: "Search",2331clearFilterButtonTooltip: "Clear filter",2332insertButtonTooltip: "Insert",2333updateButtonTooltip: "Update",2334cancelEditButtonTooltip: "Cancel edit",2335
2336editButton: true,2337deleteButton: true,2338clearFilterButton: true,2339modeSwitchButton: true,2340
2341_initConfig: function() {2342this._hasFiltering = this._grid.filtering;2343this._hasInserting = this._grid.inserting;2344
2345if(this._hasInserting && this.modeSwitchButton) {2346this._grid.inserting = false;2347}2348
2349this._configInitialized = true;2350},2351
2352headerTemplate: function() {2353if(!this._configInitialized) {2354this._initConfig();2355}2356
2357var hasFiltering = this._hasFiltering;2358var hasInserting = this._hasInserting;2359
2360if(!this.modeSwitchButton || (!hasFiltering && !hasInserting))2361return "";2362
2363if(hasFiltering && !hasInserting)2364return this._createFilterSwitchButton();2365
2366if(hasInserting && !hasFiltering)2367return this._createInsertSwitchButton();2368
2369return this._createModeSwitchButton();2370},2371
2372itemTemplate: function(value, item) {2373var $result = $([]);2374
2375if(this.editButton) {2376$result = $result.add(this._createEditButton(item));2377}2378
2379if(this.deleteButton) {2380$result = $result.add(this._createDeleteButton(item));2381}2382
2383return $result;2384},2385
2386filterTemplate: function() {2387var $result = this._createSearchButton();2388return this.clearFilterButton ? $result.add(this._createClearFilterButton()) : $result;2389},2390
2391insertTemplate: function() {2392return this._createInsertButton();2393},2394
2395editTemplate: function() {2396return this._createUpdateButton().add(this._createCancelEditButton());2397},2398
2399_createFilterSwitchButton: function() {2400return this._createOnOffSwitchButton("filtering", this.searchModeButtonClass, true);2401},2402
2403_createInsertSwitchButton: function() {2404return this._createOnOffSwitchButton("inserting", this.insertModeButtonClass, false);2405},2406
2407_createOnOffSwitchButton: function(option, cssClass, isOnInitially) {2408var isOn = isOnInitially;2409
2410var updateButtonState = $.proxy(function() {2411$button.toggleClass(this.modeOnButtonClass, isOn);2412}, this);2413
2414var $button = this._createGridButton(this.modeButtonClass + " " + cssClass, "", function(grid) {2415isOn = !isOn;2416grid.option(option, isOn);2417updateButtonState();2418});2419
2420updateButtonState();2421
2422return $button;2423},2424
2425_createModeSwitchButton: function() {2426var isInserting = false;2427
2428var updateButtonState = $.proxy(function() {2429$button.attr("title", isInserting ? this.searchModeButtonTooltip : this.insertModeButtonTooltip)2430.toggleClass(this.insertModeButtonClass, !isInserting)2431.toggleClass(this.searchModeButtonClass, isInserting);2432}, this);2433
2434var $button = this._createGridButton(this.modeButtonClass, "", function(grid) {2435isInserting = !isInserting;2436grid.option("inserting", isInserting);2437grid.option("filtering", !isInserting);2438updateButtonState();2439});2440
2441updateButtonState();2442
2443return $button;2444},2445
2446_createEditButton: function(item) {2447return this._createGridButton(this.editButtonClass, this.editButtonTooltip, function(grid, e) {2448grid.editItem(item);2449e.stopPropagation();2450});2451},2452
2453_createDeleteButton: function(item) {2454return this._createGridButton(this.deleteButtonClass, this.deleteButtonTooltip, function(grid, e) {2455grid.deleteItem(item);2456e.stopPropagation();2457});2458},2459
2460_createSearchButton: function() {2461return this._createGridButton(this.searchButtonClass, this.searchButtonTooltip, function(grid) {2462grid.search();2463});2464},2465
2466_createClearFilterButton: function() {2467return this._createGridButton(this.clearFilterButtonClass, this.clearFilterButtonTooltip, function(grid) {2468grid.clearFilter();2469});2470},2471
2472_createInsertButton: function() {2473return this._createGridButton(this.insertButtonClass, this.insertButtonTooltip, function(grid) {2474grid.insertItem().done(function() {2475grid.clearInsert();2476});2477});2478},2479
2480_createUpdateButton: function() {2481return this._createGridButton(this.updateButtonClass, this.updateButtonTooltip, function(grid, e) {2482grid.updateItem();2483e.stopPropagation();2484});2485},2486
2487_createCancelEditButton: function() {2488return this._createGridButton(this.cancelEditButtonClass, this.cancelEditButtonTooltip, function(grid, e) {2489grid.cancelEdit();2490e.stopPropagation();2491});2492},2493
2494_createGridButton: function(cls, tooltip, clickHandler) {2495var grid = this._grid;2496
2497return $("<input>").addClass(this.buttonClass)2498.addClass(cls)2499.attr({2500type: "button",2501title: tooltip2502})2503.on("click", function(e) {2504clickHandler(grid, e);2505});2506},2507
2508editValue: function() {2509return "";2510}2511
2512});2513
2514jsGrid.fields.control = jsGrid.ControlField = ControlField;2515
2516}(jsGrid, jQuery));2517