jQuery.noConflict(); (function ( win, $ ) { 'use strict'; $.widget( "custom.SPTableEditable", { options: { struct: [ { title: 'Title', editable: true, cssClass: 'table-title', input_type: 'text', alias: 'title' },{ title: 'Expression', editable: true, cssClass: 'table-expr', input_type: 'text', alias: 'field' } ], theadClass: 'table-th', tbodyClass: 'table-tbody', trClass: 'table-tr', rowSelectedClass: 'row-selected', sortable: false, sortHandle: null, onSelectRow: null }, _create: function() { this.element.addClass( 'table' ); this.thead = this._createTitle() this.tbody = this._createTBody(); // if( this.options.data.length ){ // if( !(this.options.struct.length == this.options.data[0].length) ) // alert( "data and struct are incompatible" ); // else // this._createRows(); // } this.rowSelected = null; this.rowSelectedIdx = -1; this.lastRowIdx = -1; }, // _refresh: function() {}, // _destroy: function() {}, // _setOptions: function() { // _super and _superApply handle keeping the right this-context // this._superApply( arguments ); // this._refresh(); // }, // _setOption is called for each individual option that is changing _setOption: function( key, value ) { if ( /data/.test(key) && value.length ) { if( value[0].length != this.options.struct.length ) { alert( "data and struct are incompatible" ); } else { this._super( key, value ); this._createRows(); } } else { this._super( key, value ); } }, _createTitle: function( ) { var thead = $( '
' , { 'class': this.options.theadClass +" thead" }).appendTo(this.element); var title_row = $( '
', { 'class': "row" }).appendTo( thead ); [].forEach.call( this.options.struct, function( item/*, idx */) { $('' , { 'class': item.cssClass + " cell", 'html': item.title }).appendTo(title_row) }.bind( this )); return thead; }, _createTBody: function() { var tbody = $( '
', { 'class': this.options.tbodyClass + ' tbody' }).css( 'height', this.element.height() - this.thead.height() ) .appendTo(this.element) if( this.options.sortable ) { tbody.sortable({ 'axis': 'y', 'handle': this.options.sortHandle }); } return tbody; }, _createRows: function() { [].forEach.call( this.options.data, this._addRow.bind( this )); }, _addRow: function( data, idx ) { if( typeof(idx) != 'number' ){ idx = this.lastRowIdx +1; } var row = $( '
', { 'class': this.options.trClass + ' row', 'rowId': idx })//.on('click', this._selectRow.bind(this) ) .appendTo(this.tbody); // If its last argument is true the event handler is set for the capturing phase, if it is false the event handler is set for the bubbling phase. // row[0].addEventListener( 'click', this._selectRow.bind(this), true); // row[0].addEventListener( 'click', function(_this){ // return function(event){ // _this._selectRow(event); // } // }(this), true) var _this = this; [].forEach.call( this.options.struct, function( item_struct, iidx ) { /* Manage input type*/ var html = ""; switch( item_struct.input_type ){ case 'text': case 'number': html= $( '', { type: item_struct.input_type, value: data[iidx], alias: item_struct.alias }).on( 'blur', _this._valueChange.bind(_this)); break; case 'checkbox': var opt = { type: "checkbox", alias: item_struct.alias } if( data[iidx] && /(checked|true)/ig.test( data[iidx].toString() ) ) opt.checked = "checked"; html= $( '' , opt ).on( 'click', _this._valueChange.bind(_this)); break; case 'select': html = $( '