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