Cosmetic fixes
[src/app-framework-demo.git] / afm-client / app / Frontend / widgets / ActionButtons / AppliButton.js
1 /* 
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  * 
18  * Bugs: Input with Callback SHOULD BE get 'required' class
19  */
20
21 (function () {
22     'use strict';
23
24     var tmplAppli = '<div  ng-click="clicked()">' +
25             '<img ng-src="{{icon}}">' +
26             '<span>{{label}}</span>' +
27             '</div>';
28     
29     var tmplModal = 
30             '<b class="close-button" ng-click="close()">×</b>' +
31             '<img ng-src="{{icon}}">' +
32             '<span class="modal-text">Application <b>{{name}}</b></span>' +
33             '<ul class="vertical icon-left primary menu-bar">' +
34             '<li><a ng-click=action("start")><i class="fi-check"> Start</i></a></li>' +
35             '<li><a ng-click=action("stop")><i class="fi-x"> Stop</i></a></li>' +
36             '<li><a ng-click=action("info")><i class="fi-info"> Info</i></a></li>' +
37             '</ul>' +
38             '';
39
40     angular.module('AppliButton', [])
41             .directive('appliButton', function (AppConfig, AppCall, ModalFactory, Notification, $timeout) {
42
43                 function mymethods(scope, elem, attrs) {
44                     scope.clicked = function () {
45                         
46                         var closeModal = function() {
47                             console.log ("Modal Closing");
48                             scope.modal.deactivate();
49                             $timeout (function() {scope.modal.destroy();}, 1000);
50                         };
51                         
52                         var actionModal = function(action) {
53                             console.log ("Modal Action=%s", action);
54                             switch (action) {
55                                 
56                                 case "start":
57                                     AppCall.get ("afm-main", "start", {id: scope.appliID}, function(response) {
58                                         if (response.status !== 200) {
59                                             Notification.error ({message: "Fail to start application=" + scope.label +" ID="+ scope.appliID, delay: 5000});
60                                             elem.addClass ("fail");
61                                             elem.removeClass ("success");
62                                             scope.callback (scope.appliID, "/api/afm-main/start", response);
63                                             return;
64                                         }
65
66                                         // Check this is a valid response from Binder
67                                         if (response.data.request.jtype !== "AJB_reply" && response.data.request.api !== "start") {
68                                             Notification.error ({message: "Invalid Respond to /opa/afm-main/start response.data="+response.data, delay: 5000}); 
69                                             elem.addClass ("fail");
70                                             elem.removeClass ("success");
71                                             scope.callback (scope.appliID, "/api/afm-main/start", response);
72                                             return;
73                                         }
74                                         
75                                         // Application was started
76                                         elem.addClass ("success");
77                                         elem.removeClass ("fail");
78                                         scope.runID = response.data.response.runid;
79                                         scope.callback (scope.appliID, "/api/afm-main/start", response);
80                                     });
81                                     break;
82                                     
83                                 case "stop":
84                                     break;
85                                     
86                                 default:
87                                     console.log ("ActionModal unknown action=[%s]", action);
88                                     break;
89                             }
90                             
91                             closeModal();
92                         };
93             
94                         // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
95                         var config = {
96                             animationIn: 'slideInFromTop',
97                             contentScope: {
98                                 action  : actionModal,
99                                 close   : closeModal,
100                                 icon    : scope.icon,
101                                 label   : scope.label
102                             }, template : tmplModal
103                         }; 
104                         // Popup Modal to render application data
105                         scope.modal = new ModalFactory(config);
106                         scope.modal.activate ();
107                     };
108
109                     // extract application information from AppID+Store
110                     if (attrs.handle && scope.store [attrs.handle].name) {
111                         scope.icon  = AppConfig.paths.icons + scope.store [attrs.handle].name.toLowerCase() + '-ico.png';
112                         scope.label = scope.store [attrs.handle].name;
113                         scope.appliID= attrs.handle;
114                     } else {
115                          scope.icon  = AppConfig.paths.icons + 'w3c-ico.png';
116                          scope.label = attrs.handle;
117                     }
118                                 
119                     // add label as class
120                     elem.addClass (scope.label.toLowerCase());
121                     
122                     // note: clicked in imported and when template is clicked
123                     // it will call clicked method passed in param.
124                 }
125                 
126                 return {
127                     restrict: 'E',
128                     template: tmplAppli,
129                     link: mymethods,
130                     scope: {callback: '=', store: '='}
131                 };
132             });
133 })();