From 506a48569b437739a943dca2b51d0623758ad7bf Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Wed, 20 Apr 2016 13:38:37 +0530 Subject: [PATCH 1/2] Now operations are auto generated and supports query params, path params and form params. --- .../operation-bar.hbs | 38 +++++++++-- .../public/js/operation-bar.js | 66 ++++++++++++++++++- 2 files changed, 98 insertions(+), 6 deletions(-) diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/operation-bar.hbs b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/operation-bar.hbs index bdeac19679..a0eb524f96 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/operation-bar.hbs +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/operation-bar.hbs @@ -44,13 +44,20 @@
-
- {{#each params}} - + + {{#each params.0.pathParams}} +
{{/each}} - @@ -70,6 +77,27 @@ {{/if}} + + {{#zone "bottomJs"}} {{js "js/operation-bar.js"}} {{/zone}} diff --git a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/public/js/operation-bar.js b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/public/js/operation-bar.js index efaa79aca7..afd4c11eea 100644 --- a/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/public/js/operation-bar.js +++ b/components/iot-plugins/iot-base-plugin/org.wso2.carbon.device.mgt.iot.ui/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.operation-bar/public/js/operation-bar.js @@ -20,13 +20,77 @@ * On operation click function. * @param selection: Selected operation */ -function operationSelect (selection) { +function operationSelect(selection) { $(modalPopupContent).addClass("operation-data"); $(modalPopupContent).html($(" .operation[data-operation-code=" + selection + "]").html()); $(modalPopupContent).data("operation-code", selection); showPopup(); } +function submitForm(formId) { + var form = $("#" + formId); + var uri = form.attr("action"); + var uriencodedQueryStr = ""; + var uriencodedFormStr = ""; + var payload = {}; + form.find("input").each(function () { + var input = $(this); + if (input.data("param-type") == "path") { + uri = uri.replace("{" + input.attr("id") + "}", input.val()); + } else if (input.data("param-type") == "query") { + var prefix = (uriencodedQueryStr == "") ? "?" : "&"; + uriencodedQueryStr += prefix + input.attr("id") + "=" + input.val(); + } else if (input.data("param-type") == "form") { + var prefix = (uriencodedFormStr == "") ? "" : "&"; + uriencodedFormStr += prefix + input.attr("id") + "=" + input.val(); + //payload[input.attr("id")] = input.val(); + } + }); + uri += uriencodedQueryStr; + var httpMethod = form.attr("method").toUpperCase(); + var contentType = form.attr("enctype"); + console.log(payload); + if (contentType == undefined || contentType.isEmpty()) { + contentType = "application/x-www-form-urlencoded"; + payload = uriencodedFormStr; + } + //setting responses callbacks + var defaultStatusClasses = "fw fw-stack-1x"; + var content = $("#operation-response-template").find(".content"); + var title = content.find("#title"); + var statusIcon = content.find("#status-icon"); + var description = content.find("#description"); + var successCallBack = function (response) { + title.html("Response Received!"); + statusIcon.attr("class", defaultStatusClasses + " fw-success"); + description.html(response); + $(modalPopupContent).html(content.html()); + }; + var errorCallBack = function (response) { + console.log(response); + title.html("An Error Occurred!"); + statusIcon.attr("class", defaultStatusClasses + " fw-error"); + var reason = (response.responseText == "null")?response.statusText:response.responseText; + description.html(reason); + $(modalPopupContent).html(content.html()); + }; + //executing http request + if (httpMethod == "GET") { + invokerUtil.get(uri, successCallBack, errorCallBack, contentType); + } else if (httpMethod == "POST") { + invokerUtil.post(uri, payload, successCallBack, errorCallBack, contentType); + } else if (httpMethod == "PUT") { + invokerUtil.put(uri, payload, successCallBack, errorCallBack, contentType); + } else if (httpMethod == "DELETE") { + invokerUtil.get(uri, successCallBack, errorCallBack, contentType); + } else { + title.html("An Error Occurred!"); + statusIcon.attr("class", defaultStatusClasses + " fw-error"); + description.html("This operation requires http method: " + httpMethod + " which is not supported yet!"); + $(modalPopupContent).html(content.html()); + } +} + $(document).on('submit', 'form', function (e) { e.preventDefault(); var postOperationRequest = $.ajax({ From 0a060d9a73f1a8f78cbc77f80eaa0deb920b307e Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Wed, 20 Apr 2016 14:04:06 +0530 Subject: [PATCH 2/2] Fixing android-sense to support auto-generated operations --- .../device-view.hbs | 2 +- .../device-view.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.hbs b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.hbs index f5fb9d840d..8157e44b3d 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.hbs +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.hbs @@ -14,7 +14,7 @@ Operations
- {{unit "iot.unit.device.operation-bar" device=device}} + {{unit "iot.unit.device.operation-bar" device=device backendApiUri=backendApiUri}}
{{/zone}} diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.js b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.js index e4dd5c0da5..35041e0cdb 100644 --- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.js +++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.device-view/device-view.js @@ -20,13 +20,14 @@ function onRequest(context) { var log = new Log("device-view.js"); var deviceType = context.uriParams.deviceType; var deviceId = request.getParameter("id"); + var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); if (deviceType && deviceId) { var deviceModule = require("/app/modules/device.js").deviceModule; var device = deviceModule.viewDevice(deviceType, deviceId); if (device && device.status != "error") { - return {"device": device}; + return {"device": device, "backendApiUri" : devicemgtProps["httpsURL"] + "/android_sense/"}; } else { response.sendError(404, "Device Id " + deviceId + "of type " + deviceType + " cannot be found!"); exit();