e51b3b6bfa94c3424acbb50a7350b74637a242ea
[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>{{label}}</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             '<li><a ng-click=action("uninstall")><i class="fi-x"> Uninstall</i></a></li>' +
38             '</ul>' +
39             '';
40     
41     var tmplDetail = 
42             '<b class="close-button" ng-click="close()">×</b>' +
43             '<img ng-src="{{icon}}">' +
44             '<span class="modal-text">Application <b>{{label}}</b></span>' +
45             '<ul class="vertical icon-left">' +
46             '<li><i class="fi-paperclip"> Name : {{detail.name}} </i></li>' +
47             '<li><i class="fi-info"> Description {{detail.description}}</i></li>' +
48             '<li><i class="fi-torso"> Author : {{detail.author}}</i></li>' +
49             '</ul>' +
50             '';
51
52     angular.module('AppliButton', [])
53             .directive('appliButton', function (AppConfig, AppCall, ModalFactory, Notification, $timeout) {
54
55                 function mymethods(scope, elem, attrs) {
56                     scope.clicked = function () {
57
58                         var notifyError = function(api, response) {
59                             Notification.error ({message: "Fail /api/afm-main" + api + "=" + scope.label + " RunID="+ scope.appID, delay: 5000});
60                             elem.addClass ("fail");
61                             elem.removeClass ("success");
62                             scope.callback (scope.appID, api, response);
63                         };
64                         
65                         var notifySuccess = function (api, response) {
66                             elem.removeClass ("fail");
67                             scope.runID = response.data.response.runid;
68                             scope.callback (scope.appID, "/api/afm-main/start", response);
69                         };
70                         
71                         var closeModal = function() {
72                             console.log ("Modal Closing");
73                             scope.modal.deactivate();
74                             $timeout (function() {scope.modal.destroy();}, 1000);
75                         };
76                         
77                         var actionModal = function(action) {
78                             console.log ("Modal Action=%s", action);
79                             switch (action) {
80                                 
81                                 case "start":
82                                     AppCall.get ("afm-main", "start", {id: scope.appID}, function(response) {
83                                         if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
84                                             notifyError ("start", response);
85                                             return;
86                                         }
87                                         
88                                         notifySuccess ("start", response);
89                                     });
90                                     break;
91                                     
92                                 case "stop":
93                                     AppCall.get ("afm-main", "terminate", {runid: scope.runID}, function(response) {
94                                         if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
95                                             notifyError ("stop", response);
96                                             return;
97                                         }
98                                         
99                                         notifySuccess ("stop", response);
100                                     });
101                                     break;
102                                         
103                                 case "info":
104                                     AppCall.get ("afm-main", "detail", {id: scope.appID}, function(response) {
105                                         if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
106                                             notifyError ("detail", response);
107                                             return;
108                                         }
109                                                                                
110                                         // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
111                                         var config = {
112                                             animationIn: 'slideInFromTop',
113                                             contentScope: {
114                                                 close   : closeModal,
115                                                 icon    : scope.icon,
116                                                 label   : scope.appID,
117                                                 detail  : response.data.response
118                                             }, template : tmplDetail
119                                         }; 
120                                         // Popup Modal to render application data
121                                         scope.modal = new ModalFactory(config);
122                                         scope.modal.activate ();
123
124                                     });
125                                     break;
126
127                                 case "uninstall":
128                                     AppCall.get ("afm-main", "uninstall", {id: scope.appID}, function(response) {
129                                         if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
130                                             notifyError ("uninstall", response);
131                                             return;
132                                         }
133                                         
134                                         notifySuccess ("uninstall", response);
135                                     });
136                                     break;
137
138                                 default:
139                                     console.log ("ActionModal unknown action=[%s]", action);
140                                     break;
141                             }
142                             
143                             closeModal();
144                         };
145             
146                         // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
147                         var config = {
148                             animationIn: 'slideInFromTop',
149                             contentScope: {
150                                 action  : actionModal,
151                                 close   : closeModal,
152                                 icon    : scope.icon,
153                                 label   : scope.label
154                             }, template : tmplModal
155                         }; 
156                         // Popup Modal to render application data
157                         scope.modal = new ModalFactory(config);
158                         scope.modal.activate ();
159                     };
160
161                     // extract application information from AppID+Store
162                     if (attrs.handle && scope.store [attrs.handle].name) {
163                         scope.icon  = AppConfig.paths.icons + attrs.handle; //scope.store [attrs.handle].name.toLowerCase() + '-ico.png';
164                         scope.label = scope.store [attrs.handle].name;
165                         scope.appID= attrs.handle;
166                     } else {
167                          scope.icon  = AppConfig.paths.icons + 'w3c-ico.png';
168                          scope.label = attrs.handle;
169                     }
170                                 
171                     // add label as class
172                     elem.addClass (scope.label.toLowerCase());
173                     
174                     // note: clicked in imported and when template is clicked
175                     // it will call clicked method passed in param.
176                 }
177                 
178                 return {
179                     restrict: 'E',
180                     template: tmplAppli,
181                     link: mymethods,
182                     scope: {callback: '=', store: '='}
183                 };
184             });
185 })();