/*!@license
 * Infragistics.Web.ClientUI Tree Grid 21.2.12
 *
 * Copyright (c) 2011-2022 Infragistics Inc.
 *
 * http://www.infragistics.com/
 *
 * Depends on:
 *	jquery-1.9.1.js
 *	jquery.ui.core.js
 *	jquery.ui.widget.js
 *	infragistics.ui.grid.framework.js
 *	infragistics.ui.editors.js
 *	infragistics.ui.shared.js
 *	infragistics.dataSource.js
 *	infragistics.util.js
 *	infragistics.ui.treegrid.js
 *	infragistics.ui.grid.rowselectors.js
 */
(function(factory){if(typeof define==="function"&&define.amd){define(["./infragistics.ui.treegrid","./infragistics.ui.grid.rowselectors"],factory)}else{return factory(jQuery)}})(function($){"use strict";$.widget("ui.igTreeGridRowSelectors",$.ui.igGridRowSelectors,{options:{rowSelectorNumberingMode:"sequential",checkBoxMode:"biState",inherit:false},css:{rowSelectorChecked:"ui-state-checked",checkBoxP:"ui-icon ui-icon-check-p ui-igcheckbox-normal-on",checkBoxF:"ui-icon ui-icon-check-f ui-igcheckbox-normal-on"},_create:function(){this.element.data($.ui.igGridRowSelectors.prototype.widgetName,this.element.data($.ui.igTreeGridRowSelectors.prototype.widgetName));$.ui.igGridRowSelectors.prototype._create.apply(this,arguments);this._cIndexes={};this._indexCache={};this._path="";this._pathBuild=false;this._nCache={};this._recordNumbers={};this._checkboxStates={}},destroy:function(){this._superApply(arguments);this.element.removeData($.ui.igGridRowSelectors.prototype.widgetName)},changeLocale:function(){this._superApply(arguments)},_recordsRendering:function(){$.ui.igGridRowSelectors.prototype._recordsRendering.apply(this,arguments);if(this.options.rowSelectorNumberingMode==="sequential"){if(this._cIdx===this.options.rowNumberingSeed){this._numberRecords(this.grid.dataSource.data())}return}this._cIndexes={};this._pathBuild=false;this._nCache={};this._numberRecordsWithParents(this.grid.dataSource.data(),"")},_rsExpandCollapseRow:function(row,expand){if(expand){this._cIndexes={};this._path=(row instanceof $?row.find("span.ui-iggrid-rowselector-row-number").html():this.element.find("[data-id='"+row+"']").find("span.ui-iggrid-rowselector-row-number").html())+".";this._pathBuild=true}$.ui.igTreeGrid.prototype._expandCollapseRow.apply(this.grid,arguments)},_applyNumberToNewChild:function(directParent,row){var directParentId,childrenLength,rsNumber;directParentId=directParent[this.grid.options.primaryKey];childrenLength=directParent[this.grid.options.childDataKey]?directParent[this.grid.options.childDataKey].length:1;if(!$.isEmptyObject(this._nCache)){rsNumber=this._nCache[directParentId]+"."+childrenLength;this._nCache[row.data("id")]=rsNumber}else if(this.element.find("[data-id='"+directParentId+"']").length!==0){rsNumber=this.element.find("[data-id='"+directParentId+"']").find("span.ui-iggrid-rowselector-row-number").html();rsNumber+="."+childrenLength}row.find("span.ui-iggrid-rowselector-row-number").html(rsNumber)},_childRowRendering:function(args){if(this.options.rowSelectorNumberingMode==="sequential"){return}this._getCurrentNumberForNewChild(args.parentId,args.id);this._nCache[args.parentId].children++},_redirectFunctions:function(){if(this._functionsRedirected===false){this._expandCollapseRowHandler=$.proxy(this._rsExpandCollapseRow,this);if(this.options.rowSelectorNumberingMode!=="sequential"){this.grid._expandCollapseRow=this._expandCollapseRowHandler}if(this.options.checkBoxMode!=="biState"){this._select=$.proxy(this._selectHandler,this);this._deselect=$.proxy(this._deselectHandler,this);this._handleCheck=$.proxy(this._handleTriStateCheck,this);this._handleHeaderCheck=$.proxy(this._handleTriStateHeaderCheck,this)}}$.ui.igGridRowSelectors.prototype._redirectFunctions.apply(this,arguments)},_injectGrid:function(){var i;$.ui.igGridRowSelectors.prototype._injectGrid.apply(this,arguments);for(i=0;i<this.grid.options.features.length;i++){if(this.grid.options.features[i].name==="Selection"){if(this.grid.options.features[i].multipleSelection&&this.options.enableCheckBoxes&&this.options.checkBoxMode==="triState"){throw new Error(this._getLocaleValue("multipleSelectionWithTriStateCheckboxesNotSupported"))}break}}},_shouldRenderHeaderCheckBoxes:function(){var o=this.options;return o.enableCheckBoxes===true&&o.checkBoxMode==="triState"||o.enableCheckBoxes===true&&o.checkBoxMode==="biState"&&this._ms},_updateHeader:function(){var sl,check=true,i,dv=this.grid.dataSource.flatDataView(),dvl=dv.length,selection=this._getSelectionInstance(),isSelected,pk=selection._pkProp;if(this.grid.element.data("igGridPaging")){for(i=0;i<dvl;i++){isSelected=this.options.checkBoxMode==="triState"?this._checkboxStates[dv[i][selection._pkProp]]:this.grid._selection.isSelected(pk!==null?dv[i][pk]:i,this.grid);if(!isSelected){check=false;break}}}else{dvl=this.grid.dataSource.getFlatData().length;sl=this.grid._selection?this.grid._selection.selectionLength():0;if(this.options.checkBoxMode==="triState"){check=this._areAllRecordsChecked(sl,dvl)}else{check=sl===dvl&&this._isFirstRowSelected()}}this._alterCheckbox(this._headerCheckbox(),check)},_rowExpanded:function(ui){if(this.options.rowSelectorNumberingMode==="sequential"){this._reapplyNumbering(ui)}if(this.options.enableCheckBoxes===true&&this._ms){this._updateHeader()}},_rowCollapsed:function(ui){if(this.options.rowSelectorNumberingMode==="sequential"){this._reapplyNumbering(ui)}},_getCurrentNumber:function(rowData){var pKey=this.grid.options.primaryKey,path;if(this.options.rowSelectorNumberingMode==="sequential"&&!$.isEmptyObject(this._recordNumbers)){return"<span class='ui-iggrid-rowselector-row-number'>"+(this._recordNumbers[rowData[pKey]]||"")+"</span>"}if(!$.isEmptyObject(this._nCache)){if(rowData.hasOwnProperty("__fr")){path=this._getCurrentNumberForNewChild(rowData.__fr)}else if(this._nCache[rowData[pKey]]){path=this._nCache[rowData[pKey]].path||""}else{this._nCache[rowData[pKey]]={children:0,path:String(this._getNextNumberForParent())};path=this._nCache[rowData[pKey]].path}return"<span class='ui-iggrid-rowselector-row-number'>"+path+"</span>"}return this._super(rowData)},_getCurrentNumberForNewChild:function(parentId,rowId){var childIdx=this._nCache[parentId].path+"."+(this._nCache[parentId].children+1);if(rowId!==undefined){this._nCache[rowId]={path:childIdx,children:0}}return childIdx},_getNextNumberForParent:function(){var i,pt=this.grid.dataSource.pendingTransactions(),count=0;for(i=0;i<pt.length;i++){if(pt[i].type==="newrow"){count++}}return this.grid.dataSource.data().length+count},_getRowSelectorCellMarkup:function(selected,rowData){if(!rowData){return""}var markup="",o=this.options,go=this.grid.options,pKey=go.primaryKey,rowId=rowData[pKey],checkStateDefined=this._checkboxStates[rowId]!==undefined&&this._checkboxStates[rowId]!==null;markup+="<th role='rowheader' tabindex='"+go.tabIndex+"' class='"+this.css.rowSelector;if(selected){markup+=" "+this.css.rowSelectorSelected}if(checkStateDefined){markup+=" "+this.css.rowSelectorChecked}markup+="'><span class='ui-icon ui-icon-triangle-1-e' style='margin-left: -5px'></span>";if(o.enableRowNumbering){markup+=this._getCurrentNumber(rowData)}if(o.enableCheckBoxes){markup+=this._getTriStateCheckBox(rowData,selected&&o.checkBoxMode!=="triState"||checkStateDefined)}markup+="</th>";return markup},_getTriStateCheckBox:function(rowData,checked){var markup="",dataChk="off",checkBoxState=this.css.checkBoxOff,pKey=this.grid.options.primaryKey;if(checked){dataChk="on";checkBoxState=this._checkboxStates[rowData[pKey]]===0?this.css.checkBoxP:this.css.checkBoxF}markup+="<span name='"+"chk"+"' ";markup+="data-chk='"+dataChk+"' ";markup+="data-role='checkbox' class='"+this.css.checkBox+" '";markup+="tabindex='"+this.grid.options.tabIndex+" '";if(this.options.showCheckBoxesOnFocus===true&&this._checkBoxesShown===false){markup+=" style='visibility: hidden;'"}markup+="><span class='"+checkBoxState+"'>";markup+="</span></span>";return markup},_numberRecordsWithParents:function(data,currentPath){var pKey=this.grid.options.primaryKey,childDataKey=this.grid.options.childDataKey,counter=1,cPath,dataLen,i,dataRow;if($.type(data)==="array"){dataLen=data.length;for(i=0;i<dataLen;i++,counter++){dataRow=data[i];cPath=(currentPath===""?"":currentPath+".")+counter.toString();this._nCache[dataRow[pKey]]={path:cPath,children:dataRow[childDataKey]?dataRow[childDataKey].length||0:0};if(dataRow[childDataKey]!==undefined&&dataRow[childDataKey]!==null){this._numberRecordsWithParents(dataRow[childDataKey],cPath)}}}},_numberRecords:function(data){var pKey=this.grid.options.primaryKey,childDataKey=this.grid.options.childDataKey,expanded=this.grid.options.dataSourceSettings.expandedKey,dataLen,i,dataRow;if($.type(data)==="array"){dataLen=data.length;for(i=0;i<dataLen;i++){dataRow=data[i];if(this._recordNumbers===undefined||this._recordNumbers===0){this._cIdx=this.options.rowNumberingSeed;this._recordNumbers={}}this._recordNumbers[dataRow[pKey]]=++this._cIdx;if(dataRow[expanded]&&dataRow[childDataKey]!==undefined&&dataRow[childDataKey]!==null){this._numberRecords(dataRow[childDataKey])}}}},_reapplyNumbering:function(ui){var nextRowId,nextRow=ui.fixedRow||ui.row,rsSelector="span.ui-iggrid-rowselector-row-number";this._cIdx=this.options.rowNumberingSeed;this._numberRecords(this.grid.dataSource.data());while(nextRow.length===1){nextRowId=nextRow.attr("data-id");nextRow.find(rsSelector).text(this._recordNumbers[nextRowId]);nextRow=nextRow.next("tr[aria-level]")}},_selectHandler:function(info){var res;info.element=info.element||this._getRowById(info.id);res=this._rowSelectorFromSelection(info);if(!res){return}res.rowSelector.addClass(this.css.rowSelectorSelected)},_deselectHandler:function(info){var res,shouldDeselect;info.element=info.element||this._getRowById(info.id);res=this._rowSelectorFromSelection(info);if(!res){return}shouldDeselect=this.grid._selection instanceof $.ig.SelectedRowsCollection||!this.grid._selection.atLeastOneSelected(this.grid._fixPKValue(info.element.parent().attr("data-id")));if(shouldDeselect){res.rowSelector.removeClass(this.css.rowSelectorSelected)}},_changeParentsCheckState:function(rowId,toCheck){var grid=this.grid,pKey=grid.options.primaryKey,action=$.proxy(toCheck?this._checkParent:this._uncheckParent,this),parents,res,i,element,parentRow;parents=grid.dataSource.getParentRowsForRow(rowId);for(i=parents.length-1;i>=0;i--){parentRow=parents[i].row;element=this._getRowById(parentRow[pKey]);res=this._rowSelectorFromSelection({element:element});action(parentRow,res)}},_checkParent:function(parentRow,res){var pKey=this.grid.options.primaryKey,childDataKey=this.grid.options.childDataKey,parentRowId=parentRow[pKey];if(this._areAllChildrenChecked(parentRow[childDataKey])){this._checkboxStates[parentRowId]=1;res.checkbox.parent().addClass(this.css.rowSelectorChecked)}else{this._checkboxStates[parentRowId]=0;res.checkbox.parent().addClass(this.css.rowSelectorChecked)}this._alterTriStateCheckbox(res.checkbox,this._checkboxStates[parentRowId])},_uncheckParent:function(parentRow,res){var pKey=this.grid.options.primaryKey,childDataKey=this.grid.options.childDataKey,parentRowId=parentRow[pKey];if(this._hasCheckedChildren(parentRow[childDataKey])){this._checkboxStates[parentRowId]=0;this._alterTriStateCheckbox(res.checkbox,0)}else{delete this._checkboxStates[parentRowId];res.checkbox.parent().removeClass(this.css.rowSelectorChecked);this._alterTriStateCheckbox(res.checkbox)}},_changeChildrenCheckState:function(rowId,toCheck){var grid=this.grid,children=grid.dataSource.getChildrenByKey(rowId,grid.dataSource._data)||[],pKey=grid.options.primaryKey,childDataKey=grid.options.childDataKey,i,childId,child,element,res;for(i=0;i<children.length;i++){child=children[i];childId=child[pKey];element=this._getRowById(childId);res=this._rowSelectorFromSelection({element:element});if(toCheck){this._checkboxStates[childId]=1;res.checkbox.parent().addClass(this.css.rowSelectorChecked);this._alterTriStateCheckbox(res.checkbox,1)}else{delete this._checkboxStates[childId];res.checkbox.parent().removeClass(this.css.rowSelectorChecked);this._alterTriStateCheckbox(res.checkbox)}if($.type(child[childDataKey])==="array"){this._changeChildrenCheckState(childId,toCheck)}}},_areAllChildrenChecked:function(children){var i,childrenLen=children?children.length:0,pKey=this.grid.options.primaryKey,childId,state;for(i=0;i<childrenLen;i++){childId=children[i][pKey];state=this._checkboxStates[childId];if(state===null||state===undefined||state===0){return false}}return true},_hasCheckedChildren:function(children){var i,childrenLen=children?children.length:0,pKey=this.grid.options.primaryKey,childId,state;for(i=0;i<childrenLen;i++){childId=children[i][pKey];state=this._checkboxStates[childId];if(state!==undefined&&state!==null){return true}}return false},_rrn:function(){var rs=this._allRowSelectorCells(),cb=this._allCheckboxes(),cbx,i=0,self=this,sri=this.grid._startRowIndex||0,row,rowId,state;if(!this._skipRefresh){rs.removeClass(this.css.rowSelectorSelected);cb.map(function(){self._alterCheckbox($(this),false)});for(i=0;i<rs.length;i++){row=rs.eq(i).closest("tr");rowId=this.grid._fixPKValue(row.attr("data-id"));state=this._checkboxStates[rowId];if(rowId===null||rowId===undefined){rowId=i+sri}if(this.grid._selection.selection[rowId]!==undefined||state!==undefined&&state!==null){if(this.grid._selection.selection[rowId]){rs.eq(i).addClass(this.css.rowSelectorSelected)}cbx=cb.eq(i);if(cbx.length===1){if(this.options.checkBoxMode!=="triState"&&(state===undefined||state===null)){this._alterCheckbox(cbx,true)}else{this._alterTriStateCheckbox(cbx,state)}}}}}},_alterTriStateCheckbox:function(checkbox,checkState){var inner=checkbox.children().first();if(checkbox.length>0&&inner.length>0){if(checkState===0){checkbox.attr("data-chk","mixed");inner.removeClass(this.css.checkBoxOff).removeClass(this.css.checkBoxF).addClass(this.css.checkBoxP)}else if(checkState===1){checkbox.attr("data-chk","on");inner.removeClass(this.css.checkBoxOff).removeClass(this.css.checkBoxP).addClass(this.css.checkBoxF)}else{checkbox.attr("data-chk","off");inner.removeClass(this.css.checkBoxP).removeClass(this.css.checkBoxF).addClass(this.css.checkBoxOff)}}},_clearSelection:function(){var rsCells,self=this;if(this.grid.hasFixedColumns()&&this.grid.fixingDirection()==="left"){rsCells=this.grid.fixedBodyContainer().find("tbody").find("th.ui-iggrid-selectedcell").removeClass(this.css.rowSelectorSelected)}else{rsCells=this.grid.element.children("tbody").find("th.ui-iggrid-selectedcell").removeClass(this.css.rowSelectorSelected)}if(this.options.enableCheckBoxes&&this.options.checkBoxMode!=="triState"){$.each(rsCells,function(){self._alterCheckbox($(this).children("span:last"),false)});this._alterCheckbox(this._headerCheckbox(),false)}},_areAllRecordsChecked:function(sl,dvl){var size=0;for(var key in this._checkboxStates){if(this._checkboxStates.hasOwnProperty(key)){if(this._checkboxStates[key]===0){return false}if(this._checkboxStates[key]===1){size++}}}return size===dvl},_getDataView:function(){return this.grid.dataSource.flatDataView()},_getAllData:function(){if(this._flatData){return this._flatData}else{this._flatData=this.grid.dataSource.generateFlatData().flatData;return this._flatData}},_handleTriStateCheck:function(checkbox){var row,rowId,upd,sel,offset;if(!checkbox){return}upd=this.grid.element.data("igGridUpdating");sel=this._getSelectionInstance();if(sel&&sel._suspend){if(upd){if(upd.findInvalid()){return}upd._endEdit(null,true)}else{return}}offset=this._v?this.grid._startRowIndex:0;row=checkbox.parent().parent();if(this.grid.hasFixedColumns()){if(this.grid.fixedBodyContainer().attr("data-fixing-direction")==="left"){row=row.add(this.grid.element.find("tbody > tr").eq(row.index()))}else{row=row.add(this.grid.fixedBodyContainer().find("tbody > tr").eq(row.index()))}}rowId=this.grid._fixPKValue(row.attr("data-id"));if(rowId===null||rowId===undefined){rowId=row.closest("tbody").children("tr:not([data-container])").index(row)+offset}this.changeCheckStateById(rowId,checkbox.attr("data-chk")==="off");if(!this._suspendHeader){this._updateHeader()}},_handleTriStateHeaderCheck:function(checkbox){var dv=this._getDataView(),elem=this.grid.element,toCheck=checkbox.attr("data-chk")==="off",templateData,c,all;this._changeCheckStateForRows(dv,toCheck);this._alterCheckbox(checkbox,toCheck);if(this.options.enableSelectAllForPaging&&elem.data("igGridPaging")){c=this.checkedRows().length;all=this._getAllData().length;templateData=[{checked:c,unchecked:all-c,allCheckedRecords:c,totalRecordsCount:all}];this._renderOverlay(templateData,toCheck)}},_selectAllFromOverlay:function(){if(this.options.checkBoxMode==="biState"){this._changeCheckStateForAllRecords(this._getSelectionInstance(),true)}else{this._changeCheckStateForRows(this._getAllData(),true)}},_deselectAllFromOverlay:function(){if(this.options.checkBoxMode==="biState"){this._changeCheckStateForAllRecords(this._getSelectionInstance(),false)}else{this._changeCheckStateForRows(this._getAllData(),false)}},_getRowById:function(identifier){var urow,frow=$();if(this.grid.hasFixedColumns()){frow=this.grid.rowById(identifier,true)}urow=this.grid.rowById(identifier,false);return urow instanceof $?urow.add(frow):$()},_changeCheckStateForRows:function(dv,toCheck){var i,res,element,rowId,pKey=this.grid.options.primaryKey;for(i=0;i<dv.length;i++){rowId=dv[i][pKey];element=this._getRowById(rowId);res=this._rowSelectorFromSelection({element:element});this._alterCheckbox(res.checkbox,toCheck);this._changeChildrenCheckState(rowId,toCheck);this._changeParentsCheckState(rowId,toCheck)}},_changeTriStateById:function(rowId,toCheck){var element=this._getRowById(rowId),res=this._rowSelectorFromSelection({element:element}),checkbox=res.checkbox;if(toCheck){checkbox.parent().addClass("ui-state-checked")}else{checkbox.parent().removeClass("ui-state-checked")}this._alterCheckbox(checkbox,toCheck);this._changeChildrenCheckState(rowId,toCheck);this._changeParentsCheckState(rowId,toCheck)},changeCheckStateById:function(rowId,toCheck){if(this.options.checkBoxMode==="biState"){if(toCheck){this._selection.selectRowById(rowId);return}this._selection.deselectRowById(rowId);return}this._changeTriStateById(rowId,toCheck)},changeCheckState:function(index,toCheck){var pKey=this.grid.options.primaryKey,rowId=this._getDataView()[index][pKey];this.changeCheckStateById(rowId,toCheck)},toggleCheckStateById:function(rowId){var element=this._getRowById(rowId),res=this._rowSelectorFromSelection({element:element}),checkbox=res.checkbox;if(checkbox.attr("data-chk")==="off"){this.changeCheckStateById(rowId,true)}else{this.changeCheckStateById(rowId,false)}},toggleCheckState:function(index){var pKey=this.grid.options.primaryKey,rowId=this._getDataView()[index][pKey];this.toggleCheckStateById(rowId)},_getIndexForRow:function(row){return row.closest("tbody").children("tr:not([data-container])").index(row)+(this.grid._startRowIndex||0)},_rowObject:function(rowId){var rowInfo={};rowInfo.element=this._getRowById(rowId);rowInfo.index=this._getIndexForRow(rowInfo.element);return $.extend(true,{},rowInfo,{id:rowId})},_checkForRequireSelectionWithCheckboxes:function(){return this.options.requireSelection===false&&this.options.enableCheckBoxes===true&&this.options.checkBoxMode==="biState"},checkedRows:function(){var checkStateKey,rows=[];if(this.options.checkBoxMode==="biState"){if(this._ms){return this._selection.selectedRows()}return this._selection.selectedRow()?[this._selection.selectedRow()]:[]}for(checkStateKey in this._checkboxStates){if(this._checkboxStates.hasOwnProperty(checkStateKey)&&this._checkboxStates[checkStateKey]===1){rows.push(this._rowObject(this.grid._fixPKValue(checkStateKey)))}}return rows},partiallyCheckedRows:function(){var checkStateKey,rows=[];for(checkStateKey in this._checkboxStates){if(this._checkboxStates.hasOwnProperty(checkStateKey)&&this._checkboxStates[checkStateKey]===0){rows.push(this._rowObject(this.grid._fixPKValue(checkStateKey)))}}return rows},uncheckedRows:function(){var dv=this._getAllData(),rows=[],i,rowId,pKey=this.grid.options.primaryKey,checkedRows=this.options.checkBoxMode==="biState"?this.grid._selection.selection:this._checkboxStates;for(i=0;i<dv.length;i++){rowId=dv[i][pKey];if(checkedRows[rowId]===undefined||checkedRows[rowId]===null){rows.push(this._rowObject(this.grid._fixPKValue(rowId)))}}return rows},checkStateById:function(rowId){if(this.options.checkBoxMode==="biState"){return this.grid._selection.selection[rowId]?"on":"off"}if(this._checkboxStates[rowId]===undefined||this._checkboxStates[rowId]===null){return"off"}if(this._checkboxStates[rowId]===1){return"on"}if(this._checkboxStates[rowId]===0){return"partial"}}});$.extend($.ui.igTreeGridRowSelectors,{version:"21.2.12"});return $});