2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
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.
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.
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/>.
18 * Bugs: Input with Callback SHOULD BE get 'required' class
24 var tmplAppli = '<div ng-click="clicked()">' +
25 '<img ng-src="{{icon}}">' +
26 '<span>{{label}}</span>' +
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 appli-menu-start">' +
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>' +
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 appli-menu-info">' +
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>' +
52 angular.module('AppliButton', [])
53 .directive('appliButton', function (AppConfig, AppCall, ModalFactory, Notification, $timeout, $window, $location, urlquery) {
55 function mymethods(scope, elem, attrs) {
56 scope.runstatus = "stop";
57 scope.runmode = urlquery.runmode || "auto";
58 scope.clicked = function () {
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);
67 var notifySuccess = function (action, response) {
68 elem.removeClass ("fail");
69 scope.runID = response.data.response.runid;
70 scope.callback (scope.appID, action, response);
73 var closeModApp = function() {
74 scope.modApp.deactivate();
75 $timeout (function() {scope.modApp.destroy();}, 1000);
78 var closeModInfo = function() {
79 scope.modInfo.deactivate();
80 $timeout (function() {scope.modInfo.destroy();}, 1000);
83 var actionModal = function(action) {
84 console.log ("Modal Action=%s", action);
88 if (scope.runstatus !== "stop") return;
89 AppCall.get ("afm-main", "start", {id: scope.appID, mode: scope.runmode}, function(response) {
90 if (response.status !== 200 || response.data.jtype !== "afb-reply") {
91 notifyError ("start", response);
94 scope.runstatus="start";
95 notifySuccess (action, response);
96 if(response.data.response.uri)
97 scope.winapp= $window.open(response.data.response.uri.replace("%h", $location.host()));
102 if (scope.runstatus !== "start") return;
104 AppCall.get ("afm-main", "terminate", {runid: scope.runID}, function(response) {
105 if (response.status !== 200 || response.data.jtype !== "afb-reply") {
106 notifyError ("stop", response);
109 scope.runstatus="stop";
111 // if a remote window app was open let's close it
113 console.log ("Closing Application Window label=%s id=%s", scope.label, scope.appID);
114 scope.winapp.close();
117 notifySuccess (action, response);
122 AppCall.get ("afm-main", "detail", {id: scope.appID}, function(response) {
123 if (response.status !== 200 || response.data.jtype !== "afb-reply") {
124 notifyError ("detail", response);
128 // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
130 animationIn: 'slideInFromTop',
132 close : closeModInfo,
135 detail : response.data.response
136 }, template : tmplDetail
138 // Popup Modal to render application data
139 scope.modInfo = new ModalFactory(config);
140 scope.modInfo.activate ();
146 if (scope.runstatus !== "stop") return;
147 AppCall.get ("afm-main", "uninstall", {id: scope.appID}, function(response) {
148 if (response.status !== 200 || response.data.jtype !== "afb-reply") {
149 notifyError ("uninstall", response);
153 notifySuccess (action, response);
158 console.log ("ActionModal unknown action=[%s]", action);
165 // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
167 animationIn: 'slideInFromTop',
169 action : actionModal,
170 runstatus: scope.runstatus,
174 }, template : tmplModal
176 // Popup Modal to render application data
177 scope.modApp = new ModalFactory(config);
178 scope.modApp.activate ();
181 // extract application information from AppID+Store
182 if (attrs.handle && scope.store [attrs.handle].name) {
183 scope.icon = AppConfig.paths.icons + attrs.handle; //scope.store [attrs.handle].name.toLowerCase() + '-ico.png';
184 scope.label = scope.store [attrs.handle].name;
185 scope.appID= attrs.handle;
187 scope.icon = AppConfig.paths.icons + 'w3c-ico.png';
188 scope.label = attrs.handle;
191 // add label as class
192 elem.addClass (scope.label.toLowerCase());
194 // note: clicked in imported and when template is clicked
195 // it will call clicked method passed in param.
202 scope: {callback: '=', store: '='}