Skip to content

Commit

Permalink
Merge pull request #103 from premasagar/feature/nest-options
Browse files Browse the repository at this point in the history
Nest options
  • Loading branch information
adhipg committed Nov 7, 2013
2 parents e158d7a + bc3c376 commit ab3619f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 27 additions & 14 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,35 @@ function(require, bonzo, qwery, bean) {

//convert data-sqwidget to dictionary
SqwidgetCore.prototype.getWidgetParams = function($el) {
var data, key, val, _ref;
data = {};
_ref = $el.data();

for (key in _ref) {
val = _ref[key];
if (!(key.match("sqwidget"))) {
continue;
var key, val,
data = {},
elData = $el.data();

//for compatibility data-sqwidget gets renamed data-sqwidget-url
if(elData.sqwidget) {
elData.sqwidgetUrl = elData.sqwidget;
delete elData.sqwidget;
}

//convert list of names into a nested structure with val as the value
var nest = function( names, val, data ) {
for( var i = 0; i < names.length; i++ ) {
data = data[ names[i].toLowerCase() ] =
i === names.length - 1 ? val : data[ names[i] ] || {};
}
key = key.replace("sqwidget", "").toLowerCase();
data[key || "url"] = val;
};

for (key in elData) {
val = elData[key];
if (!(key.match("^sqwidget"))) { continue; }
nest(key.match(/([A-Z]?[^A-Z]*)/g).slice(0,-1), val, data);
}
return data;

return data.sqwidget;
};

SqwidgetCore.prototype.guid = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
return 'sqwidget-xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
Expand All @@ -34,10 +46,11 @@ function(require, bonzo, qwery, bean) {
SqwidgetCore.prototype.register = function(el) {
var opts,
_this = this,
$el = bonzo(el).addClass('sqwidget');
id = this.guid(),
$el = bonzo(el).addClass('sqwidget').addClass(id);
opts = this.getWidgetParams($el);
opts.el = $el;
opts.id = this.guid();
opts.id = id;

if (!opts.url) {
throw new Error("No widget source defined (set data-sqwidget-url)");
Expand Down
2 changes: 1 addition & 1 deletion sqwidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
baseUrl: './sqwidget',
//preloads: ['curl/plugin/css','curl/plugin/i18n', 'curl/plugin/domReady'],
paths: {
underscore: { location: '//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.2.1/lodash.underscore.min.js' },
//underscore: { location: '//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.2.1/lodash.underscore.min.js' },
bean: { location: '//cdnjs.cloudflare.com/ajax/libs/bean/1.0.3/bean.min.js' },
moment: { location: '//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js' },
},
Expand Down

0 comments on commit ab3619f

Please sign in to comment.