Clean up to prepare new version of API
[src/app-framework-demo.git] / afm-client / app / Frontend / widgets / ActionButtons / AppliButton.js
index 0ed0415..a10030c 100644 (file)
             '<b class="close-button" ng-click="close()">×</b>' +
             '<img ng-src="{{icon}}">' +
             '<span class="modal-text">Application <b>{{label}}</b></span>' +
-            '<ul class="vertical icon-left primary menu-bar">' +
-            '<li><a ng-click=action("start")><i class="fi-check"> Start</i></a></li>' +
-            '<li><a ng-click=action("stop")><i class="fi-x"> Stop</i></a></li>' +
+            '<ul class="vertical icon-left primary menu-bar appli-menu-start">' +
+            '<li class=start-{{runstatus}}><a ng-click=action("start")><i class="fi-check"> Start</i></a></li>' +
+            '<li class=stop-{{runstatus}}><a ng-click=action("stop")><i class="fi-x"> Stop</i></a></li>' +
             '<li><a ng-click=action("info")><i class="fi-info"> Info</i></a></li>' +
-            '<li><a ng-click=action("uninstall")><i class="fi-x"> Uninstall</i></a></li>' +
+            '<li class=start-{{runstatus}}><a ng-click=action("uninstall")><i class="fi-x"> Uninstall</i></a></li>' +
             '</ul>' +
             '';
     
@@ -42,7 +42,7 @@
             '<b class="close-button" ng-click="close()">×</b>' +
             '<img ng-src="{{icon}}">' +
             '<span class="modal-text">Application <b>{{label}}</b></span>' +
-            '<ul class="vertical icon-left">' +
+            '<ul class="vertical icon-left appli-menu-info">' +
             '<li><i class="fi-paperclip"> Name : {{detail.name}} </i></li>' +
             '<li><i class="fi-info"> Description {{detail.description}}</i></li>' +
             '<li><i class="fi-torso"> Author : {{detail.author}}</i></li>' +
             '';
 
     angular.module('AppliButton', [])
