FileUpload now triggers dependsOn event

form.beforeRefresh event now uses dataholder pattern
Improve exception handling in ControllerBehavior
This commit is contained in:
Samuel Georges 2015-07-18 08:50:31 +10:00
parent e3ba89ba74
commit 846feb6a33
5 changed files with 85 additions and 34 deletions

View File

@ -125,8 +125,9 @@ class FormController extends ControllerBehavior
$this->controller->formExtendFields($this->formWidget, $fields); $this->controller->formExtendFields($this->formWidget, $fields);
}); });
$this->formWidget->bindEvent('form.beforeRefresh', function ($saveData) { $this->formWidget->bindEvent('form.beforeRefresh', function ($holder) {
return $this->controller->formExtendRefreshData($this->formWidget, $saveData); $result = $this->controller->formExtendRefreshData($this->formWidget, $holder->data);
if (is_array($result)) $holder->data = $result;
}); });
$this->formWidget->bindEvent('form.refreshFields', function ($fields) { $this->formWidget->bindEvent('form.refreshFields', function ($fields) {

View File

@ -103,7 +103,7 @@ class ControllerBehavior extends ExtensionBase
* Loop the remaining key parts and build a result * Loop the remaining key parts and build a result
*/ */
foreach ($keyParts as $key) { foreach ($keyParts as $key) {
if (!array_key_exists($key, $result)) { if (!is_array($result) || !array_key_exists($key, $result)) {
return $default; return $default;
} }

View File

@ -164,6 +164,11 @@
$('.upload-remove-button', $preview).data('request-data', { file_id: response.id }) $('.upload-remove-button', $preview).data('request-data', { file_id: response.id })
$img.attr('src', response.thumb) $img.attr('src', response.thumb)
} }
/*
* Trigger change event (Compatability with october.form.js)
*/
this.$el.closest('[data-field-name]').trigger('change.oc.formwidget')
} }
FileUpload.prototype.onUploadError = function(file, error) { FileUpload.prototype.onUploadError = function(file, error) {

View File

@ -287,7 +287,7 @@ class Form extends WidgetBase
*/ */
public function setFormValues($data = null) public function setFormValues($data = null)
{ {
if ($data == null) { if ($data === null) {
$data = $this->getSaveData(); $data = $this->getSaveData();
} }
@ -312,12 +312,10 @@ class Form extends WidgetBase
/* /*
* Extensibility * Extensibility
*/ */
$eventResults = $this->fireEvent('form.beforeRefresh', [$saveData]) + $dataHolder = (object) ['data' => $saveData];
Event::fire('backend.form.beforeRefresh', [$this, $saveData]); $this->fireEvent('form.beforeRefresh', [$dataHolder]);
Event::fire('backend.form.beforeRefresh', [$this, $dataHolder]);
foreach ($eventResults as $eventResult) { $saveData = $dataHolder->data;
$saveData = $eventResult + $saveData;
}
/* /*
* Set the form variables and prepare the widget * Set the form variables and prepare the widget
@ -356,8 +354,10 @@ class Form extends WidgetBase
/* /*
* Extensibility * Extensibility
*/ */
$eventResults = $this->fireEvent('form.refresh', [$result]) + $eventResults = array_merge(
Event::fire('backend.form.refresh', [$this, $result]); $this->fireEvent('form.refresh', [$result]),
Event::fire('backend.form.refresh', [$this, $result])
);
foreach ($eventResults as $eventResult) { foreach ($eventResults as $eventResult) {
$result = $eventResult + $result; $result = $eventResult + $result;

View File

@ -5,22 +5,47 @@
* - Nil * - Nil
*/ */
+function ($) { "use strict"; +function ($) { "use strict";
var Base = $.oc.foundation.base,
BaseProto = Base.prototype
var FormWidget = function (element, options) { var FormWidget = function (element, options) {
this.$el = $(element)
this.options = options || {}
var $el = this.$el = $(element); /*
* Throttle dependency updating
*/
this.dependantUpdateInterval = 300
this.dependantUpdateTimers = {}
this.$form = $el.closest('form') $.oc.foundation.controlUtils.markDisposable(element)
this.options = options || {}; Base.call(this)
this.init()
}
FormWidget.prototype = Object.create(BaseProto)
FormWidget.prototype.constructor = FormWidget
FormWidget.prototype.init = function() {
this.$form = this.$el.closest('form')
this.bindDependants() this.bindDependants()
this.bindCheckboxlist() this.bindCheckboxlist()
this.toggleEmptyTabs() this.toggleEmptyTabs()
this.bindCollapsibleSections() this.bindCollapsibleSections()
this.$el.one('dispose-control', this.proxy(this.dispose))
} }
FormWidget.DEFAULTS = { FormWidget.prototype.dispose = function() {
refreshHandler: null this.$el.off('dispose-control', this.proxy(this.dispose))
this.$el.removeData('oc.formwidget')
this.$el = null
this.options = null
BaseProto.dispose.call(this)
} }
/* /*
@ -51,13 +76,12 @@
FormWidget.prototype.bindDependants = function() { FormWidget.prototype.bindDependants = function() {
var self = this, var self = this,
form = this.$el, form = this.$el,
formEl = this.$form,
fieldMap = {} fieldMap = {}
/* /*
* Map master and slave field map * Map master and slave field map
*/ */
form.find('[data-field-depends]').each(function(){ form.find('[data-field-depends]').each(function() {
var name = $(this).data('field-name'), var name = $(this).data('field-name'),
depends = $(this).data('field-depends') depends = $(this).data('field-depends')
@ -73,20 +97,37 @@
* When a master is updated, refresh its slaves * When a master is updated, refresh its slaves
*/ */
$.each(fieldMap, function(fieldName, toRefresh){ $.each(fieldMap, function(fieldName, toRefresh){
form.find('[data-field-name="'+fieldName+'"]') form
.on('change', 'select, input', function(){ .find('[data-field-name="'+fieldName+'"]')
formEl.request(self.options.refreshHandler, { .on('change.oc.formwidget', $.proxy(self.onRefreshDependants, self, fieldName, toRefresh))
data: toRefresh })
}).success(function(){ }
self.toggleEmptyTabs()
})
$.each(toRefresh.fields, function(index, field){ /*
form.find('[data-field-name="'+field+'"]:visible') * Refresh a dependancy field
.addClass('loading-indicator-container size-form-field') * Uses a throttle to prevent duplicate calls and click spamming
.loadIndicator() */
}) FormWidget.prototype.onRefreshDependants = function(fieldName, toRefresh) {
}) var self = this,
form = this.$el,
formEl = this.$form
if (this.dependantUpdateTimers[fieldName] !== undefined) {
window.clearTimeout(this.dependantUpdateTimers[fieldName])
}
this.dependantUpdateTimers[fieldName] = window.setTimeout(function() {
formEl.request(self.options.refreshHandler, {
data: toRefresh
}).success(function() {
self.toggleEmptyTabs()
})
}, this.dependantUpdateInterval)
$.each(toRefresh.fields, function(index, field) {
form.find('[data-field-name="'+field+'"]:visible')
.addClass('loading-indicator-container size-form-field')
.loadIndicator()
}) })
} }
@ -99,7 +140,7 @@
if (!tabControl.length) if (!tabControl.length)
return return
$('.tab-pane', tabControl).each(function(){ $('.tab-pane', tabControl).each(function() {
$('[data-target="#' + $(this).attr('id') + '"]', tabControl) $('[data-target="#' + $(this).attr('id') + '"]', tabControl)
.toggle(!!$('.form-group:not(:empty)', $(this)).length) .toggle(!!$('.form-group:not(:empty)', $(this)).length)
}) })
@ -123,6 +164,10 @@
.nextUntil('.section-field').hide() .nextUntil('.section-field').hide()
} }
FormWidget.DEFAULTS = {
refreshHandler: null
}
// FORM WIDGET PLUGIN DEFINITION // FORM WIDGET PLUGIN DEFINITION
// ============================ // ============================
@ -157,7 +202,7 @@
// FORM WIDGET DATA-API // FORM WIDGET DATA-API
// ============== // ==============
$(document).render(function(){ $(document).render(function() {
$('[data-control="formwidget"]').formWidget(); $('[data-control="formwidget"]').formWidget();
}) })