blob: b47713d731eaca14c25af62247912184e439778c [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo4208f112016-08-10 17:02:02 -070019'use strict';
20
21angular.module('xos.UITutorial', [
22 'ngResource',
23 'ngCookies',
24 'ui.router',
Matteo Scandoloa81496a2016-08-16 17:29:42 -070025 'xos.helpers',
26 'ui.ace'
Matteo Scandolo4208f112016-08-10 17:02:02 -070027])
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 Scandoloa81496a2016-08-16 17:29:42 -070038.directive('jsShell', function($rootScope, TemplateHandler, codeToString){
Matteo Scandolo4208f112016-08-10 17:02:02 -070039 return {
40 restrict: 'E',
41 scope: {},
42 bindToController: true,
43 controllerAs: 'vm',
44 templateUrl: 'templates/js-shell.tpl.html',
Matteo Scandoloa81496a2016-08-16 17:29:42 -070045 controller: function(ExploreCmd, PlayCmd, LearnCmd){
Matteo Scandoloef969922016-08-11 13:49:12 -070046 var history = new Josh.History({ key: 'jsshell.history'});
47 this.shell = Josh.Shell({history: history});
Matteo Scandolo4208f112016-08-10 17:02:02 -070048
Matteo Scandoloef969922016-08-11 13:49:12 -070049 this.shell.onNewPrompt(done => {
Matteo Scandolo86f3f282016-08-11 11:21:33 -070050 done('[ngXosLib] $ ');
Matteo Scandolo4208f112016-08-10 17:02:02 -070051 });
52
Matteo Scandoloef969922016-08-11 13:49:12 -070053 this.shell.setCommandHandler('explore', {
Matteo Scandolo4208f112016-08-10 17:02:02 -070054 exec: (cmd, args, done) => {
Matteo Scandoloef969922016-08-11 13:49:12 -070055 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 Scandolo4208f112016-08-10 17:02:02 -070064 }
65 });
66
Arpit Agarwal45fd7052016-08-16 09:33:22 -070067 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 Scandoloa81496a2016-08-16 17:29:42 -070080
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 Agarwal45fd7052016-08-16 09:33:22 -070093
Matteo Scandoloef969922016-08-11 13:49:12 -070094 this.shell.activate();
Matteo Scandoloa81496a2016-08-16 17:29:42 -070095
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 Scandolo4208f112016-08-10 17:02:02 -0700117 }
118 };
119});