-            .directive('appliButton', function (AppConfig, AppCall, ModalFactory, Notification, $timeout) {
+            .directive('appliButton', function (AppConfig, AppCall, ModalFactory, Notification, $timeout, $window, $location, urlquery) {
 
                 function mymethods(scope, elem, attrs) {
+                    scope.runstatus = "stop";
+                    scope.runmode   = urlquery.runmode || "auto";
                     scope.clicked = function () {
 
-                        var notifyError = function(api, response) {
-                            Notification.error ({message: "Fail /api/afm-main" + api + "=" + scope.label + " RunID="+ scope.appID, delay: 5000});
+                        var notifyError = function(action, jresp) {
+                            Notification.error ({message: "Fail /api/afm-main" + action + "=" + scope.label + " RunID="+ scope.appID, delay: 5000});
                             elem.addClass ("fail");
                             elem.removeClass ("success");
-                            scope.callback (scope.appID, api, response);
+                            scope.callback (scope.appID, action, jresp);
                         };
                         
-                        var notifySuccess = function (api, response) {
+                        var notifySuccess = function (action, jresp) {
                             elem.removeClass ("fail");
-                            scope.runID = response.data.response.runid;
-                            scope.callback (scope.appID, "/api/afm-main/start", response);
+                            scope.runID = jresp.response.runid;
+                            scope.callback (scope.appID, action, jresp);
                         };
                         
-                        var closeModal = function() {
-                            console.log ("Modal Closing");
-                            scope.modal.deactivate();
-                            $timeout (function() {scope.modal.destroy();}, 1000);
+                        var closeModApp = function() {
+                            scope.modApp.deactivate();
+                            $timeout (function() {scope.modApp.destroy();}, 1000);
+                        };
+                        
+                        var closeModInfo = function() {
+                            scope.modInfo.deactivate();
+                            $timeout (function() {scope.modInfo.destroy();}, 1000);
                         };
                         
                         var actionModal = function(action) {
                             switch (action) {
                                 
                                 case "start":
-                                    AppCall.get ("afm-main", "start", {id: scope.appID}, function(response) {
-                                        if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
-                                            notifyError ("start", response);
+                                    if (scope.runstatus !== "stop") return;
+                                    AppCall.get ("afm-main", "start", {id: scope.appID, mode: scope.runmode}, function(jresp, errcode) {
+                                        if (errcode !== 200 || jresp.jtype !== "afb-reply") {
+                                            notifyError ("start", jresp);
                                             return;
                                         }
-                                        
-                                        notifySuccess ("start", response);
+                                        scope.runstatus="start";
+                                        notifySuccess (action, jresp);
+                                       if(response.data.response.uri)
+                                            scope.winapp= $window.open(response.data.response.uri.replace("%h", $location.host()));                                            
                                     });
                                     break;
                                     
                                 case "stop":
-                                    AppCall.get ("afm-main", "terminate", {runid: scope.runID}, function(response) {
-                                        if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
-                                            notifyError ("stop", response);
+                                    if (scope.runstatus !== "start") return;
+                                    
+                                    AppCall.get ("afm-main", "terminate", {runid: scope.runID}, function(jresp, errcode) {
+                                        if (errcode !== 200 || jresp.jtype !== "afb-reply") {
+                                            notifyError ("stop", jresp);
                                             return;
                                         }
+                                        scope.runstatus="stop";
                                         
-                                        notifySuccess ("stop", response);
+                                        // if a remote window app was open let's close it
+                                        if (scope.winapp) {
+                                           console.log ("Closing Application Window label=%s id=%s", scope.label, scope.appID);
+                                           scope.winapp.close();
+                                           scope.winapp=false;
+                                        }
+                                        notifySuccess (action, jresp);
                                     });
                                     break;
                                         
                                 case "info":
-                                    AppCall.get ("afm-main", "detail", {id: scope.appID}, function(response) {
-                                        if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
-                                            notifyError ("detail", response);
+                                    AppCall.get ("afm-main", "detail", {id: scope.appID}, function(jresp, errcode) {
+                                        if (errcode !== 200 || jresp.jtype !== "afb-reply") {
+                                            notifyError ("detail", jresp);
                                             return;
                                         }
                                                                                
                                         var config = {
                                             animationIn: 'slideInFromTop',
                                             contentScope: {
-                                                close   : closeModal,
+                                                close   : closeModInfo,
                                                 icon    : scope.icon,
                                                 label   : scope.appID,
                                                 detail  : response.data.response
                                             }, template : tmplDetail
                                         }; 
                                         // Popup Modal to render application data
-                                        scope.modal = new ModalFactory(config);
-                                        scope.modal.activate ();
+                                        scope.modInfo = new ModalFactory(config);
+                                        scope.modInfo.activate ();
 
                                     });
                                     break;
 
                                 case "uninstall":
-                                    AppCall.get ("afm-main", "uninstall", {id: scope.appID}, function(response) {
-                                        if (response.status !== 200 || response.data.jtype !== "AJB_reply") {
-                                            notifyError ("uninstall", response);
+                                    if (scope.runstatus !== "stop") return;
+                                    AppCall.get ("afm-main", "uninstall", {id: scope.appID}, function(jresp, errcode) {
+                                        if (errcode !== 200 || jresp.jtype !== "afb-reply") {
+                                            notifyError ("uninstall", jresp);
                                             return;
                                         }
                                         
-                                        notifySuccess ("uninstall", response);
+                                        notifySuccess (action, jresp);
                                     });
                                     break;
 
                                     break;
                             }
                             
-                            closeModal();
+                            closeModApp();
                         };
             
                         // reference http://foundation.zurb.com/apps/docs/#!/angular-modules
                         var config = {
                             animationIn: 'slideInFromTop',
                             contentScope: {
-                                action  : actionModal,
-                                close   : closeModal,
-                                icon    : scope.icon,
-                                label   : scope.label
-                            }, template : tmplModal
+                                action   : actionModal,
+                                runstatus: scope.runstatus,
+                                close    : closeModApp,
+                                icon     : scope.icon,
+                                label    : scope.label
+                            }, template  : tmplModal
                         }; 
                         // Popup Modal to render application data
-                        scope.modal = new ModalFactory(config);
-                        scope.modal.activate ();
+                        scope.modApp = new ModalFactory(config);
+                        scope.modApp.activate ();
                     };
 
                     // extract application information from AppID+Store
                     if (attrs.handle && scope.store [attrs.handle].name) {
-                        scope.icon  = AppConfig.paths.icons + scope.store [attrs.handle].name.toLowerCase() + '-ico.png';
+                        scope.icon  = AppConfig.paths.icons + attrs.handle; //scope.store [attrs.handle].name.toLowerCase() + '-ico.png';
                         scope.label = scope.store [attrs.handle].name;
                         scope.appID= attrs.handle;
                     } else {