First version
[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}}-ico.png>' +
26             '<span>{{label}}</span>' +
27             '</div>';
28     
29     var tmplModal = 
30             '<b class="close-button" ng-click="close()">×</b>' +
31             '<img ng-src="{{appicon}}-ico.png">' +
32             '<span class="modal-text">Application <b>{{appname}}</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 href="#"><i class="fi-x"> Stop</i></a></li>' +
36             '<li><a href="#"><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 stated
76                                         scope.callback (scope.appliID, "/api/afm-main/start", response);
77                                     });
78                                     break;
79                                     
80                                 case "stop":
81                                     break;
82                                     
83                                 default:
84                                     console.log ("ActionModal unknown action=[%s]", action);
85                                     break;
86                             }
87                             
88                             closeModal();
89                         };
90             
91                         // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
92                         var config = {
93                             animationIn: 'slideInFromTop',
94                             contentScope: {
95                                 action  : actionModal,
96                                 close   : closeModal,
97                                 appicon : scope.icon,
98                                 appname : scope.label,
99                             }, template : tmplModal
100                         }; 
101                         // Popup Modal to render application data
102                         scope.modal = new ModalFactory(config);
103                         scope.modal.activate ();
104                     };
105
106                     // extract application information from AppID+Store
107                     if (attrs.handle && scope.store [attrs.handle].name) {
108                         scope.icon  = AppConfig.paths.icons + scope.store [attrs.handle].name.toLowerCase();
109                         scope.label = scope.store [attrs.handle].name;
110                         scope.appliID= attrs.handle;
111                     } else {
112                          scope.icon  = AppConfig.paths.icons + 'w3c-ico.png';
113                          scope.label = attrs.handle;
114                     }
115                                 
116                     // add label as class
117                     elem.addClass (scope.label.toLowerCase());
118                     
119                     // note: clicked in imported and when template is clicked
120                     // it will call clicked method passed in param.
121                 }
122                 
123                 return {
124                     restrict: 'E',
125                     template: tmplAppli,
126                     link: mymethods,
127                     scope: {callback: '=', store: '='}
128                 };
129             });
130 })();