Browse Source

adds responsive styles to modal, collapsing when mobile, keeping 5% gutters on responsive grid all other sizes

pull/110/head
jlukic 11 years ago
parent
commit
4777267a54
2 changed files with 155 additions and 94 deletions
  1. 180
      src/modules/modal.js
  2. 69
      src/modules/modal.less

180
src/modules/modal.js

@ -13,7 +13,7 @@
$.fn.modal = function(parameters) { $.fn.modal = function(parameters) {
var var
$allModals = $(this),
$allModules = $(this),
$document = $(document), $document = $(document),
settings = ( $.isPlainObject(parameters) ) settings = ( $.isPlainObject(parameters) )
@ -27,7 +27,7 @@ $.fn.modal = function(parameters) {
eventNamespace = '.' + namespace, eventNamespace = '.' + namespace,
moduleNamespace = 'module-' + namespace, moduleNamespace = 'module-' + namespace,
moduleSelector = $allModals.selector || '',
moduleSelector = $allModules.selector || '',
time = new Date().getTime(), time = new Date().getTime(),
performance = [], performance = [],
@ -39,78 +39,78 @@ $.fn.modal = function(parameters) {
invokedResponse invokedResponse
; ;
$allModals
$allModules
.each(function() { .each(function() {
var var
$modal = $(this),
$module = $(this),
$context = $(settings.context), $context = $(settings.context),
$otherModals = $allModals.not($modal),
$closeButton = $modal.find(selector.closeButton),
$otherModals = $allModules.not($module),
$closeButton = $module.find(selector.closeButton),
$dimmer, $dimmer,
element = this, element = this,
instance = $modal.data(moduleNamespace),
modal
instance = $module.data(moduleNamespace),
module
; ;
modal = {
module = {
initialize: function() { initialize: function() {
modal.verbose('Attaching events');
module.verbose('Attaching events');
$closeButton $closeButton
.on('click', modal.event.close)
.on('click', module.event.close)
; ;
modal.cache.sizes();
module.cache.sizes();
modal.verbose('Creating dimmer');
module.verbose('Creating dimmer');
$context $context
.dimmer({ .dimmer({
closable: settings.closable, closable: settings.closable,
duration: settings.duration, duration: settings.duration,
onShow: function() { onShow: function() {
modal.add.keyboardShortcuts();
module.add.keyboardShortcuts();
$.proxy(settings.onShow, this)(); $.proxy(settings.onShow, this)();
}, },
onHide: function() { onHide: function() {
if($modal.is(':visible')) {
if($module.is(':visible')) {
$context.off('.dimmer'); $context.off('.dimmer');
modal.hide();
module.hide();
$.proxy(settings.onHide, this)(); $.proxy(settings.onHide, this)();
} }
modal.remove.keyboardShortcuts();
module.remove.keyboardShortcuts();
} }
}) })
; ;
$dimmer = $context.children(selector.dimmer); $dimmer = $context.children(selector.dimmer);
if( $modal.parent()[0] !== $dimmer[0] ) {
modal.debug('Moving element inside dimmer', $context);
$modal = $modal
if( $module.parent()[0] !== $dimmer[0] ) {
module.debug('Moving element inside dimmer', $context);
$module = $module
.detach() .detach()
.appendTo($dimmer) .appendTo($dimmer)
; ;
} }
modal.instantiate();
module.instantiate();
}, },
instantiate: function() { instantiate: function() {
modal.verbose('Storing instance of modal');
instance = modal;
$modal
module.verbose('Storing instance of modal');
instance = module;
$module
.data(moduleNamespace, instance) .data(moduleNamespace, instance)
; ;
}, },
destroy: function() { destroy: function() {
modal.verbose('Destroying previous modal');
$modal
module.verbose('Destroying previous modal');
$module
.off(eventNamespace) .off(eventNamespace)
; ;
}, },
event: { event: {
close: function() { close: function() {
modal.verbose('Close button pressed');
module.verbose('Close button pressed');
$context.dimmer('hide'); $context.dimmer('hide');
}, },
keyboard: function(event) { keyboard: function(event) {
@ -119,62 +119,64 @@ $.fn.modal = function(parameters) {
escapeKey = 27 escapeKey = 27
; ;
if(keyCode == escapeKey) { if(keyCode == escapeKey) {
modal.debug('Escape key pressed hiding modal');
module.debug('Escape key pressed hiding modal');
$context.dimmer('hide'); $context.dimmer('hide');
event.preventDefault(); event.preventDefault();
} }
}, },
resize: function() { resize: function() {
modal.cache.sizes();
if( $modal.is(':visible') ) {
modal.set.type();
modal.set.position();
module.cache.sizes();
if( $module.is(':visible') ) {
module.set.type();
module.set.position();
} }
} }
}, },
toggle: function() { toggle: function() {
if( modal.is.active() ) {
modal.hide();
if( module.is.active() ) {
module.hide();
} }
else { else {
modal.show();
module.show();
} }
}, },
show: function() { show: function() {
modal.debug('Showing modal');
modal.set.type();
modal.set.position();
modal.hideAll();
module.debug('Showing modal');
module.set.type();
module.set.position();
module.hideAll();
if(settings.transition && $.fn.transition !== undefined) { if(settings.transition && $.fn.transition !== undefined) {
$modal
.transition(settings.transition + ' in', settings.duration, modal.set.active)
$module
.transition(settings.transition + ' in', settings.duration, module.set.active)
; ;
} }
else { else {
$modal
.fadeIn(settings.duration, settings.easing, modal.set.active)
$module
.fadeIn(settings.duration, settings.easing, module.set.active)
; ;
} }
modal.debug('Triggering dimmer');
module.debug('Triggering dimmer');
$context.dimmer('show'); $context.dimmer('show');
}, },
hide: function() { hide: function() {
modal.debug('Hiding modal');
module.debug('Hiding modal');
// remove keyboard detection // remove keyboard detection
$document $document
.off('keyup.' + namespace) .off('keyup.' + namespace)
; ;
if(settings.transition && $.fn.transition !== undefined) { if(settings.transition && $.fn.transition !== undefined) {
$modal
.transition(settings.transition + ' out', settings.duration, modal.remove.active)
$module
.transition(settings.transition + ' out', settings.duration, function() {
module.remove.active();
})
; ;
} }
else { else {
$modal
.fadeOut(settings.duration, settings.easing, modal.remove.active)
$module
.fadeOut(settings.duration, settings.easing, module.remove.active)
; ;
} }
}, },
@ -188,19 +190,19 @@ $.fn.modal = function(parameters) {
add: { add: {
keyboardShortcuts: function() { keyboardShortcuts: function() {
modal.verbose('Adding keyboard shortcuts');
module.verbose('Adding keyboard shortcuts');
$document $document
.on('keyup' + eventNamespace, modal.event.keyboard)
.on('keyup' + eventNamespace, module.event.keyboard)
; ;
} }
}, },
remove: { remove: {
active: function() { active: function() {
$modal.removeClass(className.active);
$module.removeClass(className.active);
}, },
keyboardShortcuts: function() { keyboardShortcuts: function() {
modal.verbose('Removing keyboard shortcuts');
module.verbose('Removing keyboard shortcuts');
$document $document
.off('keyup' + eventNamespace) .off('keyup' + eventNamespace)
; ;
@ -209,54 +211,53 @@ $.fn.modal = function(parameters) {
cache: { cache: {
sizes: function() { sizes: function() {
modal.cache = {
height : $modal.outerHeight() + settings.offset,
module.cache = {
height : $module.outerHeight() + settings.offset,
contextHeight : (settings.context == 'body') contextHeight : (settings.context == 'body')
? $(window).height() ? $(window).height()
: $context.height() : $context.height()
}; };
console.log($modal);
modal.debug('Caching modal and container sizes', modal.cache);
module.debug('Caching modal and container sizes', module.cache);
} }
}, },
can: { can: {
fit: function() { fit: function() {
return (modal.cache.height < modal.cache.contextHeight);
return (module.cache.height < module.cache.contextHeight);
} }
}, },
is: { is: {
active: function() { active: function() {
return $modal.hasClass(className.active);
return $module.hasClass(className.active);
} }
}, },
set: { set: {
active: function() { active: function() {
$modal.addClass('active');
$module.addClass(className.active);
}, },
type: function() { type: function() {
if(modal.can.fit()) {
modal.verbose('Modal fits on screen');
$modal.removeClass(className.scrolling);
if(module.can.fit()) {
module.verbose('Modal fits on screen');
$module.removeClass(className.scrolling);
} }
else { else {
modal.verbose('Modal cannot fit on screen setting to scrolling');
$modal.addClass(className.scrolling);
module.verbose('Modal cannot fit on screen setting to scrolling');
$module.addClass(className.scrolling);
} }
}, },
position: function() { position: function() {
modal.verbose('Centering modal on page', modal.cache.height / 2);
if(modal.can.fit()) {
$modal
module.verbose('Centering modal on page', module.cache.height / 2);
if(module.can.fit()) {
$module
.css({ .css({
marginTop: -(modal.cache.height / 2)
marginTop: -(module.cache.height / 2)
}) })
; ;
} }
else { else {
$modal
$module
.css({ .css({
top: $context.prop('scrollTop') top: $context.prop('scrollTop')
}) })
@ -281,41 +282,41 @@ $.fn.modal = function(parameters) {
internal: function(name, value) { internal: function(name, value) {
if(value !== undefined) { if(value !== undefined) {
if( $.isPlainObject(name) ) { if( $.isPlainObject(name) ) {
$.extend(true, modal, name);
$.extend(true, module, name);
} }
else { else {
modal[name] = value;
module[name] = value;
} }
} }
else { else {
return modal[name];
return module[name];
} }
}, },
debug: function() { debug: function() {
if(settings.debug) { if(settings.debug) {
if(settings.performance) { if(settings.performance) {
modal.performance.log(arguments);
module.performance.log(arguments);
} }
else { else {
modal.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
modal.debug.apply(console, arguments);
module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
module.debug.apply(console, arguments);
} }
} }
}, },
verbose: function() { verbose: function() {
if(settings.verbose && settings.debug) { if(settings.verbose && settings.debug) {
if(settings.performance) { if(settings.performance) {
modal.performance.log(arguments);
module.performance.log(arguments);
} }
else { else {
modal.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
modal.verbose.apply(console, arguments);
module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
module.verbose.apply(console, arguments);
} }
} }
}, },
error: function() { error: function() {
modal.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
modal.error.apply(console, arguments);
module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
module.error.apply(console, arguments);
}, },
performance: { performance: {
log: function(message) { log: function(message) {
@ -336,8 +337,8 @@ $.fn.modal = function(parameters) {
'Execution Time' : executionTime 'Execution Time' : executionTime
}); });
} }
clearTimeout(modal.performance.timer);
modal.performance.timer = setTimeout(modal.performance.display, 100);
clearTimeout(module.performance.timer);
module.performance.timer = setTimeout(module.performance.display, 100);
}, },
display: function() { display: function() {
var var
@ -345,7 +346,7 @@ $.fn.modal = function(parameters) {
totalTime = 0 totalTime = 0
; ;
time = false; time = false;
clearTimeout(modal.performance.timer);
clearTimeout(module.performance.timer);
$.each(performance, function(index, data) { $.each(performance, function(index, data) {
totalTime += data['Execution Time']; totalTime += data['Execution Time'];
}); });
@ -399,7 +400,7 @@ $.fn.modal = function(parameters) {
return false; return false;
} }
else { else {
modal.error(error.method);
module.error(error.method);
return false; return false;
} }
}); });
@ -425,15 +426,15 @@ $.fn.modal = function(parameters) {
if(methodInvoked) { if(methodInvoked) {
if(instance === undefined) { if(instance === undefined) {
modal.initialize();
module.initialize();
} }
invokedResponse = modal.invoke(query);
invokedResponse = module.invoke(query);
} }
else { else {
if(instance !== undefined) { if(instance !== undefined) {
modal.destroy();
module.destroy();
} }
modal.initialize();
module.initialize();
} }
}) })
; ;
@ -471,6 +472,7 @@ $.fn.modal.settings = {
method : 'The method you called is not defined.' method : 'The method you called is not defined.'
}, },
className : { className : {
active : 'active',
scrolling : 'scrolling' scrolling : 'scrolling'
}, },
}; };

69
src/modules/modal.less

@ -10,7 +10,7 @@
*/ */
/******************************* /*******************************
Modal
Modal
*******************************/ *******************************/
.ui.modal { .ui.modal {
@ -22,8 +22,8 @@
left: 50%; left: 50%;
text-align: left; text-align: left;
width: 800px;
margin-left: -400px;
width: 90%;
margin-left: -45%;
background-color: #FFFFFF; background-color: #FFFFFF;
border: 1px solid #DDDDDD; border: 1px solid #DDDDDD;
@ -33,6 +33,7 @@
border-radius: 5px; border-radius: 5px;
} }
/******************************* /*******************************
Content Content
*******************************/ *******************************/
@ -120,6 +121,62 @@
margin-left: 0.75em; margin-left: 0.75em;
} }
/*-------------------
Sizing
--------------------*/
/* Mobile Only */
@media only screen and (max-width : 768px) {
.ui.modal .content .left {
display: block;
padding: 0em 0em 0em 1em;
}
.ui.modal .content .right {
display: block;
padding: 1em 0em 0em 0em;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
}
}
/* Tablet and Mobile */
@media only screen and (max-width : 998px) {
.ui.modal {
width: 92%;
margin-left: -46%;
}
.ui.modal > .close {
color: rgba(0, 0, 0, 0.8);
top: 1.5rem;
right: 1rem;
}
}
/* Computer / Responsive */
@media only screen and (min-width : 998px) {
.ui.modal {
width: 74%;
margin-left: -37%;
}
}
@media only screen and (min-width : 1500px) {
.ui.modal {
width: 64%;
margin-left: -32%;
}
}
@media only screen and (min-width : 1750px) {
.ui.modal {
width: 54%;
margin-left: -27%;
}
}
@media only screen and (min-width : 2000px) {
.ui.modal {
width: 44%;
margin-left: -22%;
}
}
/******************************* /*******************************
@ -131,12 +188,14 @@
border: none; border: none;
color: #FFFFFF; color: #FFFFFF;
} }
.ui.basic.modal > .close {
top: 1.5rem;
right: 1rem;
}
.ui.basic.modal .content { .ui.basic.modal .content {
background-color: transparent; background-color: transparent;
} }
/******************************* /*******************************
Variations Variations
*******************************/ *******************************/

Loading…
Cancel
Save