Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modal): dynamic modal content #1774

Merged
merged 5 commits into from
Nov 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 117 additions & 6 deletions src/definitions/modules/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ $.fn.modal = function(parameters) {
selector = settings.selector,
className = settings.className,
namespace = settings.namespace,
fields = settings.fields,
error = settings.error,

eventNamespace = '.' + namespace,
Expand All @@ -74,7 +75,7 @@ $.fn.modal = function(parameters) {
$dimmer,

element = this,
instance = $module.data(moduleNamespace),
instance = $module.hasClass('modal') ? $module.data(moduleNamespace) : undefined,

ignoreRepeatedEvents = false,

Expand All @@ -91,6 +92,41 @@ $.fn.modal = function(parameters) {
module = {

initialize: function() {
if(!$module.hasClass('modal')) {
module.create.modal();
if(!$.isFunction(settings.onHidden)) {
settings.onHidden = function () {
module.destroy();
$module.remove();
};
}
}
$module.addClass(settings.class);
if (settings.title !== '') {
$module.find(selector.title).html(module.helpers.escape(settings.title, settings.preserveHTML)).addClass(settings.classTitle);
}
if (settings.content !== '') {
$module.find(selector.content).html(module.helpers.escape(settings.content, settings.preserveHTML)).addClass(settings.classContent);
}
if(module.has.configActions()){
var $actions = $module.find(selector.actions).addClass(settings.classActions);
settings.actions.forEach(function (el) {
var icon = el[fields.icon] ? '<i class="' + module.helpers.deQuote(el[fields.icon]) + ' icon"></i>' : '',
text = module.helpers.escape(el[fields.text] || '', settings.preserveHTML),
cls = module.helpers.deQuote(el[fields.class] || ''),
click = el[fields.click] && $.isFunction(el[fields.click]) ? el[fields.click] : function () {};
$actions.append($('<button/>', {
html: icon + text,
class: className.button + ' ' + cls,
click: function () {
if (click.call(element, $module) === false) {
return;
}
module.hide();
}
}));
});
}
module.cache = {};
module.verbose('Initializing dimmer', $context);

Expand Down Expand Up @@ -121,6 +157,23 @@ $.fn.modal = function(parameters) {
},

create: {
modal: function() {
$module = $('<div/>', {class: 'ui modal'});
if (settings.closeIcon) {
$close = $('<i/>', {class: className.close})
$module.append($close);
}
if (settings.title !== '') {
$('<div/>', {class: 'header'}).appendTo($module);
}
if (settings.content !== '') {
$('<div/>', {class: 'content'}).appendTo($module);
}
if (module.has.configActions()) {
$('<div/>', {class: 'actions'}).appendTo($module);
}
$context.append($module);
},
dimmer: function() {
var
defaultSettings = {
Expand Down Expand Up @@ -499,7 +552,9 @@ $.fn.modal = function(parameters) {
$previousModal.find(selector.dimmer).removeClass('active');
}
}
settings.onHidden.call(element);
if($.isFunction(settings.onHidden)) {
settings.onHidden.call(element);
}
module.remove.dimmerStyles();
module.restore.focus();
callback();
Expand Down Expand Up @@ -699,7 +754,35 @@ $.fn.modal = function(parameters) {
$module.removeClass(className.loading);
module.debug('Caching modal and container sizes', module.cache);
},

helpers: {
deQuote: function(string) {
return String(string).replace(/"/g,"");
},
escape: function(string, preserveHTML) {
if (preserveHTML){
return string;
}
var
badChars = /[<>"'`]/g,
shouldEscape = /[&<>"'`]/,
escape = {
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
},
escapedChar = function(chr) {
return escape[chr];
}
;
if(shouldEscape.test(string)) {
string = string.replace(/&(?![a-z0-9#]{1,6};)/, "&amp;");
return string.replace(badChars, escapedChar);
}
return string;
}
},
can: {
leftBodyScrollbar: function(){
if(module.cache.leftBodyScrollbar === undefined) {
Expand Down Expand Up @@ -734,7 +817,11 @@ $.fn.modal = function(parameters) {
;
}
},

has: {
configActions: function () {
return Array.isArray(settings.actions) && settings.actions.length > 0;
}
},
is: {
active: function() {
return $module.hasClass(className.active);
Expand Down Expand Up @@ -1107,6 +1194,7 @@ $.fn.modal = function(parameters) {
instance.invoke('destroy');
}
module.initialize();
returnedValue = $module;
}
})
;
Expand Down Expand Up @@ -1161,6 +1249,24 @@ $.fn.modal.settings = {
padding : 50,
scrollbarWidth: 10,

//dynamic content
title : '',
content : '',
class : '',
classTitle : '',
classContent : '',
classActions : '',
closeIcon : false,
actions : false,
preserveHTML : true,

fields : {
class : 'class',
text : 'text',
icon : 'icon',
click : 'click'
},

// called before show animation
onShow : function(){},

Expand All @@ -1171,7 +1277,7 @@ $.fn.modal.settings = {
onHide : function(){ return true; },

// called after hide animation
onHidden : function(){},
onHidden : false,

// called after approve selector match
onApprove : function(){ return true; },
Expand All @@ -1180,6 +1286,9 @@ $.fn.modal.settings = {
onDeny : function(){ return true; },

selector : {
title : '> .header',
content : '> .content',
actions : '> .actions',
close : '> .close',
approve : '.actions .positive, .actions .approve, .actions .ok',
deny : '.actions .negative, .actions .deny, .actions .cancel',
Expand All @@ -1201,7 +1310,9 @@ $.fn.modal.settings = {
loading : 'loading',
scrolling : 'scrolling',
undetached : 'undetached',
front : 'front'
front : 'front',
close : 'close icon',
button : 'ui button'
}
};

Expand Down