├── .gitignore ├── .travis.yml ├── .versions ├── LICENSE ├── README.md ├── dist ├── bindPolyfill.js └── webcomponents.js ├── lib └── registerElement.coffee ├── package.js ├── test ├── componentRendered.coffee └── mockup │ ├── component.coffee │ ├── component.css │ └── component.html └── versions.json /.gitignore: -------------------------------------------------------------------------------- 1 | .build* 2 | *.swo 3 | *.swp 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | before_install: 5 | - "curl -L http://git.io/ejPSng | /bin/sh" 6 | -------------------------------------------------------------------------------- /.versions: -------------------------------------------------------------------------------- 1 | base64@1.0.3 2 | binary-heap@1.0.3 3 | blaze@2.1.2 4 | blaze-tools@1.0.3 5 | callback-hook@1.0.3 6 | check@1.0.5 7 | coffeescript@1.0.6 8 | ddp@1.1.0 9 | deps@1.0.7 10 | ejson@1.0.6 11 | geojson-utils@1.0.3 12 | html-tools@1.0.4 13 | htmljs@1.0.4 14 | id-map@1.0.3 15 | jquery@1.11.3_2 16 | json@1.0.3 17 | local-test:numtel:webcomponent@0.0.6 18 | logging@1.0.7 19 | meteor@1.1.6 20 | minifiers@1.1.5 21 | minimongo@1.0.8 22 | mongo@1.1.0 23 | numtel:webcomponent@0.0.6 24 | observe-sequence@1.0.6 25 | ordered-dict@1.0.3 26 | random@1.0.3 27 | reactive-dict@1.1.0 28 | reactive-var@1.0.5 29 | retry@1.0.3 30 | session@1.1.0 31 | spacebars-compiler@1.0.6 32 | templating@1.1.1 33 | test-helpers@1.0.4 34 | tinytest@1.0.5 35 | tracker@1.0.7 36 | underscore@1.0.3 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 ben@latenightsketches.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # numtel:webcomponent 2 | 3 | Turn any Meteor template into a new element on all modern browsers with help from Polymer's [webcomponents.js polyfill library](https://github.com/Polymer/webcomponentsjs) 4 | 5 | Last decade's iframes can finally be banished in favor of new WebComponents. Full stylesheet and DOM isolation without extra layers is native in some browsers already (Chrome, Firefox 34). The Polymer library brings WebComponent support to all modern browsers (IE 9+). [Learn more about WebComponents...](http://webcomponents.org/) 6 | 7 | ### Why use WebComponents when Meteor already has Spacebars? 8 | 9 | Spacebars already provides some of the features of WebComponents: attributes, child DOM. 10 | Beyond these features, a WebComponent provides CSS and DOM isolation in what is called a Shadow DOM. 11 | DOM isolation means that `querySelector()` or jQuery will not be able to directly find the elements. 12 | CSS isolation means that a rules on your page will not effect an element in your WebComponent's Shadow DOM (or vice-versa). 13 | 14 | Polymer's webcomponents.js library includes polyfills for all features except shadow DOM CSS isolation. To make up for this shortcoming, I have been working on a [shadow DOM CSS isolation polyfill](https://github.com/numtel/shadowstyles). 15 | 16 | ## Installation 17 | 18 | ```bash 19 | $ meteor add numtel:webcomponent 20 | ``` 21 | 22 | ## Hello, World 23 | 24 | Imagine the familiar template: 25 | 26 | ```html 27 | 31 | ``` 32 | 33 | This widget can be converted in to a WebComponent using its `registerElement` method: 34 | 35 | ```javascript 36 | 37 | // ...Default event handlers... 38 | 39 | Template.hello.registerElement('hello-counter'); 40 | ``` 41 | 42 | Then insert the new element anywhere in your application: 43 | 44 | ```html 45 | 46 | ``` 47 | 48 | ## Implements 49 | 50 | #### Template.prototype.registerElement(name, options) 51 | 52 | `name` *String* - The name of the new element type to be created. Must include a hyphen. A reference to the element constructor will be added to `window` on the camel-cased version of this name. 53 | 54 | `options` *Object* - Optionally, specify the following options: 55 | 56 | Key | Type | Description 57 | ---------|----------|-------------------------- 58 | `css` |`string` | Rules to add in a `' 25 | # Blaze Template must be wrapped as jQuery fails to find children 26 | # directly from the shadowRoot. 27 | shadowContent = styles + '
' 28 | 29 | newPrototype = Object.create HTMLElement.prototype 30 | newPrototype.createdCallback = -> 31 | shadow = this.createShadowRoot() 32 | shadow.innerHTML = shadowContent 33 | @childRoot = shadow.querySelector 'div' 34 | @blazeData = {} 35 | @blazeView = Blaze.renderWithData blazeTemplate, @blazeData, @childRoot 36 | @blazeView.autorun => 37 | _.each @blazeView.dataVar.get(), (name, attr) => 38 | @childRoot.parentNode.host.setAttribute attr, name 39 | newPrototype.attributeChangedCallback = (name, oldValue, newValue) -> 40 | @blazeData = @blazeView.dataVar.get() 41 | @blazeData[name] = newValue 42 | @blazeView.dataVar.set @blazeData 43 | 44 | element = document.registerElement name, 45 | prototype: newPrototype 46 | 47 | # Create global reference 48 | nameCamelCase = name.replace /-([a-z])/g, (g) -> g[1].toUpperCase() 49 | window[nameCamelCase] = element 50 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'numtel:webcomponent', 3 | summary: 'Create WebComponents from Templates', 4 | version: '0.0.6', 5 | git: 'https://github.com/numtel/meteor-webcomponent.git' 6 | }); 7 | 8 | Package.onUse(function(api) { 9 | api.versionsFrom('METEOR@0.9.3.1'); 10 | api.use([ 11 | 'underscore', 12 | 'coffeescript', 13 | 'templating' 14 | ]); 15 | api.addFiles([ 16 | 'dist/bindPolyfill.js', 17 | 'dist/webcomponents.js', 18 | 'lib/registerElement.coffee' 19 | ], 'client'); 20 | }); 21 | 22 | Package.onTest(function(api) { 23 | api.use([ 24 | 'tinytest', 25 | 'test-helpers', 26 | 'coffeescript', 27 | 'session', 28 | 'templating', 29 | 'numtel:webcomponent' 30 | ]); 31 | api.addFiles([ 32 | 'test/mockup/component.html', 33 | 'test/mockup/component.css', 34 | 'test/mockup/component.coffee', 35 | 'test/componentRendered.coffee' 36 | ], 'client'); 37 | }); 38 | -------------------------------------------------------------------------------- /test/componentRendered.coffee: -------------------------------------------------------------------------------- 1 | # numtel:webcomponent 2 | # MIT License, ben@latenightsketches.com 3 | # test/componentRendered.coffee 4 | 5 | Tinytest.add 'window reference created', (test) -> 6 | test.equal typeof window.testElement, 'function' 7 | 8 | Tinytest.add 'return value correct', (test) -> 9 | retval = Template.testElement.registerElement 'return-test' 10 | test.equal typeof retval, 'function' 11 | 12 | Tinytest.add 'h1 is red and contains "someval"', (test) -> 13 | rendered = document.querySelector 'test-element' 14 | h1 = rendered.shadowRoot.querySelector 'h1' 15 | style = rendered.shadowRoot.querySelector 'style' 16 | # Check attribute forwarding 17 | test.equal h1.innerHTML, 'someval' 18 | # Check css option 19 | test.include ['rgb(255, 0, 0)', 'red'], getStyleProperty(h1, 'color') 20 | # Check cssLinks option 21 | test.matches style.innerText, \ 22 | /^(@import url\("notfound.css"\);)/ 23 | 24 | Tinytest.addAsync 'counter span text is 1 greater after click', (test, done) -> 25 | # Check that events and reactivity work 26 | rendered = document.querySelector 'test-element' 27 | span = rendered.shadowRoot.querySelector 'span' 28 | button = rendered.shadowRoot.querySelector 'button' 29 | origValue = parseInt span.innerHTML, 10 30 | $(button).trigger 'click' 31 | Meteor.setTimeout (-> 32 | newValue = parseInt span.innerHTML, 10 33 | test.equal origValue, newValue - 1 34 | done() 35 | ), 10 36 | -------------------------------------------------------------------------------- /test/mockup/component.coffee: -------------------------------------------------------------------------------- 1 | Session.setDefault 'counter', 0 2 | 3 | Template.testElementInner.helpers 4 | counter: -> Session.get 'counter' 5 | 6 | Template.testElementInner.events 7 | 'click button': -> Session.set 'counter', Session.get('counter') + 1 8 | 9 | Template.testElement.registerElement 'test-element', 10 | css: 'h1 { color: red; } p { color: blue; }' 11 | cssLinks: ['notfound.css'] 12 | 13 | -------------------------------------------------------------------------------- /test/mockup/component.css: -------------------------------------------------------------------------------- 1 | p { color: green; } 2 | 3 | test-element { display: none; } 4 | -------------------------------------------------------------------------------- /test/mockup/component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

