oi-metrology/Archive/wwwroot/igniteui/js/extensions/infragistics.ui.grid.knockout-extensions.js
2022-07-26 09:34:09 -07:00

14 lines
11 KiB
JavaScript

/*!@license
* Infragistics.Web.ClientUI igGrid KnockoutJS extension 19.1.20
*
* Copyright (c) 2011-2019 Infragistics Inc.
*
* http://www.infragistics.com/
*
* Depends on:
* jquery-1.9.1.js
* ig.util.js
* ig.dataSource.js
* ig.ui.grid.framework.js
*/
(function(factory){if(typeof define==="function"&&define.amd){define(["../modules/infragistics.ui.grid.framework","knockout"],factory)}else{factory(jQuery,ko)}})(function($,ko){ko.bindingHandlers.igGrid={init:function(element,valueAccessor){var grid=$(element),gridObj=grid.data("igGrid"),binding,options,key;if(grid.attr("data-create")!=="false"){binding=ko.utils.unwrapObservable(valueAccessor());options={};options.dataSource=new $.ig.KnockoutDataSource({dataSource:binding.dataSource});for(key in binding){if(binding.hasOwnProperty(key)&&key!=="dataSource"){options[key]=binding[key]}}}else{binding=options=gridObj.options}grid.igGridKnockoutBridge();if(grid.attr("data-create")!=="false"){grid.igGrid(options)}else{grid.igGridKnockoutBridge("rebindCells")}return{controlsDescendantBindings:true}},update:function(element,valueAccessor){$(element).igGridKnockoutBridge("recordsUpdated",valueAccessor)}};ko.visitPropertiesOrArrayEntries=function(rootObject,visitorCallback){var i,propertyName;if(rootObject instanceof Array){for(i=0;i<rootObject.length;i++){visitorCallback(i)}}else{for(propertyName in rootObject){if(rootObject.hasOwnProperty(propertyName)){visitorCallback(propertyName)}}}};ko.bindingHandlers.igHierarchicalGrid={init:function(element,valueAccessor){var grid=$(element),binding,options,ds,childgridhandler,key;binding=ko.utils.unwrapObservable(valueAccessor());options={};ds=ko.isObservable(binding.dataSource)?binding.dataSource():binding.dataSource;if(ds instanceof $.ig.DataSource){ds=ds.data()}options.dataSource=new $.ig.KnockoutDataSource({dataSource:binding.dataSource});for(key in binding){if(binding.hasOwnProperty(key)&&key!=="dataSource"){options[key]=binding[key]}}grid.igGridKnockoutBridge();grid.igHierarchicalGrid(options);grid.data("observableDataSource",options.dataSource);childgridhandler=function(event,args){var kods,opts=args.options,observableDs;if(args.element.data("igGrid")){return}kods=args.owner.element.data("observableDataSource");observableDs=kods.dataAt(args.id,args.path);opts.dataSource=new $.ig.KnockoutDataSource({primaryKey:opts.primaryKey,dataSource:opts.dataSource,observableDataSource:observableDs,responseDataKey:opts.responseDataKey});args.element.igGridKnockoutBridge();args.element.igGrid(opts);args.element.attr("data-path",args.path);args.element.attr("data-ids",args.id);opts=args.element.data("igGrid").options;args.element.attr("data-create",false);ko.applyBindingsToNode(args.element[0],{igGrid:{}},observableDs)};grid.on("igchildgridcreating","table",childgridhandler);return{controlsDescendantBindings:true}},update:ko.bindingHandlers.igGrid.update};ko.bindingHandlers.igCell={update:function(element,valueAccessor,allBindingsAccessor,viewModel){var cell=$(element),grid=cell.closest(".ui-iggrid-table");if(grid.attr("id").endsWith("_fixed")){grid=grid.closest(".ui-iggrid").find("[data-bind]:first")}grid.igGridKnockoutBridge("cellUpdated",cell,typeof valueAccessor==="function"?valueAccessor():valueAccessor,viewModel!==null&&ko.isObservable(viewModel.$data)?viewModel.$data():viewModel)}};$.widget("ui.igGridKnockoutBridge",{options:{parent:null},_create:function(){if(!this.options.parent){this.options.parent=this.element.data("igGrid")}this._createHandlers();this._bridgeGrid(this.element)},destroy:function(){var handler=this._rebindCellsHandler;this.options.parent.renderNewRow=this._renderNewRowDefault;if(this._grb){this._grb._renderNewRow=this._renderNewRowGroupBy}this.element.unbind({iggridrendering:this._gridRenderingHandler,iggridrendered:this._gridRenderedHandler,iggriddatarendered:handler,iggridvirtualrecordsrender:handler,iggridcolumnscollectionmodified:handler});$.Widget.prototype.destroy.apply(this,arguments);return this},renderNewRow:function(rec,key){this._renderNewRowDefault.apply(this.options.parent,[rec,key]);this._rebindCells(key)},_renderNewRowGroupByKo:function(rec,key){this._renderNewRowGroupBy.apply(this._grb,[rec,key]);this._rebindCells(key)},rebindCells:function(rowId){this._rebindCells(rowId)},_gridRendering:function(evt,ui){if(this.options.parent&&this.options.parent.id()!==ui.owner.id()){return}this.options.parent=ui.owner;if(this.renderNewRow!==ui.owner.renderNewRow){this._renderNewRowDefault=ui.owner.renderNewRow;ui.owner.renderNewRow=$.proxy(this.renderNewRow,this)}},_gridRendered:function(evt,ui){var grb=$(ui.owner.element).data("igGridGroupBy")||$("#"+ui.owner.id()).data("igGridGroupBy");if(grb&&this._renderNewRowGroupByKo!==grb._renderNewRow){this._renderNewRowGroupBy=grb._renderNewRow;grb._renderNewRow=$.proxy(this._renderNewRowGroupByKo,this);this._grb=grb}},_rebindCells:function(rowId){var ds,owner=this.options.parent,gridElement=this.options.parent.element,pageIndex=gridElement.data("igGridPaging")?gridElement.data("igGridPaging").pageIndex():0,pageSize=gridElement.data("igGridPaging")?gridElement.data("igGridPaging").pageSize():0,startRowIndex=pageIndex*pageSize,opts=owner.options,dataIdSelector=$.type(rowId)==="number"||$.type(rowId)==="string"?"[data-id='"+rowId+"']":"",bindingFunction;if(ko.isObservable(owner.dataSource.kods)){ds=owner.dataSource.kods()}else if(ko.isObservable(owner.dataSource.kods[owner.options.responseDataKey])){ds=owner.dataSource.kods[owner.options.responseDataKey]()}else{ds=owner.dataSource.dataSource()}if(!opts.rowVirtualization&&!opts.virtualization){owner._startRowIndex=startRowIndex}if(gridElement.hasClass("ui-iggrid-responsive-vertical")){bindingFunction=this._bindingFunctionResponsiveVerticalColumns(ds,owner,opts)}else{bindingFunction=this._bindingFunctionDefault(ds,owner,opts)}owner.element.children("tbody").children("tr[data-grouprow!=true][data-container!=true]"+dataIdSelector).filter(function(){return this.style.visibility!=="hidden"}).map(bindingFunction)},_bindingFunctionDefault:function(ds,owner,opts){return function(i){var trId=$(this).attr("data-id"),id,k,l,fl,cells,fcells,dataVal,rec,cell;for(i=0;i<ds.length;i++){id=ds[i][owner.options.primaryKey];if(ko.isObservable(id)){id=id()}if(String(id)===String(trId)){rec=ds[i];break}}if(rec){cells=$(this).children("td[data-parent!=true][data-parent!=false][data-skip!=true]");fcells=$(this).closest(".ui-iggrid").find(".ui-iggrid-fixedcontainer tbody>tr[data-id='"+trId+"']").children("td[data-parent!=true][data-parent!=false][data-skip!=true]");l=0;fl=0;for(k=0;k<opts.columns.length;k++){if(opts.columns[k].hidden){continue}if(opts.columns[k].unbound){l++;continue}dataVal=rec[opts.columns[k].key];if(opts.columns[k].fixed){cell=fcells[fl++]}else{cell=cells[l++]}if(ko.isObservable(dataVal)){if(ko.hasOwnProperty("applyBindingAccessorsToNode")){ko.applyBindingAccessorsToNode(cell,{igCell:{value:opts.columns[k]}},{$data:dataVal})}else{ko.applyBindingsToNode(cell,{igCell:{value:opts.columns[k]}},dataVal)}}}}else{$(this).remove()}}},_bindingFunctionResponsiveVerticalColumns:function(ds,owner,opts){return function(i){var trId=$(this).attr("data-id"),id,k,cells,dataVal,rec,cell,colkey;for(i=0;i<ds.length;i++){id=ds[i][owner.options.primaryKey];if(ko.isObservable(id)){id=id()}if(String(id)===String(trId)){rec=ds[i];break}}if(rec){cells=$(this).children("td[data-parent!=true][data-parent!=false][data-skip!=true]");for(k=0;k<opts.columns.length;k++){colkey=$(this).data("col-key");if(opts.columns[k].key!==colkey){continue}dataVal=rec[colkey];if(ko.isObservable(dataVal)){cell=cells[1];if(ko.hasOwnProperty("applyBindingAccessorsToNode")){ko.applyBindingAccessorsToNode(cell,{igCell:{value:opts.columns[k]}},{$data:dataVal})}else{ko.applyBindingsToNode(cell,{igCell:{value:opts.columns[k]}},dataVal)}break}}}else{$(this).remove()}}},recordsUpdated:function(valueAccessor){var grid=this.options.parent,newds,oldds,updating=$(this.options.parent.element).data("igGridUpdating")||$("#"+grid.id()).data("igGridUpdating"),i,key,changes,c,autoCommit=grid.options.autoCommit;newds=valueAccessor().dataSource;if(!newds){if(grid.options.responseDataKey){newds=grid.dataSource.kods[grid.options.responseDataKey]}else{newds=grid.dataSource.kods}}else{grid.dataSource.kods=newds}oldds=grid.dataSource._knockoutState;grid.dataSource._knockoutState=$.extend([],newds());if(!oldds){return}if(updating===null||updating===undefined){throw new Error("two-way adding and deleting of rows with "+"KnockoutJS requires the Updating feature to be defined")}if(typeof newds==="function"&&!grid.dataSource._ownUpdate){changes=ko.utils.compareArrays(oldds,newds());for(i=0;i<changes.length;i++){c=changes[i];if(c.status==="deleted"){grid.dataSource._koUpdate=true;key=c.value[grid.options.primaryKey];key=ko.isObservable(key)?key():key;grid.options.autoCommit=true;updating.deleteRow(key);grid.options.autoCommit=autoCommit}}for(i=0;i<changes.length;i++){c=changes[i];if(c.status==="added"){key=c.value[grid.options.primaryKey];key=ko.isObservable(key)?key():key;grid.dataSource._koUpdate=true;grid.options.autoCommit=true;updating.addRow(ko.toJS(c.value));grid.options.autoCommit=autoCommit}}}grid.dataSource._ownUpdate=false},cellUpdated:function(cell,valueAccessor,viewModel){var grid=this.options.parent,key,tmpldata,newFormattedVal,tr,rec,keyVal,col,editor,curCol,origDs,origRec;key=valueAccessor.value.key;tr=cell.closest("tr");col=grid.columnByKey(grid.options.primaryKey);if(tr.attr("data-id")!==null&&tr.attr("data-id")!==undefined){if(col.dataType==="number"||col.dataType===undefined){keyVal=parseInt(tr.attr("data-id"),10);if(key===grid.options.primaryKey){viewModel=parseInt(viewModel,10)}}else{keyVal=tr.attr("data-id")}origDs=grid.dataSource.settings.localSchemaTransform?grid.dataSource._origDs:null;rec=grid.dataSource.findRecordByKey(keyVal);if(origDs&&origDs!==grid.dataSource._data){origRec=grid.dataSource.findRecordByKey(keyVal,origDs)}if(rec===null){rec=grid.dataSource.findRecordByKey(viewModel);if(origDs&&origDs!==grid.dataSource._data){origRec=grid.dataSource.findRecordByKey(keyVal,origDs)}}if(key===grid.options.primaryKey){tr.attr("data-id",viewModel);tr.data().id=viewModel}rec[key]=viewModel;if(origRec){origRec[key]=viewModel}}else{throw new Error("Updating the data source requires a primary key to be defined")}curCol=grid.columnByKey(key);if(curCol&&curCol.template&&curCol.template.length>0){tmpldata=origRec||{};if(!tmpldata.hasOwnProperty(key)){tmpldata[key]=viewModel}newFormattedVal=grid._renderTemplatedCell(tmpldata,valueAccessor.value);if(newFormattedVal&&newFormattedVal.startsWith("<td")){newFormattedVal=$(newFormattedVal).html()}}else{newFormattedVal=grid._renderCell(viewModel,valueAccessor.value,rec)}editor=cell.children(".ui-iggrid-editor").detach();cell.html(newFormattedVal);if(editor.length>0){cell.prepend(editor)}grid.dataSource._ownUpdate=false},_bridgeGrid:function(grid){var handler=this._rebindCellsHandler;grid.bind({iggridrendering:this._gridRenderingHandler,iggridrendered:this._gridRenderedHandler,iggriddatarendered:handler,iggridvirtualrecordsrender:handler,iggridcolumnscollectionmodified:handler,iggridcolumnfixingcolumnfixed:handler})},_createHandlers:function(){this._rebindCellsHandler=$.proxy(this._rebindCells,this);this._gridRenderingHandler=$.proxy(this._gridRendering,this);this._gridRenderedHandler=$.proxy(this._gridRendered,this)}})});