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 | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 19 | 'use strict'; |
| 20 | |
| 21 | angular.module('xos.UITutorial', [ |
| 22 | 'ngResource', |
| 23 | 'ngCookies', |
| 24 | 'ui.router', |
Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 25 | 'xos.helpers', |
| 26 | 'ui.ace' |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 27 | ]) |
| 28 | .config(($stateProvider) => { |
| 29 | $stateProvider |
| 30 | .state('shell', { |
| 31 | url: '/', |
| 32 | template: '<js-shell></js-shell>' |
| 33 | }); |
| 34 | }) |
| 35 | .config(function($httpProvider){ |
| 36 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 37 | }) |
Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 38 | .directive('jsShell', function($rootScope, TemplateHandler, codeToString){ |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 39 | return { |
| 40 | restrict: 'E', |
| 41 | scope: {}, |
| 42 | bindToController: true, |
| 43 | controllerAs: 'vm', |
| 44 | templateUrl: 'templates/js-shell.tpl.html', |
Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 45 | controller: function(ExploreCmd, PlayCmd, LearnCmd){ |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 46 | var history = new Josh.History({ key: 'jsshell.history'}); |
| 47 | this.shell = Josh.Shell({history: history}); |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 48 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 49 | this.shell.onNewPrompt(done => { |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 50 | done('[ngXosLib] $ '); |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 51 | }); |
| 52 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 53 | this.shell.setCommandHandler('explore', { |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 54 | exec: (cmd, args, done) => { |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 55 | ExploreCmd.setup(this.shell); |
| 56 | done(TemplateHandler.instructions({ |
| 57 | title: `You can now explore the API use angular $resouces!`, |
| 58 | messages: [ |
| 59 | `Use <code>resource list</code> to list all the available resources and <code>resource {resoureName} {method} {?paramters}</code> to call the API.`, |
| 60 | `An example command is <code>resource Slices query</code>`, |
| 61 | `You can also provide paramters with <code>resource Slices query {max_instances: 10}</code>` |
| 62 | ] |
| 63 | })); |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 64 | } |
| 65 | }); |
| 66 | |
Arpit Agarwal | 45fd705 | 2016-08-16 09:33:22 -0700 | [diff] [blame] | 67 | this.shell.setCommandHandler('learn', { |
| 68 | exec: (cmd, args, done) => { |
| 69 | LearnCmd.setup(this.shell); |
| 70 | done(TemplateHandler.instructions({ |
| 71 | title: `You can now learn the API`, |
| 72 | messages: [ |
| 73 | `Use <code>next</code> to move to the next lesson <code>resource {resoureName} {method} {?paramters}</code> to call the API.`, |
| 74 | `An example command is <code>resource Slices query</code>`, |
| 75 | `You can also provide paramters with <code>next</code>` |
| 76 | ] |
| 77 | })); |
| 78 | } |
| 79 | }); |
Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 80 | |
| 81 | this.shell.setCommandHandler('play', { |
| 82 | exec: (cmd, args, done) => { |
| 83 | PlayCmd.setup(this.shell); |
| 84 | done(TemplateHandler.instructions({ |
| 85 | title: `You can now play with UI components!`, |
| 86 | messages: [ |
| 87 | `Use <code>component list</code> to list all the available component and <code>component {componentName}</code> to startusing it.`, |
| 88 | `An example command is <code>component xosTable</code>` |
| 89 | ] |
| 90 | })); |
| 91 | } |
| 92 | }); |
Arpit Agarwal | 45fd705 | 2016-08-16 09:33:22 -0700 | [diff] [blame] | 93 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 94 | this.shell.activate(); |
Matteo Scandolo | a81496a | 2016-08-16 17:29:42 -0700 | [diff] [blame] | 95 | |
| 96 | this.componentScope = null; |
| 97 | |
| 98 | $rootScope.$on('uiTutorial.attachScope', (e, scope) => { |
| 99 | this.componentScope = { |
| 100 | config: JSON.stringify(codeToString.toString(scope.config), null, 2), |
| 101 | data: JSON.stringify(codeToString.toString(scope.data), null, 2) |
| 102 | }; |
| 103 | }); |
| 104 | |
| 105 | this.applyScope = (scope) => { |
| 106 | |
| 107 | // let a = codeToString.toCode(scope.config); |
| 108 | // console.log(a); |
| 109 | const newScope = { |
| 110 | config: codeToString.toCode(scope.config), |
| 111 | data: eval(`(${scope.data})`) |
| 112 | }; |
| 113 | |
| 114 | $rootScope.$emit('uiTutorial.applyScope', newScope); |
| 115 | } |
| 116 | |
Matteo Scandolo | 4208f11 | 2016-08-10 17:02:02 -0700 | [diff] [blame] | 117 | } |
| 118 | }; |
| 119 | }); |