kendoHelpers = { ///Pack of helper methods to make your life easier when working with Kendo UI® framework. ///Salar Khalilzadeh ///https://github.com/salarcode/kendoHelpers datasource: { findDataItemByUid: function (data, uid) { ///Finds the dataItem by Uid if (data == null || data.length == 0) return -1; for (var i = 0; i < data.length; i++) { var dataItem = data[i]; if (dataItem.uid == uid) { return dataItem; } } return -1; }, findDataIndexByUid: function (data, uid) { ///Finds the index of Uid if (data == null || data.length == 0) return -1; for (var i = 0; i < data.length; i++) { if (data[i].uid == uid) { return i; } } return -1; }, }, grid: { addRow: function (kendoGrid, editRow) { ///Adds a new row , then switches to edit mode if specified ///DataItem of the new row if succeeded, otherwise null var newRow = null; try { newRow = kendoGrid.dataSource.add(); var cellRow = kendoGrid.element.find("tr[data-uid=\"" + newRow.uid + "\"]"); kendoGrid.current(cellRow); if (editRow) kendoGrid.editRow(cellRow); } catch (e) { console.warn("addRow is failed, ", kendoGrid, e); } return newRow; }, editRrow: function (kendoGrid, dataItem) { ///Triggers edit mode for the specified dataItem ///N/A try { var cellRow = kendoGrid.element.find("tr[data-uid=\"" + dataItem.uid + "\"]"); kendoGrid.current(cellRow); kendoGrid.editRow(cellRow); } catch (e) { console.warn("editRrow is failed, ", kendoGrid, e); } }, getDataItemById: function (kendoGrid, id) { ///Returns the dataItem found by Id ///DataItem if found, otherwise null try { return kendoGrid.dataSource.get(id); } catch (e) { console.warn("getDataItemById is failed, id=" + id + " for ", kendoGrid, e); } return null; }, getColumnDefinition: function (kendoGrid, fieldName) { ///Gets the grid column definition ///Kendo Column definition if found, otherwise null try { var cols = kendoGrid.columns; for (var i = 0; i < cols.length; i++) { var col = cols[i]; if (col.field === fieldName) return col; } } catch (e) { console.warn("getColumnDefinition is failed, fieldName=" + fieldName, kendoGrid, e); } return null; }, getSelectedDataItem: function (kendoGrid) { ///Returns DataItem of the selected row. Selectable config is required ///DataItem if found, otherwise null try { var selected = kendoGrid.select(); return kendoGrid.dataItem(selected); } catch (e) { console.warn("getSelectedDataItem is failed, ", kendoGrid, e); } return null; }, getSelectedDataItemsList: function (kendoGrid) { ///Returns the list of DataItems from the selected rows. Selectable config is required ///DataItem Array if found, otherwise null try { var selected = kendoGrid.select(); if (selected.length == 0) { return null; } else { var dataItems = []; for (var i = 0; i < selected.length; i++) { dataItems.push(kendoGrid.dataItem(selected[i])); } return dataItems; } } catch (e) { console.warn("getSelectedDataItemsList is failed, ", kendoGrid, e); } return null; }, getSelectedDataItemByCurrentCell: function (kendoGrid) { ///Returns DataItem of the current active cell. Selectable config is not required. ///DataItem if found, otherwise null try { var cell = kendoGrid.current(); if (cell == null || cell.length == 0) return null; var row = cell.parent(); if (row == null || row.length == 0) return null; // retrieving from the row return kendoGrid.dataItem(row); } catch (e) { console.warn("getSelectedDataItemByCurrentCell is failed, ", kendoGrid, e); } return null; }, selectNextCell: function (kendoGrid, cell, editCell, editRow) { ///Selects the next cell of the current cell ///Optional. The current cell ///N/A try { if (cell == null || cell.length == 0) cell = kendoGrid.current(); if (cell == null || cell.length == 0) return; var colIndex = kendoGrid.cellIndex(cell); var cellRow = cell.parent("tr"); var rowUid = cellRow.data("uid"); var element = kendoGrid.element.find("tr[data-uid=\"" + rowUid + "\"] td:eq(" + (colIndex + 1) + ")"); kendoGrid.current(element); if (editCell) kendoGrid.editCell(element); if (editRow) kendoGrid.editRow(cellRow); } catch (e) { console.warn("selectNextCell is failed, ", kendoGrid, e); } }, selectCell: function (kendoGrid, cell, editCell, editRow) { ///Selects and activates the current/specified cell of the grid ///Optional. The current cell ///N/A try { if (cell == null || cell.length == 0) cell = kendoGrid.current(); if (cell == null || cell.length == 0) return; var colIndex = kendoGrid.cellIndex(cell); var cellRow = cell.parent("tr"); var rowUid = cellRow.data("uid"); var element = kendoGrid.element.find("tr[data-uid=\"" + rowUid + "\"] td:eq(" + (colIndex) + ")"); kendoGrid.current(element); if (editCell) kendoGrid.editCell(element); if (editRow) kendoGrid.editRow(cellRow); } catch (e) { console.warn("selectCell is failed, ", kendoGrid, e); } }, selectCellByIndex: function (kendoGrid, colIndex, cell, editCell, editRow) { ///Selects the next cell of the grid ///Optional. The current cell ///N/A try { if (cell == null || cell.length == 0) cell = kendoGrid.current(); if (cell == null || cell.length == 0) return; var cellRow = cell.parent("tr"); var rowUid = cellRow.data("uid"); var element = kendoGrid.element.find("tr[data-uid=\"" + rowUid + "\"] td:eq(" + (colIndex) + ")"); kendoGrid.current(element); if (editCell) kendoGrid.editCell(element); if (editRow) kendoGrid.editRow(cellRow); } catch (e) { console.warn("kendoGridSelectCellByIndex is failed, ", kendoGrid, e); } }, //selectCellByDataItem: function (kendoGrid, dataItem, editCell) { // ///Selects a cell by its dataItem // if (dataItem == null) // return; // var found = false; // var colIndex = 1; // kendoGrid.items().each(function () { // if (found) return; // var data = kendoGrid.dataItem(this); // if (data.uid == dataItem.uid) { // var element = kendoGrid.element.find("tr[data-uid=\"" + data.uid + "\"] td:eq(" + (colIndex) + ")"); // kendoGrid.current(element); // if (editCell) // kendoGrid.editCell(element); // found = true; // return; // } // }); //}, refreshAndKeepEditing: function (kendoGrid, editTheCurrent) { ///Refreshes the grid, keeps the cell in editing mode if there is any ///edit the current active instead of the cell in the edit mode ///N/A try { var currentCell = kendoGrid.element.find("td[id$='_active_cell']"); var currentRow = currentCell.parent('tr').attr("data-uid"); var currentCellIndex = currentCell.index(); var editedRowUid = kendoGrid.element.find("tr.k-grid-edit-row").attr("data-uid"); var cellIndex = kendoGrid.element.find("td.k-edit-cell").index(); kendoGrid.refresh(); // the timeout is to let the process continue other kendo events setTimeout(function () { // should focus on the current instead of the item being edited if (editTheCurrent) { // is in edit mode? if (cellIndex >= 0) { kendoGrid.editCell(kendoGrid.element.find("tr[data-uid='" + currentRow + "'] td:eq(" + currentCellIndex + ")")); } else { kendoGrid.current(kendoGrid.element.find("tr[data-uid='" + currentRow + "'] td:eq(" + currentCellIndex + ")")); } } else { if (cellIndex >= 0 && editedRowUid) { kendoGrid.editCell(kendoGrid.element.find("tr[data-uid='" + editedRowUid + "'] td:eq(" + cellIndex + ")")); } else if (editedRowUid) { kendoGrid.editRow(kendoGrid.element.find("tr[data-uid='" + editedRowUid + "']")); } else { kendoGrid.current(kendoGrid.element.find("tr[data-uid='" + currentRow + "'] td:eq(" + currentCellIndex + ")")); } } }, 50); } catch (e) { console.warn("refreshAndKeepEditing is failed, ", kendoGrid, e); } }, selectRowByUid: function (kendoGrid, rowUid) { ///Activates and selects the specified row by Uid ///N/A try { var row = kendoGrid.element.find("tr[data-uid=\"" + rowUid + "\"]"); kendoGrid.current(row); try { kendoGrid.select(row); } catch (e) { // catch the error if the grid is not selectable } } catch (e) { console.warn("selectRowByUid is failed, ", kendoGrid, rowUid, " > error> ", e); } }, selectRowByIndex: function (kendoGrid, rowNumber, editRow, colIndex, editCell) { ///Activates and selects the row by row number. Also selects the cell if requested ///N/A try { if (!colIndex) colIndex = 0; if (!rowNumber) rowNumber = 1; var row = kendoGrid.element.find("tr:eq(" + rowNumber + ")"); var cell = row.find("td:eq(" + colIndex + ")"); kendoGrid.current(cell); try { kendoGrid.select(row); } catch (e) { // catch the error if the grid is not selectable } if (editCell) kendoGrid.editCell(cell); if (editRow) kendoGrid.editRow(row); } catch (e) { console.warn("selectRowByIndex is failed, ", kendoGrid, e); } }, selectByCondition: function (kendoGrid, conditionFunc) { ///Selects a row if provided function applies ///N/A if (conditionFunc == null) return; var found = false; kendoGrid.items().each(function () { if (found) return; var data = kendoGrid.dataItem(this); if (conditionFunc(data)) { try { kendoGrid.select(this); } catch (e) { console.warn("selectByCondition is failed, maybe the grid is not selectable. ", kendoGrid, e); } found = true; return; } }); }, selectCellByCondition: function (kendoGrid, conditionFunc, editCell) { ///Activates a cell by provided function ///N/A if (conditionFunc == null) return; var found = false; var colIndex = 1; kendoGrid.items().each(function () { if (found) return; var data = kendoGrid.dataItem(this); if (conditionFunc(data)) { var element = kendoGrid.element.find("tr[data-uid=\"" + data.uid + "\"] td:eq(" + (colIndex) + ")"); kendoGrid.current(element); if (editCell) kendoGrid.editCell(element); found = true; return; } }); }, eventRowDoubleClick: function (kendoGrid, onDoubleClick) { ///Double click event on rows for grid var element = kendoGrid.element; if (element == null) { console.error("eventRowDoubleClick is failed because of null kendoGrid.element", kendoGrid); return; } element.on("dblclick", " tbody > tr", function () { if (onDoubleClick) { var dataItem = kendoGrid.dataItem($(this)); onDoubleClick(dataItem); } }); }, eventCellDoubleClick: function (kendoGrid, onDoubleClick) { ///Double click event on cells for grid var element = kendoGrid.element; if (element == null) { console.error("eventCellDoubleClick failed because of null kendoGrid.element", kendoGrid); return; } element.on("dblclick", " tbody > tr > td", function () { if (onDoubleClick) { var dataItem = kendoGrid.dataItem($(this)); debugger; onDoubleClick(dataItem); } }); }, }, tabstrip: { displayLoading: function (tabstrip) { ///Displays loading process on the tabStrip ///N/A if (tabstrip == null) return; function showLoading() { window.setTimeout(function () { kendo.ui.progress(tabstrip.element, true); }); } function hideLoading() { window.setTimeout(function () { kendo.ui.progress(tabstrip.element, false); }); } tabstrip.bind("select", showLoading); tabstrip.bind("activate", hideLoading); tabstrip.bind("contentLoad", hideLoading); }, showHideTab: function (tabstrip, tabIndex, hide) { ///Show/hide the specified tab ///N/A try { if (hide) { $(tabstrip.items()[tabIndex]).css("display", "none"); } else { $(tabstrip.items()[tabIndex]).css("display", "inline-block"); } } catch (e) { console.warn("tabStripShowHide is failed, tabIndex=" + tabIndex, tabstrip, e); } } }, listview: { selectRowByUid: function (kendoList, rowUid) { ///Selects the specified row by Uid ///N/A try { var element = kendoList.element.find("div[data-uid=\"" + rowUid + "\"]"); kendoList.select(element); } catch (e) { console.warn("selectRowByUid is failed ", kendoList, rowUid, " > error> ", e); } }, }, treelist: { actionForSubItems: function (treeList, dataItem, action, onlyFirstLevel) { ///Executes a callback for each sub item ///If true, only the direct children of the item will be applied ///N/A if (dataItem == null || action == null) return; var children = treeList.dataSource.childNodes(dataItem); for (var i = 0; i < children.length; i++) { var child = children[i]; action(child); if (!onlyFirstLevel) kendoHelpers.treelist.actionForSubItems(treeList, child, action, onlyFirstLevel); } //// the dev should update the tree to replicate the changes to the UI // treeList.refresh(); }, getColumnDefinition: function (treeList, fieldName) { ///[Alias] Gets the treeList column definition ///Kendo Column definition if found, otherwise null return kendoHelpers.grid.getColumnDefinition(treeList, fieldName); }, }, treeview: { applyRightClickSelection: function (treeview, onRightClick) { ///Makes treeview select the item on the right click ///N/A var element = treeview.element; if (element == null) { console.error("applyRightClickSelection failed because of null treeview.element", treeview); return; } element.on("mousedown", ".k-item", function (event) { if (event.which === 3) { event.stopPropagation(); // to avoid propagation of this event to the root of the treeview treeview.select(this); if (onRightClick) { onRightClick(); } } }); }, checkSubItems: function (node, check, checkSubItems) { ///Checks the node and its sub items ///N/A var nodeChildren = node.children.view(); for (var i = 0; i < nodeChildren.length; i++) { var n = nodeChildren[i]; n.checked = check; // make the tree update itself n.trigger("change", { field: "checked" }); if (checkSubItems && n.hasChildren) { kendoHelpers.treeview.checkSubItems(n, check, checkSubItems); } } }, getCheckedItems: function (treeview, uncheck) { ///Returns the checked items if there is any ///DataItem Array if found, otherwise empty array ///Should uncheck the nodes // Source: http://blogs.telerik.com/kendoui/posts/13-10-17/how-to-get-the-checked-items-from-a-treeview-with-checkboxes var nodes = treeview.dataSource.view(); function getCheckedNodes(nodes) { var node, childCheckedNodes; var checkedNodes = []; for (var i = 0; i < nodes.length; i++) { node = nodes[i]; if (node.checked) { checkedNodes.push(node); if (uncheck) { node.checked = false; } } // to understand recursion, first // you must understand recursion if (node.hasChildren) { childCheckedNodes = getCheckedNodes(node.children.view()); if (childCheckedNodes.length > 0) { checkedNodes = checkedNodes.concat(childCheckedNodes); } } } return checkedNodes; } return getCheckedNodes(nodes); }, reloadSelectedNode: function (treeview, childrenOnly) { ///Reloads the selected node's parent in order to refresh the node itself ///If true, only the children of the selected node will update try { var item = treeview.select(); if (item != null) { var dataItem = treeview.dataItem(item); if (childrenOnly && dataItem && dataItem.hasChildren) { dataItem.loaded(false); dataItem.load(); } else { var parent = treeview.parent(item); var parentDataItem = treeview.dataItem(parent); if (parentDataItem != null) { parentDataItem.loaded(false); parentDataItem.load(); } else { treeview.dataSource.read(); } } } } catch (e) { console.warn("treeViewReloadSelectedNode is failed. No node is selected. Realoding the whole tree", treeview); treeview.dataSource.read(); } } }, upload: { hasAnyFileSelected: function (kendoUpload) { ///Check if any file is selected ///Boolean if (!kendoUpload.wrapper) { return false; } return kendoUpload.wrapper.find(".k-file").length > 0; }, getUploadElements: function (kendoUpload) { ///Retrieving the upload elements from the upload control ///Array of elements if found, otherwise null if (!kendoUpload.wrapper) { return null; } return kendoUpload.wrapper.find("[data-role=upload]"); }, }, dropdown: { //makeItemsAutoWith: function (kendoDropDown) { // ///Makes drop-down items auto width // var name = "#" + kendoDropDown.element.attr('id') + "-list"; // $(name).addClass('k-dropdown-items-auto-width'); // //.k-dropdown-items-auto-width.k-list-container { // // -moz-min-width: 130px !important; // // -ms-min-width: 130px !important; // // -o-min-width: 130px !important; // // -webkit-min-width: 130px !important; // // min-width: 130px !important; // // width: auto !important; // //} // //.k-dropdown-items-auto-width.k-list-container .k-list { // // width: auto !important; // //} //}, }, validator: { isValid: function (form) { ///Triggers kendo validation for form and returns the status of form ///Boolean try { form = $(form); if (form == null || form.length == 0) return false; var validator = form.kendoValidator().data("kendoValidator"); if (!validator.validate()) return false; return true; } catch (e) { console.warn("isValid is failed, ", form, e); return false; } }, hideMessages: function (form) { ///Enables kendo validation for form then hides any visible message ///N/A try { form = $(form); if (form == null || form.length == 0) return; var validator = form.kendoValidator().data("kendoValidator"); validator.hideMessages(); } catch (e) { console.warn("hideMessages is failed, ", form, e); } }, }, getDataItemById: function (kendoWidget, id) { ///Returns the dataItem found by Id ///DataItem if found, otherwise null try { return kendoWidget.dataSource.get(id); } catch (e) { console.warn("getDataItemById is failed, id=" + id + " for ", kendoWidget, e); } return null; }, };