-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from premasagar/feature/tests
Feature/tests
- Loading branch information
Showing
17 changed files
with
137 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
define ['underscore','backbone'], (_, Backbone) -> | ||
|
||
class SqwidgetCore | ||
constructor: () -> | ||
_.extend(@, Backbone.Events) | ||
|
||
registered: [] | ||
|
||
register: (el) -> | ||
$this = $(el).addClass('sqwidget') | ||
opts = @getWidgetParams($this) | ||
pkg = el: $this, opts: opts | ||
_.extend(pkg, Backbone.Events) | ||
@registered.push(pkg) | ||
|
||
throw new Error("No widget source") unless opts.url | ||
|
||
# we're expecting an 'index.js' file inside every widget. | ||
require ["#{opts.url}/js/index.js"], (module) => | ||
# 'settings' object defines all the settings that were passed in via the | ||
# embed code. | ||
widget = new module.Controller({settings: opts, sqwidget: @, el: $this}) | ||
pkg.instance = widget | ||
# fire a 'rendered' method so that the widget can do any post-render | ||
# operations that it needs to do. | ||
#widget.view.trigger("rendered") | ||
pkg.trigger("rendered") | ||
@trigger("rendered:#{widget.id || opts.url}") | ||
return pkg | ||
|
||
# returns an array of all the custom widget parameters. The new keys are | ||
# lowercase concatenated attributes from the embed code with 'data-sqwidget-' | ||
# removed. | ||
# | ||
# eg: | ||
# 'data-sqwidget-color="#F00" -> `data['color'] = '#F00'` | ||
# 'data-sqwidget-bg-color='#FFF' -> data['bgcolor'] = '#FFF' | ||
# | ||
getWidgetParams: ($el) -> | ||
data = {} | ||
for key, val of $el.data() | ||
key = key.replace("sqwidget", "").toLowerCase() | ||
data[key || "url" ] = val | ||
data | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,24 @@ | ||
requirejs.config | ||
paths: | ||
require: '../../lib/requirejs/require' | ||
jquery: '../../lib/jquery/jquery' | ||
underscore: '../../lib/underscore-amd/underscore' | ||
backbone: '../../lib/backbone-amd/backbone' | ||
ractive: '../../lib/ractive/Ractive' | ||
Ractive: '../../lib/ractive/Ractive' | ||
text: '../../lib/requirejs-text/text', | ||
rv: '../../lib/requirejs-ractive/rv', | ||
normalize: '../../lib/normalize-css/normalize' | ||
config: '../../config' | ||
|
||
# The module that is loaded first | ||
requirejs [ | ||
'jquery' | ||
'underscore' | ||
'backbone' | ||
'require' | ||
'ractive' | ||
], ($, _, Backbone, require, Ractive) -> | ||
# the only global object that we will use. | ||
Sqwidget = window.Sqwidget || {} | ||
|
||
# add pub/sub to sqwidget. may be removed for a cleaner implementation | ||
_.extend(Sqwidget, Backbone.Events) | ||
'component/core' | ||
], ($, Core) -> | ||
|
||
# Just loads all the widgets | ||
# TODO: Remove jQuery. | ||
$(document).ready () => | ||
# the only global object that we will use. | ||
sqwidget = window.sqwidget = new Core() | ||
# Iterate all elements and register | ||
$(document).ready -> | ||
$('div[data-sqwidget]').each (index) -> | ||
$this = $(this).addClass('sqwidget') | ||
url = $this.data('sqwidget') | ||
# we're expecting an 'index.js' file inside every widget. | ||
# TODO: Use grunt to concat all the widget JS files into a single index.js | ||
# file. | ||
require ["#{url}/js/index.js"], (module) -> | ||
params = getWidgetParams($this) | ||
# 'settings' object defines all the settings that were passed in via the | ||
# embed code. | ||
widget = new module.Controller({settings: params}) | ||
$this.html(widget.view.el) | ||
# fire a 'rendered' method so that the widget can do any post-render | ||
# operations that it needs to do. | ||
widget.view.trigger("rendered") | ||
sqwidget.register(@) | ||
|
||
# returns an array of all the custom widget parameters. The new keys are | ||
# lowercase concatenated attributes from the embed code with 'data-sqwidget-' | ||
# removed. | ||
# | ||
# eg: | ||
# 'data-sqwidget-color="#F00" -> `data['color'] = '#F00'` | ||
# 'data-sqwidget-bg-color='#FFF' -> data['bgcolor'] = '#FFF' | ||
getWidgetParams = ($widget) -> | ||
data = [] | ||
for key, val of $widget.data() | ||
key = key.replace("sqwidget", "").toLowerCase() | ||
if key != "" | ||
data[key] = val | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
. | ||
* | ||
!*.js |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
define ['chai', 'jquery', 'component/core'], (chai, $, Core) -> | ||
assert = chai.assert | ||
describe 'Core', -> | ||
sqwidget = new Core() | ||
src = $("<div data-sqwidget-test='moo' data-sqwidget='/base/widgets/test'></div>'") | ||
|
||
describe '#register()', -> | ||
widget = sqwidget.register(src) | ||
|
||
it 'should register widget', -> | ||
assert.lengthOf(sqwidget.registered, 1, "Registered module") | ||
|
||
it 'should parse params correctly', -> | ||
assert.deepEqual( widget.opts, { test: 'moo', url: '/base/widgets/test' } , "params parsed") | ||
|
||
it 'should trigger rendered event', (done) -> | ||
widget.on 'rendered', -> | ||
assert.ok "Triggered event" | ||
assert.equal(src.html(), | ||
'<div>TEST</div>' | ||
'Rendered Ractive view correctly' | ||
) | ||
done() | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../compiled/widgets/js/test/src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#Example widget using ractive to handle its view | ||
|
||
define [ | ||
'underscore' | ||
'backbone' | ||
'Ractive' | ||
'rv!../templates/test.html' | ||
#"css!./css/app.css" | ||
], (_, Backbone, Ractive, template) -> | ||
module = {views: {}} | ||
|
||
class module.Controller | ||
constructor: ({@el, @settings} = {}) -> | ||
view = new Ractive | ||
el: @el, | ||
template: template, | ||
data: | ||
test: "TEST" | ||
|
||
module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>{{test}}</div> |