Skip to content

Commit

Permalink
Conveniece method for namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
antstorm committed Jun 10, 2015
1 parent cb618cb commit f13c245
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions src/remodal.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
*/
function lockScreen() {
var $html = $('html');
var lockedClass = NAMESPACE + '-is-locked';
var lockedClass = namespacify('is-locked');
var paddingRight;
var $body;

Expand All @@ -250,7 +250,7 @@
*/
function unlockScreen() {
var $html = $('html');
var lockedClass = NAMESPACE + '-is-locked';
var lockedClass = namespacify('is-locked');
var paddingRight;
var $body;

Expand All @@ -274,37 +274,28 @@
* @param {String} Reason of a state change.
*/
function setState(instance, state, isSilent, reason) {

var newState = namespacify('is', state);
var allStates = [namespacify('is', STATES.CLOSING),
namespacify('is', STATES.OPENING),
namespacify('is', STATES.CLOSED),
namespacify('is', STATES.OPENED)].join(' ');

instance.$bg
.removeClass(
NAMESPACE + '-is-' + STATES.CLOSING + ' ' +
NAMESPACE + '-is-' + STATES.OPENING + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED + ' ' +
NAMESPACE + '-is-' + STATES.OPENED)
.addClass(NAMESPACE + '-is-' + state);
.removeClass(allStates)
.addClass(newState);

instance.$overlay
.removeClass(
NAMESPACE + '-is-' + STATES.CLOSING + ' ' +
NAMESPACE + '-is-' + STATES.OPENING + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED + ' ' +
NAMESPACE + '-is-' + STATES.OPENED)
.addClass(NAMESPACE + '-is-' + state);
.removeClass(allStates)
.addClass(newState);

instance.$wrapper
.removeClass(
NAMESPACE + '-is-' + STATES.CLOSING + ' ' +
NAMESPACE + '-is-' + STATES.OPENING + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED + ' ' +
NAMESPACE + '-is-' + STATES.OPENED)
.addClass(NAMESPACE + '-is-' + state);
.removeClass(allStates)
.addClass(newState);

instance.$modal
.removeClass(
NAMESPACE + '-is-' + STATES.CLOSING + ' ' +
NAMESPACE + '-is-' + STATES.OPENING + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED + ' ' +
NAMESPACE + '-is-' + STATES.OPENED)
.addClass(NAMESPACE + '-is-' + state);
.removeClass(allStates)
.addClass(newState);

instance.state = state;
!isSilent && instance.$modal.trigger({
Expand Down Expand Up @@ -430,6 +421,22 @@
return obj;
}

/**
* Generates a string separated by dashes and prefixed with NAMESPACE
* @private
* @param {...String}
* @returns {String}
*/
function namespacify() {
var result = NAMESPACE;

for (var i = 0; i < arguments.length; ++i) {
result += '-' + arguments[i];
}

return result;
}

/**
* Handles the hashchange event
* @private
Expand Down Expand Up @@ -481,24 +488,26 @@
remodal.index = $[PLUGIN_NAME].lookup.push(remodal) - 1;
remodal.state = STATES.CLOSED;

remodal.$overlay = $('.' + NAMESPACE + '-overlay');
remodal.$overlay = $('.' + namespacify('overlay'));

if (!remodal.$overlay.length) {
remodal.$overlay = $('<div>').addClass(NAMESPACE + '-overlay ' + NAMESPACE + '-is-' + STATES.CLOSED).hide();
remodal.$overlay = $('<div>').addClass(namespacify('overlay') + ' ' + namespacify('is', STATES.CLOSED)).hide();
$body.append(remodal.$overlay);
}

remodal.$bg = $('.' + NAMESPACE + '-bg').addClass(NAMESPACE + '-is-' + STATES.CLOSED);
remodal.$bg = $('.' + namespacify('bg')).addClass(namespacify('is', STATES.CLOSED));
remodal.$modal = $modal;
remodal.$modal.addClass(
NAMESPACE + '-is-initialized' + ' ' +
NAMESPACE + ' ' + remodal.settings.modifier + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED);
NAMESPACE + ' ' +
namespacify('is-initialized') + ' ' +
remodal.settings.modifier + ' ' +
namespacify('is', STATES.CLOSED));

remodal.$wrapper = $('<div>')
.addClass(
NAMESPACE + '-wrapper ' + remodal.settings.modifier + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED)
namespacify('wrapper') + ' ' +
remodal.settings.modifier + ' ' +
namespacify('is', STATES.CLOSED))
.hide()
.append(remodal.$modal);
$body.append(remodal.$wrapper);
Expand Down Expand Up @@ -536,7 +545,7 @@
remodal.$wrapper.on('click.' + NAMESPACE, function(e) {
var $target = $(e.target);

if (!$target.hasClass(NAMESPACE + '-wrapper')) {
if (!$target.hasClass(namespacify('wrapper'))) {
return;
}

Expand Down Expand Up @@ -654,10 +663,10 @@
if (instanceCount === 0) {
this.$overlay.remove();
this.$bg.removeClass(
NAMESPACE + '-is-' + STATES.CLOSING + ' ' +
NAMESPACE + '-is-' + STATES.OPENING + ' ' +
NAMESPACE + '-is-' + STATES.CLOSED + ' ' +
NAMESPACE + '-is-' + STATES.OPENED);
namespacify('is', STATES.CLOSING) + ' ' +
namespacify('is', STATES.OPENING) + ' ' +
namespacify('is', STATES.CLOSED) + ' ' +
namespacify('is', STATES.OPENED));
}
};

Expand Down

0 comments on commit f13c245

Please sign in to comment.