Skip to content

Commit

Permalink
Merge pull request #92 from premasagar/feature/curljs
Browse files Browse the repository at this point in the history
Use curljs to build widgets
  • Loading branch information
adhipg committed Oct 30, 2013
2 parents a24de6c + 87933ef commit e4947a7
Show file tree
Hide file tree
Showing 54 changed files with 910 additions and 419 deletions.
2 changes: 1 addition & 1 deletion .bowerrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"directory": "sqwidget/lib"
"directory": "app/lib"
}
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
node_modules/
config.js

# temporary!
compiled/**/*.*
app/lib/
dist/
compiled/
example-widget/
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
npm-debug.log
node_modules
src
test
compiled
94 changes: 0 additions & 94 deletions Gruntfile.coffee

This file was deleted.

47 changes: 47 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-shell');
grunt.initConfig({
clean: {
all: {
src: ["compiled", "dist"]
}
},
connect: {
publisher: {
options: {
port: 8000,
base: '.'
}
},
sqwidget: {
options: {
port: 8002,
base: 'example-widget'
}
}
},
karma: {
integration: {
configFile: 'karma.conf.js'
}
},
shell: {
build_example: {
command: "./build_example.sh"
}
},
watch: {
scaffold: {
files: ["grunt-scaffold/root/main.js", "grunt-scaffold/root/app/**/*.js", "grunt-scaffold/**/*.tmpl"],
tasks: ["build"]
}
}
});
grunt.registerTask("build", ["shell:build_example"]);
grunt.registerTask("test", ["clean", "build", "karma"]);
grunt.registerTask("default", ["clean", "build", "connect", "watch"]);
};
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PATH := ./node_modules/.bin:${PATH}

.PHONY : init bower build test dist publish

init: bower
npm install

build:
grunt build

test:
grunt test

bower:
bower install

dist:
grunt dist

publish: dist
npm publish
100 changes: 100 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
define(['./lib/bonzo/bonzo', './lib/qwery/qwery', './lib/eventEmitter/EventEmitter', 'domReady!'],
function(bonzo, qwery, Emitter) {

var SqwidgetCore = (function() {

function SqwidgetCore() {}

for (var key in Emitter.prototype) {
SqwidgetCore.prototype[key] = Emitter.prototype[key];
}

SqwidgetCore.prototype.packages = {};

//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;
}
key = key.replace("sqwidget", "").toLowerCase();
data[key || "url"] = val;
}
return data;
};

SqwidgetCore.prototype.guid = function() {
return '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);
});
};

SqwidgetCore.prototype.register = function(el) {
var opts, pkg, _this = this, id = this.guid();

pkg = new Emitter();
var $el = bonzo(el).addClass('sqwidget').addClass(id);
pkg.opts = opts = this.getWidgetParams($el);
opts.el = pkg.el = $el;
opts.id = id;

if (!opts.url) {
throw new Error("No widget source defined (set data-sqwidget-url)");
}

this.packages[id] = {
location: opts.url,
main: 'app/index',
config: opts
};

return pkg;
};

SqwidgetCore.prototype.initialize = function() {
var names = [], pkg, _this = this;

for(var k in _this.packages) names.push(k);

curl({
packages: _this.packages,
}, names, function() {
var loaded = Array.prototype.slice.call(arguments);
for (var i = 0; i < loaded.length; i++) {
var module = loaded[i];

if(module.Controller) {
var widget = new module.Controller({
sqwidget: _this
});

//bus events
_this.trigger("rendered:" + _this.packages[names[i]].location);
_this.trigger("rendered:" + _this.packages[names[i]].id);
} else {
console.log("controller not found for " + module.location);
}
}
});
};

return SqwidgetCore;

})();

sqwidget = new SqwidgetCore();

bonzo(qwery('div[data-sqwidget]')).each(function(el) {
sqwidget.register(el);
});

sqwidget.initialize();

return sqwidget;
});
Loading

0 comments on commit e4947a7

Please sign in to comment.