Started tutorial implementation
Change-Id: I1624d012735a090c4e7f89d7d79c210ee3617942
diff --git a/views/ngXosViews/UITutorial/src/css/dev.css b/views/ngXosViews/UITutorial/src/css/dev.css
new file mode 100644
index 0000000..a0d1a33
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/css/dev.css
@@ -0,0 +1,5 @@
+#xosUITutorial{
+ position: absolute;
+ top: 100px;
+ left: 200px;
+}
\ No newline at end of file
diff --git a/views/ngXosViews/UITutorial/src/css/main.css b/views/ngXosViews/UITutorial/src/css/main.css
new file mode 100644
index 0000000..3684934
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/css/main.css
@@ -0,0 +1,20 @@
+#xosUITutorial {
+ width: 100%; }
+ #xosUITutorial js-shell {
+ display: block; }
+ #xosUITutorial js-shell #shell-panel {
+ width: 100%;
+ height: 400px;
+ background: #002b36;
+ padding: 10px;
+ overflow: scroll; }
+ #xosUITutorial js-shell #shell-panel .prompt {
+ color: #d33682; }
+ #xosUITutorial js-shell #shell-panel .cursor {
+ color: #2aa198; }
+ #xosUITutorial js-shell #shell-panel .error {
+ color: #dc322f; }
+ #xosUITutorial js-shell #shell-panel #shell-view {
+ color: #93a1a1; }
+ #xosUITutorial js-shell #shell-panel #shell-view > div > div {
+ color: #268bd2; }
diff --git a/views/ngXosViews/UITutorial/src/icons/README.md b/views/ngXosViews/UITutorial/src/icons/README.md
new file mode 100644
index 0000000..0d5a787
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/icons/README.md
@@ -0,0 +1,18 @@
+# Set icon for custom dashboard
+
+## Path to add images : <dashboard-name>/src/icons
+
+### Add two images with name in following format
+ <dashboard-name>-icon.png
+ <dashboard-name>-icon-active.png
+
+Examples:
+ If your dashboard name is `slice`
+ then you will add the images with the name as
+ `slice-icon.png`
+ `slice-icon-active.png`
+
+
+
+
+Note: If you don't add the custom images, then dashboard will use the default images stored at `/xos/core/xoslib/static/images`
\ No newline at end of file
diff --git a/views/ngXosViews/UITutorial/src/index.html b/views/ngXosViews/UITutorial/src/index.html
new file mode 100644
index 0000000..e790d6e
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/index.html
@@ -0,0 +1,42 @@
+<!-- browserSync -->
+<!-- bower:css -->
+<link rel="stylesheet" href="vendor/bootstrap-css/css/bootstrap.min.css" />
+<link rel="stylesheet" href="vendor/angular-chart.js/dist/angular-chart.css" />
+<!-- endbower -->
+<!-- endcss -->
+<!-- inject:css -->
+<link rel="stylesheet" href="/css/dev.css">
+<link rel="stylesheet" href="/css/main.css">
+<link rel="stylesheet" href="/../../../xos/core/static/xosNgLib.css">
+<!-- endinject -->
+
+<div ng-app="xos.UITutorial" id="xosUITutorial" class="container-fluid">
+ <div ui-view></div>
+</div>
+
+<!-- bower:js -->
+<script src="vendor/jquery/dist/jquery.js"></script>
+<script src="vendor/lodash/lodash.js"></script>
+<script src="vendor/josh.js/js/readline.js"></script>
+<script src="vendor/josh.js/js/history.js"></script>
+<script src="vendor/josh.js/js/killring.js"></script>
+<script src="vendor/josh.js/js/input.js"></script>
+<script src="vendor/josh.js/js/pathhandler.js"></script>
+<script src="vendor/josh.js/js/shell.js"></script>
+<script src="vendor/angular/angular.js"></script>
+<script src="vendor/angular-mocks/angular-mocks.js"></script>
+<script src="vendor/angular-ui-router/release/angular-ui-router.js"></script>
+<script src="vendor/angular-cookies/angular-cookies.js"></script>
+<script src="vendor/angular-animate/angular-animate.js"></script>
+<script src="vendor/angular-resource/angular-resource.js"></script>
+<script src="vendor/bootstrap-css/js/bootstrap.min.js"></script>
+<script src="vendor/Chart.js/Chart.js"></script>
+<script src="vendor/angular-chart.js/dist/angular-chart.js"></script>
+<script src="vendor/d3/d3.js"></script>
+<script src="vendor/angular-recursion/angular-recursion.js"></script>
+<!-- endbower -->
+<!-- endjs -->
+<!-- inject:js -->
+<script src="/vendor/ng-xos-lib/dist/ngXosHelpers.min.js"></script>
+<script src="/.tmp/main.js"></script>
+<!-- endinject -->
\ No newline at end of file
diff --git a/views/ngXosViews/UITutorial/src/js/main.js b/views/ngXosViews/UITutorial/src/js/main.js
new file mode 100644
index 0000000..818d4f5
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/js/main.js
@@ -0,0 +1,170 @@
+'use strict';
+
+angular.module('xos.UITutorial', [
+ 'ngResource',
+ 'ngCookies',
+ 'ui.router',
+ 'xos.helpers'
+])
+.config(($stateProvider) => {
+ $stateProvider
+ .state('shell', {
+ url: '/',
+ template: '<js-shell></js-shell>'
+ });
+})
+.config(function($httpProvider){
+ $httpProvider.interceptors.push('NoHyperlinks');
+})
+.directive('jsShell', function(){
+ return {
+ restrict: 'E',
+ scope: {},
+ bindToController: true,
+ controllerAs: 'vm',
+ templateUrl: 'templates/js-shell.tpl.html',
+ controller: function($injector){
+ var history = new Josh.History({ key: 'helloworld.history'});
+ var shell = Josh.Shell({history: history});
+
+ shell.onNewPrompt(function(callback) {
+ callback('[ngXosLib] $ ');
+ });
+
+ const errorHandler = (msg, done) => {
+ const errorTpl = _.template(`<span class="error">[ERROR] <%= msg %></span>`);
+ done(errorTpl({msg: msg}));
+ }
+
+ const parseResponse = (res, done) => {
+
+ // TODO @Arpit format res (it can be an array or an object),
+ // it is better if the output is a valid JSON (that we can copy and paste)
+ // TODO handle 204/No-Content response
+ if(angular.isArray(res)){
+ res = res.map(i => {
+ return JSON.stringify(i, ['id', 'name', 'max_instances'], 2) + '<br/>';
+ });
+ }
+ else {
+ res = JSON.stringify(res, ['id', 'name', 'max_instances'], 2);
+ }
+
+ done(res);
+ }
+
+ const getAvailableResources = () => {
+ return angular.module('xos.helpers')._invokeQueue
+ .filter((d) => {
+ if(d[1] !== 'service'){
+ return false;
+ }
+ const serviceDeps = d[2][1];
+ return serviceDeps.indexOf('$resource') !== -1;
+ })
+ .reduce((list, i) => list.concat([i[2][0]]), []);
+ }
+
+ const listAvailableResources = (done) => {
+ const resources = getAvailableResources()
+ .reduce((html, i) => `${html}${i}<br/>`, '');
+ done(resources);
+ }
+
+ const consumeResource = (resourceName, method, args, done) => {
+
+ if(getAvailableResources().indexOf(resourceName) === -1){
+ return errorHandler(`Resource "${resourceName}" does not exists`, done);
+ }
+
+ if(['query', 'get', 'save', '$save', 'delete'].indexOf(method) === -1){
+ return errorHandler(`Method "${method}" not allowed`, done);
+ }
+
+ let Resource;
+ try{
+ Resource = $injector.get(resourceName);
+
+ // TODO @Teo if get/delete check for arguments
+ let params = {};
+
+ // if the method require arguments checks for them
+ if(['get', '$save', 'delete'].indexOf(method) !== -1){
+ if(args.length === 0){
+ return errorHandler(`Method "${method}" require parameters`, done);
+ }
+ }
+
+ // if there are arguments parse them
+ if(args.length > 0){
+ params = eval(`(${args[0]})`);
+ }
+
+ // if it is a query is not possible to use id as parameter
+ if(method === 'query' && angular.isDefined(params.id)){
+ return errorHandler(`Is not possible to use "id" as filter in method "${method}", use "get" instead!`, done);
+ }
+
+ Resource[method](params).$promise
+ .then(res => {
+ return parseResponse(res, done);
+ });
+ }
+ catch(e){
+ console.log(e);
+ return errorHandler(`Failed to inject resource "${resourceName}"`, done);
+ }
+ }
+
+ shell.setCommandHandler('resource', {
+ exec: (cmd, args, done) => {
+ switch(args[0]){
+ case 'list':
+ return listAvailableResources(done);
+ break;
+ default:
+ // use the resource
+ const resourceName = args.shift();
+ const method = args.shift();
+ return consumeResource(resourceName, method, args, done);
+ }
+ },
+ completion: function(cmd, arg, line, callback) {
+ const args = ['list'].concat(getAvailableResources());
+ if(line.text.match(/resource\s[A-Z][a-z]+\s/)){
+ // if arg is a resource, then return available methods
+ if(args.indexOf(arg) !== -1){
+ arg = '';
+ }
+ const methods = ['query', 'get', 'save', '$save', 'delete'];
+ return callback(shell.bestMatch(arg, methods));
+ }
+ return callback(shell.bestMatch(arg, args));
+ }
+ })
+
+ shell.setCommandHandler('slices', {
+ exec: (cmd, args, done) => {
+ Slices.query().$promise
+ .then(res => {
+ res = res.map(i => {
+ return JSON.stringify(i, ['id', 'name', 'max_instances'], 2);
+ });
+
+ res = res.reduce((response, item) => {
+ return `${response},<br>${item}`;
+ });
+
+ done(res);
+ })
+ .catch(err => {
+ console.log(err);
+ done('An error occurred');
+ })
+ }
+ });
+
+ shell.activate();
+ }
+ };
+});
\ No newline at end of file
diff --git a/views/ngXosViews/UITutorial/src/sass/main.scss b/views/ngXosViews/UITutorial/src/sass/main.scss
new file mode 100644
index 0000000..83ea377
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/sass/main.scss
@@ -0,0 +1,53 @@
+@import '../../../../style/sass/lib/_variables.scss';
+
+// The solarized palette
+
+$background: #002b36;
+$hl: #073642;
+$comment: #586e75;
+$foreground: #839496;
+$emph: #93a1a1;
+
+$yellow: #b58900;
+$orange: #cb4b16;
+$red: #dc322f;
+$magenta: #d33682;
+$violet: #6c71c4;
+$blue: #268bd2;
+$cyan: #2aa198;
+$green: #859900;
+
+#xosUITutorial {
+ width: 100%;
+ js-shell {
+ display: block;
+
+ #shell-panel {
+ width: 100%;
+ height: 400px;
+ background: $background;
+ padding: 10px;
+ overflow: scroll;
+
+ .prompt {
+ color: $magenta;
+ }
+
+ .cursor {
+ color: $cyan;
+ }
+
+ .error {
+ color: $red;
+ }
+
+ #shell-view {
+ color: $emph;
+
+ >div>div{
+ color: $blue;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/views/ngXosViews/UITutorial/src/templates/js-shell.tpl.html b/views/ngXosViews/UITutorial/src/templates/js-shell.tpl.html
new file mode 100644
index 0000000..543006d
--- /dev/null
+++ b/views/ngXosViews/UITutorial/src/templates/js-shell.tpl.html
@@ -0,0 +1,3 @@
+<div id="shell-panel">
+ <div id="shell-view"></div>
+</div>
\ No newline at end of file