my content

4 |
5 | 6 | 7 | 13 | 14 | 21 | -------------------------------------------------------------------------------- /versions.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": [ 3 | [ 4 | "base64", 5 | "1.0.1" 6 | ], 7 | [ 8 | "blaze", 9 | "2.0.3" 10 | ], 11 | [ 12 | "coffeescript", 13 | "1.0.4" 14 | ], 15 | [ 16 | "deps", 17 | "1.0.5" 18 | ], 19 | [ 20 | "ejson", 21 | "1.0.4" 22 | ], 23 | [ 24 | "geojson-utils", 25 | "1.0.1" 26 | ], 27 | [ 28 | "htmljs", 29 | "1.0.2" 30 | ], 31 | [ 32 | "id-map", 33 | "1.0.1" 34 | ], 35 | [ 36 | "jquery", 37 | "1.0.1" 38 | ], 39 | [ 40 | "json", 41 | "1.0.1" 42 | ], 43 | [ 44 | "meteor", 45 | "1.1.3" 46 | ], 47 | [ 48 | "minimongo", 49 | "1.0.5" 50 | ], 51 | [ 52 | "observe-sequence", 53 | "1.0.3" 54 | ], 55 | [ 56 | "ordered-dict", 57 | "1.0.1" 58 | ], 59 | [ 60 | "random", 61 | "1.0.1" 62 | ], 63 | [ 64 | "reactive-var", 65 | "1.0.3" 66 | ], 67 | [ 68 | "templating", 69 | "1.0.9" 70 | ], 71 | [ 72 | "tracker", 73 | "1.0.3" 74 | ], 75 | [ 76 | "underscore", 77 | "1.0.1" 78 | ] 79 | ], 80 | "pluginDependencies": [], 81 | "toolVersion": "meteor-tool@1.0.35", 82 | "format": "1.0" 83 | } --------------------------------------------------------------------------------