a17ebc9b10e9841b61174eb1260f5f3f538513a0
[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 notifyError = function(api, response) {
47                             Notification.error ({message: "Fail /api/afm-main" + api + "=" + scope.label + " RunID="+ scope.appID, delay: 5000});
48                             elem.addClass ("fail");
49                             elem.removeClass ("success");
50                             scope.callback (scope.appliID, api, response);
51                         };
52                         
53                         var notifySuccess = function (api, response) {
54                             elem.removeClass ("fail");
55                             scope.runID = response.data.response.runid;
56                             scope.callback (scope.appliID, "/api/afm-main/start", response);
57                         };
58                         
59                         var closeModal = function() {
60                             console.log ("Modal Closing");
61                             scope.modal.deactivate();
62                             $timeout (function() {scope.modal.destroy();}, 1000);
63                         };
64                         
65                         var actionModal = function(action) {
66                             console.log ("Modal Action=%s", action);
67                             switch (action) {
68                                 
69                                 case "start":
70                                     AppCall.get ("afm-main", "start", {id: scope.appliID}, function(response) {
71                                         if (response.status !== 200 || response.data.request.jtype !== "AJB_reply") {
72                                             notifyError ("start", response);
73                                             return;
74                                         }
75                                         
76                                         notifySuccess ("start", response);
77                                     });
78                                     break;
79                                     
80                                 case "stop":
81                                     AppCall.get ("afm-main", "stop", {id: scope.runID}, function(response) {
82                                         if (response.status !== 200 || response.data.request.jtype !== "AJB_reply") {
83                                             notifyError ("stop", response);
84                                             return;
85                                         }
86                                         
87                                         notifySuccess ("stop", response);
88                                     });
89                                     break;
90                                         
91                                 case "info":
92                                     AppCall.get ("afm-main", "detail", {id: scope.appID}, function(response) {
93                                         if (response.status !== 200 || response.data.request.jtype !== "AJB_reply") {
94                                             notifyError ("detail", response);
95                                             return;
96                                         }
97                                         
98                                         notifySuccess ("detail", response);
99                                     });
100                                     break;
101                                         
102                                     
103                                 default:
104                                     console.log ("ActionModal unknown action=[%s]", action);
105                                     break;
106                             }
107                             
108                             closeModal();
109                         };
110             
111                         // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
112                         var config = {
113                             animationIn: 'slideInFromTop',
114                             contentScope: {
115                                 action  : actionModal,
116                                 close   : closeModal,
117                                 icon    : scope.icon,
118                                 label   : scope.label
119                             }, template : tmplModal
120                         }; 
121                         // Popup Modal to render application data
122                         scope.modal = new ModalFactory(config);
123                         scope.modal.activate ();
124                     };
125
126                     // extract application information from AppID+Store
127                     if (attrs.handle && scope.store [attrs.handle].name) {
128                         scope.icon  = AppConfig.paths.icons + scope.store [attrs.handle].name.toLowerCase() + '-ico.png';
129                         scope.label = scope.store [attrs.handle].name;
130                         scope.appliID= attrs.handle;
131                     } else {
132                          scope.icon  = AppConfig.paths.icons + 'w3c-ico.png';
133                          scope.label = attrs.handle;
134                     }
135                                 
136                     // add label as class
137                     elem.addClass (scope.label.toLowerCase());
138                     
139                     // note: clicked in imported and when template is clicked
140                     // it will call clicked method passed in param.
141                 }
142                 
143                 return {
144                     restrict: 'E',
145                     template: tmplAppli,
146                     link: mymethods,
147                     scope: {callback: '=', store: '='}
148                 };
149             });
150 })();