Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | angular.module('xos.UITutorial') |
| 4 | .service('PlayCmd', function($compile, $rootScope, _, ErrorHandler){ |
| 5 | |
| 6 | // TODO investigate if we can load directives from an app |
| 7 | const components = [ |
| 8 | { |
| 9 | name: 'xosTable', |
| 10 | template: '<xos-table config="config" data="data"></xos-table>', |
| 11 | scope: { |
| 12 | config: { |
| 13 | columns: [ |
| 14 | {label: 'Name', prop: 'name'}, |
| 15 | {label: 'Age', prop: 'age'} |
| 16 | ] |
| 17 | }, |
| 18 | data: [ |
| 19 | {name: 'Jhon', age: 23}, |
| 20 | {name: 'Mike', age: 24} |
| 21 | ] |
| 22 | } |
| 23 | }, |
| 24 | { |
| 25 | name: 'xosForm', |
| 26 | template: '<xos-form config="config" ng-model="data"></xos-form>', |
| 27 | scope: { |
| 28 | config: { |
| 29 | fields: { |
| 30 | name: { |
| 31 | type: 'text' |
| 32 | }, |
| 33 | age: { |
| 34 | type: 'number' |
| 35 | } |
| 36 | }, |
| 37 | actions: [ |
| 38 | { |
| 39 | label: 'Print', |
| 40 | cb: (model) => { |
| 41 | console.log(model); |
| 42 | } |
| 43 | } |
| 44 | ] |
| 45 | }, |
| 46 | data: {name: 'Jhon', age: 23} |
| 47 | } |
| 48 | } |
| 49 | ]; |
| 50 | |
| 51 | this.componentCompletion = (cmd, arg, line, done) => { |
| 52 | const componentsName = components.map(c => c.name); |
| 53 | return done(this.shell.bestMatch(arg, componentsName)); |
| 54 | }; |
| 55 | |
| 56 | this.componentExec = (cmd, args, done) => { |
| 57 | const targetComponent = args[0]; |
| 58 | |
| 59 | if(!targetComponent){ |
| 60 | return ErrorHandler.print(`Component "${targetComponent}" does not exists`, done); |
| 61 | } |
| 62 | |
| 63 | this.attachComponent(targetComponent, done); |
| 64 | }; |
| 65 | |
| 66 | this.getComponentsDetails = (componentName, components) => { |
| 67 | return _.find(components, {name: componentName}); |
| 68 | }; |
| 69 | |
| 70 | this.attachComponent = (targetComponent, done) => { |
| 71 | this.scope = $rootScope.$new(); |
| 72 | targetComponent = this.getComponentsDetails(targetComponent, components); |
| 73 | |
| 74 | angular.extend(this.scope, targetComponent.scope); |
| 75 | |
| 76 | $rootScope.$emit('uiTutorial.attachScope', this.scope); |
| 77 | |
| 78 | const directive = $compile(targetComponent.template)(this.scope); |
| 79 | const container = $('#directive-container'); |
| 80 | container.html(''); |
| 81 | container.append( directive ); |
| 82 | done('Component added'); |
| 83 | }; |
| 84 | |
| 85 | $rootScope.$on('uiTutorial.applyScope', (e, scope) => { |
| 86 | this.scope.config = scope.config; |
| 87 | this.scope.data = scope.data; |
| 88 | }); |
| 89 | |
| 90 | this.setup = (shell) => { |
| 91 | this.shell = shell; |
| 92 | shell.setCommandHandler('component', { |
| 93 | exec: this.componentExec, |
| 94 | completion: this.componentCompletion |
| 95 | }); |
| 96 | |
| 97 | // activate listener to enable/disable shell |
| 98 | $('.component-container').click((e) => { |
| 99 | this.shell.deactivate(); |
| 100 | }); |
| 101 | |
| 102 | $('#shell-panel').click((e) => { |
| 103 | this.shell.activate(); |
| 104 | }); |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | }); |
| 109 | })(); |