You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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