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