Browse Source

re-init widget on refresh & replace

pull/4801/head
Halil İbrahim Kalkan 6 years ago
parent
commit
2bc29b2a19
  1. 65
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/widget-manager.js

65
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/widget-manager.js

@ -25,11 +25,16 @@
opts.filterForm = $(opts.filterForm); opts.filterForm = $(opts.filterForm);
} }
var getFilters = function ($widgetWrapperDiv) { var publicApi = {
init: init,
refresh: refresh
};
function getFilters($widget) {
var filters = {}; var filters = {};
if (opts.filterForm) { if (opts.filterForm) {
opts.filterForm.each(function() { opts.filterForm.each(function () {
filters = $.extend(filters, opts.filterForm.serializeFormToObject()); filters = $.extend(filters, opts.filterForm.serializeFormToObject());
}); });
} }
@ -38,55 +43,66 @@
filters = $.extend(filters, opts.filterCallback()); filters = $.extend(filters, opts.filterCallback());
} }
var widgetApi = $widgetWrapperDiv.data('abp-widget-api'); var widgetApi = $widget.data('abp-widget-api');
if (widgetApi && widgetApi.getFilters) { if (widgetApi && widgetApi.getFilters) {
filters = $.extend(filters, widgetApi.getFilters()); filters = $.extend(filters, widgetApi.getFilters());
} }
return filters; return filters;
}; }
var init = function () { function init($widgets) {
opts.wrapper.findWithSelf('.abp-widget-wrapper').each(function () { if(!$widgets){
var $widgetWrapperDiv = $(this); $widgets = opts.wrapper.findWithSelf('.abp-widget-wrapper');
var widgetName = $widgetWrapperDiv.attr('data-widget-name'); }
$widgets.each(function () {
var $widget = $(this);
$widget.data('abp-widget-manager', publicApi);
var widgetName = $widget.attr('data-widget-name');
var widgetApiClass = abp.widgets[widgetName]; var widgetApiClass = abp.widgets[widgetName];
if (widgetApiClass) { if (widgetApiClass) {
var widgetApi = new widgetApiClass($widgetWrapperDiv); var widgetApi = new widgetApiClass($widget);
$widgetWrapperDiv.data('abp-widget-api', widgetApi); $widget.data('abp-widget-api', widgetApi);
if (widgetApi.init) { if (widgetApi.init) {
widgetApi.init(getFilters($widgetWrapperDiv)); widgetApi.init(getFilters($widget));
} }
} }
}); });
}; }
function refresh($widgets) {
if(!$widgets){
$widgets = opts.wrapper.findWithSelf('.abp-widget-wrapper');
}
var refresh = function () { $widgets.each(function () {
opts.wrapper.findWithSelf('.abp-widget-wrapper').each(function () { var $widget = $(this);
var $widgetWrapperDiv = $(this);
var refreshUrl = $widgetWrapperDiv.attr('data-refresh-url'); var refreshUrl = $widget.attr('data-refresh-url');
if (refreshUrl) { if (refreshUrl) {
abp.ajax({ abp.ajax({
url: refreshUrl, url: refreshUrl,
type: 'GET', type: 'GET',
dataType: 'html', dataType: 'html',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8', contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: getFilters($widgetWrapperDiv) data: getFilters($widget)
}).then(function (result) { }).then(function (result) {
$widgetWrapperDiv.replaceWith($(result)); var $result = $(result);
$widget.replaceWith($result);
init($result);
}); });
} else { } else {
var widgetApi = $widgetWrapperDiv.data('abp-widget-api'); var widgetApi = $widget.data('abp-widget-api');
if (widgetApi && widgetApi.refresh) { if (widgetApi && widgetApi.refresh) {
widgetApi.refresh(getFilters($widgetWrapperDiv)); widgetApi.refresh(getFilters($widget));
} }
} }
}); });
}; }
if (opts.filterForm) { if (opts.filterForm) {
opts.filterForm.each(function() { opts.filterForm.each(function () {
$(this).submit(function (e) { $(this).submit(function (e) {
e.preventDefault(); e.preventDefault();
refresh(); refresh();
@ -94,11 +110,6 @@
}); });
} }
var publicApi = {
init: init,
refresh: refresh
};
opts.wrapper.data('abp-widget-manager', publicApi); opts.wrapper.data('abp-widget-manager', publicApi);
return publicApi; return publicApi;

Loading…
Cancel
Save