Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | angular.module('xos.UITutorial') |
| 22 | .service('PlayCmd', function($compile, $rootScope, _, ErrorHandler){ |
| 23 | |
| 24 | // TODO investigate if we can load directives from an app |
| 25 | const components = [ |
| 26 | { |
| 27 | name: 'xosTable', |
| 28 | template: '<xos-table config="config" data="data"></xos-table>', |
| 29 | scope: { |
| 30 | config: { |
| 31 | columns: [ |
| 32 | {label: 'Name', prop: 'name'}, |
| 33 | {label: 'Age', prop: 'age'} |
| 34 | ] |
| 35 | }, |
| 36 | data: [ |
| 37 | {name: 'Jhon', age: 23}, |
| 38 | {name: 'Mike', age: 24} |
| 39 | ] |
| 40 | } |
| 41 | }, |
| 42 | { |
| 43 | name: 'xosForm', |
| 44 | template: '<xos-form config="config" ng-model="data"></xos-form>', |
| 45 | scope: { |
| 46 | config: { |
| 47 | fields: { |
| 48 | name: { |
| 49 | type: 'text' |
| 50 | }, |
| 51 | age: { |
| 52 | type: 'number' |
| 53 | } |
| 54 | }, |
| 55 | actions: [ |
| 56 | { |
| 57 | label: 'Print', |
| 58 | cb: (model) => { |
| 59 | console.log(model); |
| 60 | } |
| 61 | } |
| 62 | ] |
| 63 | }, |
| 64 | data: {name: 'Jhon', age: 23} |
| 65 | } |
| 66 | } |
| 67 | ]; |
| 68 | |
| 69 | this.componentCompletion = (cmd, arg, line, done) => { |
| 70 | const componentsName = components.map(c => c.name); |
| 71 | return done(this.shell.bestMatch(arg, componentsName)); |
| 72 | }; |
| 73 | |
| 74 | this.componentExec = (cmd, args, done) => { |
| 75 | const targetComponent = args[0]; |
| 76 | |
| 77 | if(!targetComponent){ |
| 78 | return ErrorHandler.print(`Component "${targetComponent}" does not exists`, done); |
| 79 | } |
| 80 | |
| 81 | this.attachComponent(targetComponent, done); |
| 82 | }; |
| 83 | |
| 84 | this.getComponentsDetails = (componentName, components) => { |
| 85 | return _.find(components, {name: componentName}); |
| 86 | }; |
| 87 | |
| 88 | this.attachComponent = (targetComponent, done) => { |
| 89 | this.scope = $rootScope.$new(); |
| 90 | targetComponent = this.getComponentsDetails(targetComponent, components); |
| 91 | |
| 92 | angular.extend(this.scope, targetComponent.scope); |
| 93 | |
| 94 | $rootScope.$emit('uiTutorial.attachScope', this.scope); |
| 95 | |
| 96 | const directive = $compile(targetComponent.template)(this.scope); |
| 97 | const container = $('#directive-container'); |
| 98 | container.html(''); |
| 99 | container.append( directive ); |
| 100 | done('Component added'); |
| 101 | }; |
| 102 | |
| 103 | $rootScope.$on('uiTutorial.applyScope', (e, scope) => { |
| 104 | this.scope.config = scope.config; |
| 105 | this.scope.data = scope.data; |
| 106 | }); |
| 107 | |
| 108 | this.setup = (shell) => { |
| 109 | this.shell = shell; |
| 110 | shell.setCommandHandler('component', { |
| 111 | exec: this.componentExec, |
| 112 | completion: this.componentCompletion |
| 113 | }); |
| 114 | |
| 115 | // activate listener to enable/disable shell |
| 116 | $('.component-container').click((e) => { |
| 117 | this.shell.deactivate(); |
| 118 | }); |
| 119 | |
| 120 | $('#shell-panel').click((e) => { |
| 121 | this.shell.activate(); |
| 122 | }); |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | }); |
| 127 | })(); |