/*
* Copyright (C) 2015 "IoT.bzh"
* Author "Fulup Ar Foll"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Bugs: Input with Callback SHOULD BE get 'required' class
*/
(function () {
'use strict';
var tmplAppli = '
' +
'
' +
'
{{label}}' +
'
';
var tmplModal =
'×' +
'' +
'Application {{label}}' +
'' +
'';
var tmplDetail =
'×' +
'' +
'Application {{label}}' +
'' +
'- Name : {{detail.name}}
' +
'- Description {{detail.description}}
' +
'- Author : {{detail.author}}
' +
'
' +
'';
angular.module('AppliButton', [])
.directive('appliButton', function (AppConfig, AppCall, ModalFactory, Notification, $timeout) {
function mymethods(scope, elem, attrs) {
scope.clicked = function () {
var notifyError = function(api, response) {
Notification.error ({message: "Fail /api/afm-main" + api + "=" + scope.label + " RunID="+ scope.appID, delay: 5000});
elem.addClass ("fail");
elem.removeClass ("success");
scope.callback (scope.appID, api, response);
};
var notifySuccess = function (api, response) {
elem.removeClass ("fail");
scope.runID = response.data.response.runid;
scope.callback (scope.appID, "/api/afm-main/start", response);
};
var closeModal = function() {
console.log ("Modal Closing");
scope.modal.deactivate();
$timeout (function() {scope.modal.destroy();}, 1000);
};
var actionModal = function(action) {
console.log ("Modal Action=%s", 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);
return;
}
notifySuccess ("start", response);
});
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);
return;
}
notifySuccess ("stop", response);
});
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);
return;
}
// reference http://foundation.zurb.com/apps/docs/#!/angular-modules
var config = {
animationIn: 'slideInFromTop',
contentScope: {
close : closeModal,
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 ();
});
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);
return;
}
notifySuccess ("uninstall", response);
});
break;
default:
console.log ("ActionModal unknown action=[%s]", action);
break;
}
closeModal();
};
// 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
};
// Popup Modal to render application data
scope.modal = new ModalFactory(config);
scope.modal.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.label = scope.store [attrs.handle].name;
scope.appID= attrs.handle;
} else {
scope.icon = AppConfig.paths.icons + 'w3c-ico.png';
scope.label = attrs.handle;
}
// add label as class
elem.addClass (scope.label.toLowerCase());
// note: clicked in imported and when template is clicked
// it will call clicked method passed in param.
}
return {
restrict: 'E',
template: tmplAppli,
link: mymethods,
scope: {callback: '=', store: '='}
};
});
})();