Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
Move sanitization and createWidgetClass methods to intro.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Craga89 committed Apr 3, 2013
1 parent e9026a0 commit 5c88d38
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 88 deletions.
82 changes: 0 additions & 82 deletions src/core.js
@@ -1,81 +1,3 @@
// Option object sanitizer
function sanitizeOptions(opts)
{
var invalid = function(a) { return a === NULL || !$.isPlainObject(a); },
invalidContent = function(c) { return !$.isFunction(c) && ((!c && !c.attr) || c.length < 1 || ('object' === typeof c && !c.jquery && !c.then)); },
once;

if(!opts || 'object' !== typeof opts) { return FALSE; }

if(invalid(opts.metadata)) {
opts.metadata = { type: opts.metadata };
}

if('content' in opts) {
if(invalid(opts.content) || opts.content.jquery || opts.content.done) {
opts.content = { text: opts.content };
}

if(invalidContent(opts.content.text || FALSE)) {
opts.content.text = FALSE;
}

// DEPRECATED - Old content.ajax plugin functionality
// Converts it into the proper Deferred syntax
if('ajax' in opts.content) {
once = opts.content.ajax.once !== FALSE;
opts.content.text = function(event, api) {
var deferred = $.ajax(opts.content.ajax)
.then(function(content) {
if(once) { api.set('content.text', content); }
return content;
},
function(xhr, status, error) {
if(api.destroyed || xhr.status === 0) { return; }
api.set('content.text', status + ': ' + error);
});

return !once ? deferred : 'Loading...';
};
}

if('title' in opts.content) {
if(!invalid(opts.content.title)) {
opts.content.button = opts.content.title.button;
opts.content.title = opts.content.title.text;
}

if(invalidContent(opts.content.title || FALSE)) {
opts.content.title = FALSE;
}
}
}

if('position' in opts && invalid(opts.position)) {
opts.position = { my: opts.position, at: opts.position };
}

if('show' in opts && invalid(opts.show)) {
opts.show = opts.show.jquery ? { target: opts.show } :
opts.show === TRUE ? { ready: TRUE } : { event: opts.show };
}

if('hide' in opts && invalid(opts.hide)) {
opts.hide = opts.hide.jquery ? { target: opts.hide } : { event: opts.hide };
}

if('style' in opts && invalid(opts.style)) {
opts.style = { classes: opts.style };
}

// Sanitize plugin options
$.each(PLUGINS, function() {
if(this.sanitize) { this.sanitize(opts); }
});

return opts;
}

/*
* Core plugin implementation
*/
Expand Down Expand Up @@ -125,10 +47,6 @@ function QTip(target, options, id, attr)
return [obj || options, levels.pop()];
}

function createWidgetClass(cls)
{
return widget.concat('').join(cls ? '-'+cls+' ' : ' ');
}

function setWidget()
{
Expand Down
91 changes: 85 additions & 6 deletions src/intro.js
Expand Up @@ -40,16 +40,13 @@
FLIPINVERT = 'flipinvert',
SHIFT = 'shift',

// Used by image load detection (see core.js)
BLANKIMG = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==',

// Shortcut vars
QTIP, PLUGINS,
NAMESPACE = 'qtip',
HASATTR = 'data-hasqtip',
WIDGET = ['ui-widget', 'ui-tooltip'],
MOUSE = {},
usedIDs = {},
widget = ['ui-widget', 'ui-tooltip'],
selector = 'div.qtip.'+NAMESPACE,
defaultClass = NAMESPACE + '-default',
focusClass = NAMESPACE + '-focus',
Expand All @@ -59,13 +56,95 @@
trackingBound;

// Store mouse coordinates
function storeMouse(id, event)
{
function storeMouse(id, event) {
MOUSE[id] = {
pageX: event.pageX,
pageY: event.pageY,
type: 'mousemove',
scrollX: window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft,
scrollY: window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop
};
}

// Widget class creator
function createWidgetClass(cls) {
return WIDGET.concat('').join(cls ? '-'+cls+' ' : ' ');
}

// Option object sanitizer
function sanitizeOptions(opts)
{
var invalid = function(a) { return a === NULL || !$.isPlainObject(a); },
invalidContent = function(c) { return !$.isFunction(c) && ((!c && !c.attr) || c.length < 1 || ('object' === typeof c && !c.jquery && !c.then)); },
once;

if(!opts || 'object' !== typeof opts) { return FALSE; }

if(invalid(opts.metadata)) {
opts.metadata = { type: opts.metadata };
}

if('content' in opts) {
if(invalid(opts.content) || opts.content.jquery || opts.content.done) {
opts.content = { text: opts.content };
}

if(invalidContent(opts.content.text || FALSE)) {
opts.content.text = FALSE;
}

// DEPRECATED - Old content.ajax plugin functionality
// Converts it into the proper Deferred syntax
if('ajax' in opts.content) {
once = opts.content.ajax.once !== FALSE;
opts.content.text = function(event, api) {
var deferred = $.ajax(opts.content.ajax)
.then(function(content) {
if(once) { api.set('content.text', content); }
return content;
},
function(xhr, status, error) {
if(api.destroyed || xhr.status === 0) { return; }
api.set('content.text', status + ': ' + error);
});

return !once ? deferred : 'Loading...';
};
}

if('title' in opts.content) {
if(!invalid(opts.content.title)) {
opts.content.button = opts.content.title.button;
opts.content.title = opts.content.title.text;
}

if(invalidContent(opts.content.title || FALSE)) {
opts.content.title = FALSE;
}
}
}

if('position' in opts && invalid(opts.position)) {
opts.position = { my: opts.position, at: opts.position };
}

if('show' in opts && invalid(opts.show)) {
opts.show = opts.show.jquery ? { target: opts.show } :
opts.show === TRUE ? { ready: TRUE } : { event: opts.show };
}

if('hide' in opts && invalid(opts.hide)) {
opts.hide = opts.hide.jquery ? { target: opts.hide } : { event: opts.hide };
}

if('style' in opts && invalid(opts.style)) {
opts.style = { classes: opts.style };
}

// Sanitize plugin options
$.each(PLUGINS, function() {
if(this.sanitize) { this.sanitize(opts); }
});

return opts;
}

0 comments on commit 5c88d38

Please sign in to comment.