From 18194fa0f9d3cdcd013b86d68e3c4ad9bd118a8e Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 20 Feb 2016 12:45:36 +1100 Subject: [PATCH] Complete Table server data source Recompile assets --- modules/backend/widgets/Table.php | 35 ++++++++++ .../widgets/table/ServerEventDataSource.php | 29 +++++++- .../widgets/table/assets/js/build-min.js | 11 +-- .../assets/js/table.datasource.server.js | 70 ++++++++----------- .../backend/widgets/table/assets/js/table.js | 2 +- modules/system/assets/ui/storm-min.js | 7 +- 6 files changed, 105 insertions(+), 49 deletions(-) diff --git a/modules/backend/widgets/Table.php b/modules/backend/widgets/Table.php index 0eea00410..aa882cdba 100644 --- a/modules/backend/widgets/Table.php +++ b/modules/backend/widgets/Table.php @@ -198,6 +198,41 @@ class Table extends WidgetBase ]; } + public function onServerCreateRecord() + { + if ($this->isClientDataSource()) { + throw new SystemException('The Table widget is not configured to use the server data source.'); + } + + $this->dataSource->createRecord( + post('recordData'), + post('placement'), + post('relativeToKey') + ); + + return $this->onServerGetRecords(); + } + + public function onServerUpdateRecord() + { + if ($this->isClientDataSource()) { + throw new SystemException('The Table widget is not configured to use the server data source.'); + } + + $this->dataSource->updateRecord(post('key'), post('recordData')); + } + + public function onServerDeleteRecord() + { + if ($this->isClientDataSource()) { + throw new SystemException('The Table widget is not configured to use the server data source.'); + } + + $this->dataSource->deleteRecord(post('key')); + + return $this->onServerGetRecords(); + } + public function onGetDropdownOptions() { $columnName = Input::get('column'); diff --git a/modules/backend/widgets/table/ServerEventDataSource.php b/modules/backend/widgets/table/ServerEventDataSource.php index 1c4325f9f..91862cbd5 100644 --- a/modules/backend/widgets/table/ServerEventDataSource.php +++ b/modules/backend/widgets/table/ServerEventDataSource.php @@ -11,7 +11,7 @@ class ServerEventDataSource extends DataSourceBase * Return records from the data source. * @param integer $offset Specifies the offset of the first record to return, zero-based. * @param integer $count Specifies the number of records to return. - * @return array Returns the records. + * @return array Returns the records. * If there are no more records, returns an empty array. */ public function getRecords($offset, $count) @@ -28,6 +28,33 @@ class ServerEventDataSource extends DataSourceBase return $this->fireEvent('data.getCount', [], true); } + /** + * Updates a record in the data source. + * @return void + */ + public function createRecord($data, $placement, $relativeToKey) + { + return $this->fireEvent('data.createRecord', [$data, $placement, $relativeToKey]); + } + + /** + * Updates a record in the data source. + * @return void + */ + public function updateRecord($key, $data) + { + $this->fireEvent('data.updateRecord', [$key, $data]); + } + + /** + * Removes a record from the data source. + * @return array Returns the remaining records. + */ + public function deleteRecord($key) + { + return $this->fireEvent('data.deleteRecord', [$key], true); + } + /** * Initializes records in the data source. * The method doesn't replace existing records and diff --git a/modules/backend/widgets/table/assets/js/build-min.js b/modules/backend/widgets/table/assets/js/build-min.js index c76219001..a14197e9a 100644 --- a/modules/backend/widgets/table/assets/js/build-min.js +++ b/modules/backend/widgets/table/assets/js/build-min.js @@ -614,10 +614,13 @@ Server.prototype.constructor=Server Server.prototype.dispose=function(){BaseProto.dispose.call(this) this.data=null} Server.prototype.getRecords=function(offset,count,onSuccess){var handlerName=this.tableObj.getAlias()+'::onServerGetRecords' -$.request(handlerName,{data:{offset:offset,count:count}}).done(function(data){onSuccess(data.records,data.count)})} -Server.prototype.createRecord=function(recordData,placement,relativeToKey,offset,count,onSuccess){console.log('createRecord')} -Server.prototype.updateRecord=function(key,recordData){console.log('updateRecord')} -Server.prototype.deleteRecord=function(key,newRecordData,offset,count,onSuccess){console.log('deleteRecord')} +this.tableObj.$el.request(handlerName,{data:{offset:offset,count:count}}).done(function(data){onSuccess(data.records,data.count)})} +Server.prototype.createRecord=function(recordData,placement,relativeToKey,offset,count,onSuccess){var handlerName=this.tableObj.getAlias()+'::onServerCreateRecord' +this.tableObj.$el.request(handlerName,{data:{recordData:recordData,placement:placement,relativeToKey:relativeToKey,offset:offset,count:count}}).done(function(data){onSuccess(data.records,data.count)})} +Server.prototype.updateRecord=function(key,recordData){var handlerName=this.tableObj.getAlias()+'::onServerUpdateRecord' +this.tableObj.$el.request(handlerName,{data:{key:key,recordData:recordData}})} +Server.prototype.deleteRecord=function(key,newRecordData,offset,count,onSuccess){var handlerName=this.tableObj.getAlias()+'::onServerDeleteRecord' +this.tableObj.$el.request(handlerName,{data:{key:key,offset:offset,count:count}}).done(function(data){onSuccess(data.records,data.count)})} $.oc.table.datasource.server=Server}(window.jQuery);+function($){"use strict";if($.oc.table===undefined) throw new Error("The $.oc.table namespace is not defined. Make sure that the table.js script is loaded.");if($.oc.table.processor===undefined) $.oc.table.processor={} diff --git a/modules/backend/widgets/table/assets/js/table.datasource.server.js b/modules/backend/widgets/table/assets/js/table.datasource.server.js index b02ff893a..860e36012 100644 --- a/modules/backend/widgets/table/assets/js/table.datasource.server.js +++ b/modules/backend/widgets/table/assets/js/table.datasource.server.js @@ -47,7 +47,7 @@ */ Server.prototype.getRecords = function(offset, count, onSuccess) { var handlerName = this.tableObj.getAlias()+'::onServerGetRecords' - $.request(handlerName, { + this.tableObj.$el.request(handlerName, { data: { offset: offset, count: count @@ -71,21 +71,18 @@ * The onSuccess callback parameters: records, totalCount. */ Server.prototype.createRecord = function(recordData, placement, relativeToKey, offset, count, onSuccess) { - console.log('createRecord') - // if (placement === 'bottom') { - // // Add record to the bottom of the dataset - // this.data.push(recordData) - // } - // else if (placement == 'above' || placement == 'below') { - // // Add record above or below the passed record key - // var recordIndex = this.getIndexOfKey(relativeToKey) - // if (placement == 'below') - // recordIndex ++ - - // this.data.splice(recordIndex, 0, recordData) - // } - - // this.getRecords(offset, count, onSuccess) + var handlerName = this.tableObj.getAlias()+'::onServerCreateRecord' + this.tableObj.$el.request(handlerName, { + data: { + recordData: recordData, + placement: placement, + relativeToKey: relativeToKey, + offset: offset, + count: count + } + }).done(function(data) { + onSuccess(data.records, data.count) + }) } /* @@ -95,16 +92,13 @@ * - recordData - the record fields. */ Server.prototype.updateRecord = function(key, recordData) { - console.log('updateRecord') - // var recordIndex = this.getIndexOfKey(key) - - // if (recordIndex !== -1) { - // recordData[this.tableObj.options.keyColumn] = key - // this.data[recordIndex] = recordData - // } - // else { - // throw new Error('Record with they key '+key+ ' is not found in the data set') - // } + var handlerName = this.tableObj.getAlias()+'::onServerUpdateRecord' + this.tableObj.$el.request(handlerName, { + data: { + key: key, + recordData: recordData + } + }) } /* @@ -120,20 +114,16 @@ * The onSuccess callback parameters: records, totalCount. */ Server.prototype.deleteRecord = function(key, newRecordData, offset, count, onSuccess) { - console.log('deleteRecord') - // var recordIndex = this.getIndexOfKey(key) - - // if (recordIndex !== -1) { - // this.data.splice(recordIndex, 1) - - // if (this.data.length == 0) - // this.data.push(newRecordData) - - // this.getRecords(offset, count, onSuccess) - // } - // else { - // throw new Error('Record with they key '+key+ ' is not found in the data set') - // } + var handlerName = this.tableObj.getAlias()+'::onServerDeleteRecord' + this.tableObj.$el.request(handlerName, { + data: { + key: key, + offset: offset, + count: count + } + }).done(function(data) { + onSuccess(data.records, data.count) + }) } $.oc.table.datasource.server = Server diff --git a/modules/backend/widgets/table/assets/js/table.js b/modules/backend/widgets/table/assets/js/table.js index 447ee0d61..b0aef7a96 100644 --- a/modules/backend/widgets/table/assets/js/table.js +++ b/modules/backend/widgets/table/assets/js/table.js @@ -526,7 +526,7 @@ this.elementAddClass(this.activeCell, 'active') } - // If the cell belongs to other row than the currently edited, + // If the cell belongs to other row than the currently edited, // commit currently edited row to the data source. Update the // currently edited row key. var rowKey = this.getCellRowKey(cellElement) diff --git a/modules/system/assets/ui/storm-min.js b/modules/system/assets/ui/storm-min.js index 65fbea75f..f03f2a03f 100644 --- a/modules/system/assets/ui/storm-min.js +++ b/modules/system/assets/ui/storm-min.js @@ -2260,10 +2260,11 @@ $(window).off('.cursorLoadIndicator');}} $(document).ready(function(){$.oc.cursorLoadIndicator=new CursorLoadIndicator();}) $(document).on('ajaxPromise','[data-cursor-load-indicator]',function(){$.oc.cursorLoadIndicator.show()}).on('ajaxFail ajaxDone','[data-cursor-load-indicator]',function(){$.oc.cursorLoadIndicator.hide()})}(window.jQuery);+function($){"use strict";if($.oc===undefined) $.oc={} -var StripeLoadIndicator=function(){this.counter=0 +var StripeLoadIndicator=function(){var self=this +this.counter=0 this.indicator=$('
').addClass('stripe-loading-indicator loaded').append($('
').addClass('stripe')).append($('
').addClass('stripe-loaded')) this.stripe=this.indicator.find('.stripe') -$(document.body).append(this.indicator)} +$(document).ready(function(){$(document.body).append(self.indicator)})} StripeLoadIndicator.prototype.show=function(){this.counter++ this.stripe.after(this.stripe=this.stripe.clone()).remove() if(this.counter>1) @@ -2275,7 +2276,7 @@ if(force!==undefined&&force) this.counter=0 if(this.counter<=0){this.indicator.addClass('loaded') $(document.body).removeClass('loading')}} -$(document).ready(function(){$.oc.stripeLoadIndicator=new StripeLoadIndicator()}) +$.oc.stripeLoadIndicator=new StripeLoadIndicator() $(document).on('ajaxPromise','[data-stripe-load-indicator]',function(event){event.stopPropagation() $.oc.stripeLoadIndicator.show() var $el=$(this)