From cdff923aab1ea4c3e307547a650923e65b5e26ec Mon Sep 17 00:00:00 2001 From: kamidu Date: Tue, 16 Jan 2018 18:46:38 +0530 Subject: [PATCH] validation improvements --- .../public/js/geo_remote.js | 226 +++++++++--------- 1 file changed, 119 insertions(+), 107 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js index 0023bead06..b4812040d5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js @@ -56,7 +56,7 @@ function addTileUrl() { var serverUrl = "/portal/store/carbon.super/fs/gadget/geo-dashboard/controllers/tile_servers.jag"; // TODO: If failure happens notify user about the error message $.post(serverUrl, data, function (response) { - noty({text: '' + response + '', type: 'success' }); + noty({text: '' + response + '', type: 'success'}); closeAll(); }); } @@ -77,7 +77,10 @@ function getTileServers() { $.getJSON("/api/controllers/tile_servers?serverId=all", function (data) { console.log(JSON.stringify(data)); $.each(data, function (key, val) { - noty({text: 'Loading... ' + val.NAME + '', type: 'info'}); + noty({ + text: 'Loading... ' + val.NAME + '', + type: 'info' + }); //baseLayers[val.name] var newTileLayer = L.tileLayer( val.URL, { @@ -152,8 +155,11 @@ function addWmsEndPoint() { var serverUrl = "/api/controllers/wms_endpoints"; // TODO: If failure happens notify user about the error message $.post(serverUrl, data, function (response) { - console.log("------->>"+ response); - noty({text: '' + response + '',type: 'success'}); + console.log("------->>" + response); + noty({ + text: '' + response + '', + type: 'success' + }); closeAll(); }); } @@ -184,7 +190,7 @@ function setSpeedAlert() { if (!speedAlertValue) { var message = "Speed cannot be empty."; - noty({text: message, type : 'error' }); + noty({text: message, type: 'error'}); } else { data = { 'parseData': JSON.stringify({'speedAlertValue': speedAlertValue, 'deviceId': deviceId}), // parseKey : parseValue pair , this key pair is replace with the key in the template file @@ -197,7 +203,7 @@ function setSpeedAlert() { var responseHandler = function (data, textStatus, xhr) { closeAll(); if (xhr.status == 200) { - noty({text: 'Successfully added alert', type : 'success'}); + noty({text: 'Successfully added alert', type: 'success'}); } else { var ptrn = /(?:)(.*)(?:<\/am\:description>)/g; var result = (ptrn.exec(data)); @@ -207,12 +213,12 @@ function setSpeedAlert() { } else { errorTxt = data; } - noty({text: textStatus + ' : ' + errorTxt, type : 'error'}); + noty({text: textStatus + ' : ' + errorTxt, type: 'error'}); } }; invokerUtil.put(serviceUrl, - data, - responseHandler, function (xhr) { + data, + responseHandler, function (xhr) { responseHandler(xhr.responseText, xhr.statusText, xhr); }); } @@ -234,30 +240,31 @@ function setWithinAlert(leafletId) { if (areaName == null || areaName === undefined || areaName == "") { var message = "Area Name cannot be empty."; - noty({text: message, type : 'error' }); - } else if (areaName.indexOf(" ") > -1) { + noty({text: message, type: 'error'}); + } else if ($.trim(areaName).indexOf(" ") > -1) { var message = "Area Name cannot contain spaces."; - noty({text: message, type : 'error' }); + noty({text: message, type: 'error'}); } else { + areaName = $.trim(areaName); var data = { 'parseData': JSON.stringify({ - 'geoFenceGeoJSON': selectedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "WithIn", deviceId), - 'areaName': areaName, - 'deviceId' : deviceId - }), + 'geoFenceGeoJSON': selectedAreaGeoJson, + 'executionPlanName': createExecutionPlanName(queryName, "WithIn", deviceId), + 'areaName': areaName, + 'deviceId': deviceId + }), 'executionPlan': 'Within', 'customName': areaName, // TODO: fix , When template copies there can be two queryName and areaName id elements in the DOM 'queryName': queryName, 'cepAction': 'deploy', - 'deviceId' : deviceId + 'deviceId': deviceId }; var serviceUrl = '/api/device-mgt/v1.0/geo-services/alerts/Within/' + deviceType + '/' + deviceId; var responseHandler = function (data, textStatus, xhr) { closeTools(leafletId); if (xhr.status == 200) { - noty({text: 'Successfully added alert', type : 'success'}); + noty({text: 'Successfully added alert', type: 'success'}); } else { var ptrn = /(?:)(.*)(?:<\/am\:description>)/g; var errorTxt; @@ -267,12 +274,12 @@ function setWithinAlert(leafletId) { } else { errorTxt = data; } - noty({text: textStatus + ' : ' + errorTxt, type : 'error'}); + noty({text: textStatus + ' : ' + errorTxt, type: 'error'}); } }; invokerUtil.post(serviceUrl, - data, - responseHandler, function (xhr) { + data, + responseHandler, function (xhr) { responseHandler(xhr.responseText, xhr.statusText, xhr); }); viewFenceByData(selectedAreaGeoJson, queryName, areaName, null, 'Within'); @@ -292,30 +299,31 @@ function setExitAlert(leafletId) { if (areaName == null || areaName === undefined || areaName == "") { var message = "Area Name cannot be empty."; - noty({text: message, type : 'error' }); - } else if (areaName.indexOf(" ") > -1) { + noty({text: message, type: 'error'}); + } else if ($.trim(areaName).indexOf(" ") > -1) { var message = "Area Name cannot contain spaces."; - noty({text: message, type : 'error' }); + noty({text: message, type: 'error'}); } else { + areaName = $.trim(areaName); var data = { 'parseData': JSON.stringify({ - 'geoFenceGeoJSON': selectedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Exit", deviceId), - 'areaName': areaName, - 'deviceId' : deviceId - }), + 'geoFenceGeoJSON': selectedAreaGeoJson, + 'executionPlanName': createExecutionPlanName(queryName, "Exit", deviceId), + 'areaName': areaName, + 'deviceId': deviceId + }), 'executionPlan': 'Exit', 'customName': areaName, // TODO: fix , When template copies there can be two queryName and areaName id elements in the DOM 'queryName': queryName, 'cepAction': 'deploy', - 'deviceId' : deviceId + 'deviceId': deviceId }; var serviceUrl = '/api/device-mgt/v1.0/geo-services/alerts/Exit/' + deviceType + '/' + deviceId; var responseHandler = function (data, textStatus, xhr) { closeTools(leafletId); if (xhr.status == 200) { - noty({text: 'Successfully added alert', type : 'success'}); + noty({text: 'Successfully added alert', type: 'success'}); } else { var ptrn = /(?:)(.*)(?:<\/am\:description>)/g; var errorTxt; @@ -325,12 +333,12 @@ function setExitAlert(leafletId) { } else { errorTxt = data; } - noty({text: textStatus + ' : ' + errorTxt, type : 'error'}); + noty({text: textStatus + ' : ' + errorTxt, type: 'error'}); } }; invokerUtil.post(serviceUrl, - data, - responseHandler, function (xhr) { + data, + responseHandler, function (xhr) { responseHandler(xhr.responseText, xhr.statusText, xhr); }); viewFenceByData(selectedAreaGeoJson, queryName, areaName, null, 'Exit'); @@ -357,25 +365,26 @@ function setStationeryAlert(leafletId) { if (stationeryName == null || stationeryName === undefined || stationeryName == "") { var message = "Stationery Name cannot be empty."; - noty({text: message, type : 'error' }); - } else if (stationeryName.indexOf(" ") > -1) { + noty({text: message, type: 'error'}); + } else if ($.trim(stationeryName).indexOf(" ") > -1) { var message = "Stationery Name cannot contain spaces."; - noty({text: message, type : 'error' }); + noty({text: message, type: 'error'}); } else if (fluctuationRadius == null || fluctuationRadius === undefined || fluctuationRadius == "") { var message = "Fluctuation Radius cannot be empty."; - noty({text: message, type : 'error' }); + noty({text: message, type: 'error'}); } else if (time == null || time === undefined || time == "") { var message = "Time cannot be empty."; - noty({text: message, type: 'error' }); + noty({text: message, type: 'error'}); } else { + stationeryName = $.trim(stationeryName); var data = { 'parseData': JSON.stringify({ - 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Stationery", deviceId), - 'stationeryName': stationeryName, - 'stationeryTime': time, - 'fluctuationRadius': fluctuationRadius - }), + 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, + 'executionPlanName': createExecutionPlanName(queryName, "Stationery", deviceId), + 'stationeryName': stationeryName, + 'stationeryTime': time, + 'fluctuationRadius': fluctuationRadius + }), 'stationeryTime': time, 'fluctuationRadius': fluctuationRadius, 'executionPlan': 'Stationery', @@ -388,7 +397,7 @@ function setStationeryAlert(leafletId) { var responseHandler = function (data, textStatus, xhr) { closeTools(leafletId); if (xhr.status == 200) { - noty({text: 'Successfully added alert', type : 'success'}); + noty({text: 'Successfully added alert', type: 'success'}); } else { var ptrn = /(?:)(.*)(?:<\/am\:description>)/g; var errorTxt; @@ -398,12 +407,12 @@ function setStationeryAlert(leafletId) { } else { errorTxt = data; } - noty({text: textStatus + ' : ' + errorTxt, type : 'error'}); + noty({text: textStatus + ' : ' + errorTxt, type: 'error'}); } }; invokerUtil.post(serviceUrl, - data, - responseHandler, function (xhr) { + data, + responseHandler, function (xhr) { responseHandler(xhr.responseText, xhr.statusText, xhr); }); viewFenceByData(selectedProcessedAreaGeoJson, queryName, areaName, time, 'Stationery'); @@ -476,17 +485,18 @@ function setTrafficAlert(leafletId) { if (areaName == null || areaName === undefined || areaName == "") { var message = "Area Name cannot be empty."; - noty({text: message, type : 'error' }); - } else if (areaName.indexOf(" ") > -1) { + noty({text: message, type: 'error'}); + } else if ($.trim(areaName).indexOf(" ") > -1) { var message = "Area Name cannot contain spaces."; - noty({text: message, type : 'error' }); + noty({text: message, type: 'error'}); } else { + areaName = $.trim(areaName); var data = { 'parseData': JSON.stringify({ - 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Traffic", deviceId), - 'areaName': areaName - }), + 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, + 'executionPlanName': createExecutionPlanName(queryName, "Traffic", deviceId), + 'areaName': areaName + }), 'executionPlan': 'Traffic', 'customName': areaName, // TODO: fix , When template copies there can be two queryName and areaName id elements in the DOM 'queryName': queryName, @@ -498,7 +508,7 @@ function setTrafficAlert(leafletId) { var responseHandler = function (data, textStatus, xhr) { closeTools(leafletId); if (xhr.status == 200) { - noty({text: 'Successfully added alert', type : 'success'}); + noty({text: 'Successfully added alert', type: 'success'}); } else { var ptrn = /(?:)(.*)(?:<\/am\:description>)/g; var errorTxt; @@ -508,12 +518,12 @@ function setTrafficAlert(leafletId) { } else { errorTxt = data; } - noty({text: textStatus + ' : ' + errorTxt, type : 'error'}); + noty({text: textStatus + ' : ' + errorTxt, type: 'error'}); } }; invokerUtil.post(serviceUrl, - data, - responseHandler, function (xhr) { + data, + responseHandler, function (xhr) { responseHandler(xhr.responseText, xhr.statusText, xhr); }); } @@ -524,24 +534,23 @@ function removeGeoFence(geoFenceElement, id) { var areaName = $(geoFenceElement).attr('data-areaName'); var serviceUrl = '/api/device-mgt/v1.0/geo-services/alerts/' + id + '/' + deviceType + '/' + deviceId + '?queryName=' - + queryName; + + queryName; invokerUtil.delete(serviceUrl, function (response) { - noty({ - text: 'Successfully removed ' + id + ' alert', - type: 'success' - }); - closeAll(); - }, - function (xhr) { - noty({ - text: 'Could not remove ' + id + ' alert', - type: 'error' - }) - }); + noty({ + text: 'Successfully removed ' + id + ' alert', + type: 'success' + }); + closeAll(); + }, + function (xhr) { + noty({ + text: 'Could not remove ' + id + ' alert', + type: 'error' + }) + }); } - function getAlertsHistory(deviceType, deviceId, timeFrom, timeTo) { var timeRange = ''; if (timeFrom && timeTo) { @@ -549,30 +558,30 @@ function getAlertsHistory(deviceType, deviceId, timeFrom, timeTo) { } var serviceUrl = '/api/device-mgt/v1.0/geo-services/alerts/history/' + deviceType + '/' + deviceId + timeRange; invokerUtil.get(serviceUrl, - function (data) { - geoAlertsBar.clearAllAlerts(); - var alerts = JSON.parse(data); - $.each(alerts, function (key, val) { - if(val.values){ - val = val.values; - } - var msg = deviceType.charAt(0).toUpperCase() + deviceType.slice(1) + - " " + deviceId +" "+ val.information.replace("Alerts: ,", "") + " - " + timeSince(val.timeStamp); - switch (val.state) { - case "NORMAL": - return; - case "WARNING": - geoAlertsBar.addAlert('warn', msg, val); - break; - case "ALERTED": - geoAlertsBar.addAlert('danger', msg, val); - break; - case "OFFLINE": - geoAlertsBar.addAlert('info', msg, val); - break; - } - }); - }, function (message) { + function (data) { + geoAlertsBar.clearAllAlerts(); + var alerts = JSON.parse(data); + $.each(alerts, function (key, val) { + if (val.values) { + val = val.values; + } + var msg = deviceType.charAt(0).toUpperCase() + deviceType.slice(1) + + " " + deviceId + " " + val.information.replace("Alerts: ,", "") + " - " + timeSince(val.timeStamp); + switch (val.state) { + case "NORMAL": + return; + case "WARNING": + geoAlertsBar.addAlert('warn', msg, val); + break; + case "ALERTED": + geoAlertsBar.addAlert('danger', msg, val); + break; + case "OFFLINE": + geoAlertsBar.addAlert('info', msg, val); + break; + } + }); + }, function (message) { }); } @@ -584,14 +593,17 @@ function setProximityAlert() { if (proximityDistance == null || proximityDistance === undefined || proximityDistance == "") { var message = "Proximity distance cannot be empty."; - noty({text: message, type : 'error'}); + noty({text: message, type: 'error'}); } else if (proximityTime == null || proximityTime === undefined || proximityTime == "") { var message = "Proximity Time cannot be empty."; - noty({text: message, type : 'error'}); + noty({text: message, type: 'error'}); } else { var data = { - 'parseData': JSON.stringify({'proximityTime': proximityTime, 'proximityDistance': proximityDistance}), + 'parseData': JSON.stringify({ + 'proximityTime': proximityTime, + 'proximityDistance': proximityDistance + }), 'proximityTime': proximityTime, 'proximityDistance': proximityDistance, 'executionPlan': 'Proximity', @@ -603,7 +615,7 @@ function setProximityAlert() { var responseHandler = function (data, textStatus, xhr) { closeAll(); if (xhr.status == 200) { - noty({text: 'Successfully added alert', type : 'success'}); + noty({text: 'Successfully added alert', type: 'success'}); } else { var ptrn = /(?:)(.*)(?:<\/am\:description>)/g; var errorTxt; @@ -613,12 +625,12 @@ function setProximityAlert() { } else { errorTxt = data; } - noty({text: textStatus + ' : ' + errorTxt, type : 'error'}); + noty({text: textStatus + ' : ' + errorTxt, type: 'error'}); } }; invokerUtil.put(serviceUrl, - data, - responseHandler, function (xhr) { + data, + responseHandler, function (xhr) { responseHandler(xhr.responseText, xhr.statusText, xhr); }); @@ -630,7 +642,7 @@ function createExecutionPlanName(queryName, id, deviceId) { if (id == "WithIn") { return 'Geo-ExecutionPlan-Within' + (queryName ? '_' + queryName : '') + "---" + (deviceId ? '_' + deviceId : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if(id == "Exit"){ + } else if (id == "Exit") { return 'Geo-ExecutionPlan-Exit' + (queryName ? '_' + queryName : '') + "---" + (deviceId ? '_' + deviceId : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) } else if (id == "Stationery") { return 'Geo-ExecutionPlan-Stationery' + (queryName ? '_' + queryName : '') + "---" + (deviceId ? '_' + deviceId : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String)