Yangman - precheck patharray, fix modal z-index and so on
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / services / yangman.services.js
index d6f38ea7558b9203796ee899b2d97378a952ce68..740734ee32bc08519f56a06555465dda51c5513e 100644 (file)
@@ -28,11 +28,18 @@ define([], function () {
             getDataStoreIndex: getDataStoreIndex,
             prepareAllRequestData: prepareAllRequestData,
             prepareReceivedData: prepareReceivedData,
+            putIntoObj: putIntoObj,
             validateFile: validateFile,
         };
 
         return service;
 
+        /**
+         * Put data to output container if root node is rpc
+         * @param data
+         * @param node
+         * @returns {*}
+         */
         function checkRpcReceivedData(data, node){
             return node.type === 'rpc' ? cutData(data) : data;
 
@@ -43,6 +50,18 @@ define([], function () {
             }
         }
 
+        /**
+         * Put source object into destination object by source properties
+         * @param sourceObj
+         * @param destinationObj
+         */
+        function putIntoObj(sourceObj, destinationObj, containter){
+            Object.keys(sourceObj).forEach(function(prop){
+                destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {};
+                destinationObj[containter][prop] = sourceObj[prop];
+            });
+        }
+
         /**
          * Prepare request date before filling into node depends on method and node type
          * @param node
@@ -54,7 +73,7 @@ define([], function () {
          */
         function prepareReceivedData(node, method, rData, sData, outputType){
             var prepareType = {
-                rpc: function(){
+                rpc: function (){
 
                     if ( outputType === 'form' ){
                         var dObj = {};
@@ -64,35 +83,24 @@ define([], function () {
                     } else {
                         return rData;
                     }
-
-                    /**
-                     * Put source object into destination object by source properties
-                     * @param sourceObj
-                     * @param destinationObj
-                     */
-                    function putIntoObj(sourceObj, destinationObj, containter){
-                        Object.keys(sourceObj).forEach(function(prop){
-                            destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {};
-                            destinationObj[containter][prop] = sourceObj[prop];
-                        });
-                    }
                 },
-                default: function(){
+                default: function (){
                     var methodType = {
                         GET: function () {
-                            node.clear();
+                            if ( node ){
+                                node.clear();
+                            }
                             return rData;
                         },
                         DELETE: function () {
-                            node.clear();
+                            if ( node ) {
+                                node.clear();
+                            }
                             return {};
                         },
-                        PUT: function () {
+                        DEFAULT: function () {
                             return rData;
                         },
-                        DEFAULT: function () {
-                            return outputType === 'form' ? sData : rData;
-                        }
                     };
 
                     return (methodType[method] || methodType.DEFAULT)();
@@ -157,7 +165,9 @@ define([], function () {
                 customRestangular: null,
                 headers: {},
                 operation: '',
-                reqString: selectedSubApi ? selectedSubApi.buildApiRequestString() : '',
+                reqString:
+                    selectedSubApi ?
+                        RequestsService.applyParamsToStr(params, selectedSubApi.buildApiRequestString()) : '',
                 reqHeaders: {},
                 reqData: '',
                 srcData: '',
@@ -170,23 +180,29 @@ define([], function () {
             else {
                 allPreparedData.srcData = requestData;
             }
-            allPreparedData.reqData = RequestsService.applyParams(params, allPreparedData.srcData);
+
+            allPreparedData.reqData = RequestsService.applyParamsToObj(params, allPreparedData.srcData);
+
 
             // prepare req data
             if (operation === 'GET' || operation === 'DELETE'){
                 allPreparedData.srcData = null;
                 allPreparedData.reqData = null;
             }
-            else if (operation === 'POST'){
-                allPreparedData.reqData = YangUtilsService.postRequestData(
-                    allPreparedData.reqData,
-                    allPreparedData.reqString,
-                    selectedSubApi
-                );
+            else if ( operation === 'POST' ){
+
+                if ( selectedSubApi ) {
+                    allPreparedData.reqData = YangUtilsService.postRequestData(
+                        allPreparedData.reqData,
+                        allPreparedData.reqString,
+                        selectedSubApi
+                    );
+                }
             }
 
             // set correct host into restangular based on shown data type and prepare data
             if ( dataType === 'req-data' ){
+                requestUrl = RequestsService.applyParamsToStr(params, requestUrl);
                 var parser = locationHelper(requestUrl, ['pathname', 'origin']),
                     raParam = '';
 
@@ -260,11 +276,12 @@ define([], function () {
             function finishExecuting(response){
                 // finish track time response
                 time.finished = new Date().getMilliseconds();
+                var spentRequestTime = time.finished - time.started;
 
                 return {
                     status: response.status,
                     statusText: response.statusText,
-                    time: (time.finished - time.started),
+                    time: spentRequestTime < 0 ? -(spentRequestTime) : spentRequestTime,
                     requestData: allPreparedData.reqData,
                     requestSrcData: allPreparedData.srcData,
                 };