UNPKG

can

Version:

MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.

93 lines (85 loc) 2.34 kB
<script type="text/stache" id="demo-html"> <ui-tabs> <ui-panel ^add-panel,remove-panel title="foo">Foo Content</ui-panel> </ui-tabs> </script> <script src="../../../node_modules/steal/steal.js" id='demo-source'> import can from "can"; import stache from "can/view/stache/"; import 'can/map/define/'; can.Component.extend({ tag: "tabs", template: "<ul>"+ // Create an LI for each item in the panel's viewModel object "{{#panels}}"+ "<li {{#isActive}}class='active'{{/isActive}} "+ "can-click='makeActive'>"+ "{{title}}"+ "</li>"+ "{{/panels}}"+ "</ul>"+ "<content></content>", viewModel: { // Contains a list of all panel scopes within the // tabs element. panels: [], // When a `<panel>` element is inserted into the document, // it calls this method to add the panel's viewModel to the // panels array. addPanel: function(panel){ // If this is the first panel, activate it. if( this.attr("panels").length === 0 ) { this.makeActive(panel) } this.attr("panels").push(panel); }, // When a `<panel>` element is removed from the document, // it calls this method to remove the panel's viewModel from // the panels array. removePanel: function(panel){ var panels = this.attr("panels"); can.batch.start(); panels.splice(panels.indexOf(panel),1); // if the panel was active, make the first item active if(panel === this.attr("active")){ if(panels.length){ this.makeActive(panels[0]); } else { this.removeAttr("active") } } can.batch.stop() }, makeActive: function(panel){ this.attr("active",panel); this.attr("panels").each(function(panel){ panel.attr("active", false) }); panel.attr("active",true); }, // this is viewModel, not mustache // consider removing viewModel as arg isActive: function( panel ) { return this.attr('active') == panel } } }); can.Component.extend({ template: "{{#if active}}<content></content>{{/if}}", tag:"panel", viewModel: { active: false, title: "@" }, events: { inserted: function(){ this.element.parent().viewModel().addPanel( this.viewModel ); }, removed: function(){ this.element.parent().viewModel().removePanel( this.viewModel ); } } }); $("body").append(can.view("demo-html",{})); </script>