diff --git a/src/definitions/modules/modal.js b/src/definitions/modules/modal.js index 420ecc250b..d9f98fcd3e 100755 --- a/src/definitions/modules/modal.js +++ b/src/definitions/modules/modal.js @@ -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, @@ -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, @@ -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] ? '' : '', + 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($('', { + 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); @@ -121,6 +157,23 @@ $.fn.modal = function(parameters) { }, create: { + modal: function() { + $module = $('
', {class: 'ui modal'}); + if (settings.closeIcon) { + $close = $('', {class: className.close}) + $module.append($close); + } + if (settings.title !== '') { + $('', {class: 'header'}).appendTo($module); + } + if (settings.content !== '') { + $('', {class: 'content'}).appendTo($module); + } + if (module.has.configActions()) { + $('', {class: 'actions'}).appendTo($module); + } + $context.append($module); + }, dimmer: function() { var defaultSettings = { @@ -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(); @@ -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 = { + "<": "<", + ">": ">", + '"': """, + "'": "'", + "`": "`" + }, + escapedChar = function(chr) { + return escape[chr]; + } + ; + if(shouldEscape.test(string)) { + string = string.replace(/&(?![a-z0-9#]{1,6};)/, "&"); + return string.replace(badChars, escapedChar); + } + return string; + } + }, can: { leftBodyScrollbar: function(){ if(module.cache.leftBodyScrollbar === undefined) { @@ -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); @@ -1107,6 +1194,7 @@ $.fn.modal = function(parameters) { instance.invoke('destroy'); } module.initialize(); + returnedValue = $module; } }) ; @@ -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(){}, @@ -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; }, @@ -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', @@ -1201,7 +1310,9 @@ $.fn.modal.settings = { loading : 'loading', scrolling : 'scrolling', undetached : 'undetached', - front : 'front' + front : 'front', + close : 'close icon', + button : 'ui button' } };