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