Skip to content

Commit

Permalink
Fix table pagination and widget reloading
Browse files Browse the repository at this point in the history
Changes:
- Table: Fixed unscoped pagination event handler (which would reload all widgets)
- Widget: Fixed propagation of new widget ID (missing from `opts` and `widget_id`)
- Widget: Added `add_opts(ident, val)` method
  • Loading branch information
mcaskill committed Oct 5, 2017
1 parent 9eac32c commit a95eebc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
27 changes: 23 additions & 4 deletions assets/dist/scripts/charcoal.admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ Charcoal.Admin.Widget = function (opts) {
if (typeof opts.id === 'string') {
this.set_element($('#' + opts.id));
this.set_id(opts.id);
this.widget_id = opts.widget_id || opts.id;
}

if (typeof opts.type === 'string') {
Expand All @@ -1363,13 +1364,27 @@ Charcoal.Admin.Widget.prototype.set_opts = function (opts) {
return this;
};

/**
* Add option
* @param {String} ident
* @param {Mixed} val
* @return this (chainable)
*/
Charcoal.Admin.Widget.prototype.add_opts = function (ident, val) {
if (typeof ident === 'string') {
this._opts[ident] = val;
}

return this;
};

/**
* If a ident is specified, the method tries to return
* the options pointed out.
* If no ident is specified, the method returns
* the whole opts object
*
* @param {String} ident | falcultative
* @param {String} [ident]
* @return {Object|Mixed|false}
*/
Charcoal.Admin.Widget.prototype.opts = function (ident) {
Expand Down Expand Up @@ -1497,7 +1512,11 @@ Charcoal.Admin.Widget.prototype.reload = function (callback) {
contentType: 'application/json',
success: function (response) {
if (typeof response.widget_id === 'string') {
that.set_id(response.widget_id);
var wid = response.widget_id;
that.set_id(wid);
that.add_opts('id', wid);
that.add_opts('widget_id', wid);
that.widget_id = wid;
that.anim_out(function () {
that.element().replaceWith(response.widget_html);
that.set_element($('#' + that.id()));
Expand Down Expand Up @@ -3474,8 +3493,8 @@ Charcoal.Admin.Widget_Table.prototype.bind_events = function ()
}
}).disableSelection();

$('.js-page-switch').on('click', function (e) {
e.preventDefault();
$('.js-page-switch', that.table_selector).on('click', function (event) {
event.preventDefault();

var $this = $(this);
var page_num = $this.data('page-num');
Expand Down
10 changes: 5 additions & 5 deletions assets/dist/scripts/charcoal.admin.min.js

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions assets/src/scripts/charcoal/admin/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Charcoal.Admin.Widget = function (opts) {
if (typeof opts.id === 'string') {
this.set_element($('#' + opts.id));
this.set_id(opts.id);
this.widget_id = opts.widget_id || opts.id;
}

if (typeof opts.type === 'string') {
Expand All @@ -60,13 +61,27 @@ Charcoal.Admin.Widget.prototype.set_opts = function (opts) {
return this;
};

/**
* Add option
* @param {String} ident
* @param {Mixed} val
* @return this (chainable)
*/
Charcoal.Admin.Widget.prototype.add_opts = function (ident, val) {
if (typeof ident === 'string') {
this._opts[ident] = val;
}

return this;
};

/**
* If a ident is specified, the method tries to return
* the options pointed out.
* If no ident is specified, the method returns
* the whole opts object
*
* @param {String} ident | falcultative
* @param {String} [ident]
* @return {Object|Mixed|false}
*/
Charcoal.Admin.Widget.prototype.opts = function (ident) {
Expand Down Expand Up @@ -194,7 +209,11 @@ Charcoal.Admin.Widget.prototype.reload = function (callback) {
contentType: 'application/json',
success: function (response) {
if (typeof response.widget_id === 'string') {
that.set_id(response.widget_id);
var wid = response.widget_id;
that.set_id(wid);
that.add_opts('id', wid);
that.add_opts('widget_id', wid);
that.widget_id = wid;
that.anim_out(function () {
that.element().replaceWith(response.widget_html);
that.set_element($('#' + that.id()));
Expand Down
4 changes: 2 additions & 2 deletions assets/src/scripts/charcoal/admin/widget/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ Charcoal.Admin.Widget_Table.prototype.bind_events = function ()
}
}).disableSelection();

$('.js-page-switch').on('click', function (e) {
e.preventDefault();
$('.js-page-switch', that.table_selector).on('click', function (event) {
event.preventDefault();

var $this = $(this);
var page_num = $this.data('page-num');
Expand Down

0 comments on commit a95eebc

Please sign in to comment.