From 5634c7b3a14eaf992a9980de035308f4f5148623 Mon Sep 17 00:00:00 2001 From: Jonathan Hornung Date: Thu, 3 May 2018 13:49:48 +0200 Subject: [PATCH] Plugin json loader: Supporting Authorization Headers --- dist/aping.js | 27 ++++++++++++++++++--------- dist/aping.min.js | 4 ++-- src/plugin-jsonloader.js | 25 +++++++++++++++++-------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/dist/aping.js b/dist/aping.js index 0c7468c..7232a7e 100644 --- a/dist/aping.js +++ b/dist/aping.js @@ -1,6 +1,6 @@ /** @name: aping - @version: 1.4.2 (11-03-2018) + @version: 1.4.3 (03-05-2018) @author: Jonathan Hornung @url: https://github.com/JohnnyTheTank/apiNG @license: MIT @@ -752,6 +752,11 @@ angular.module('jtt_aping_jsonloader', []) if (angular.isDefined(request.xAuthToken)) { requestObject.xAuthToken = request.xAuthToken; } + + if (angular.isDefined(request.Authorization)) { + requestObject.Authorization = request.Authorization; + } + jsonloaderFactory.getJsonData(requestObject) .then(function (_data) { var resultArray = []; @@ -816,19 +821,17 @@ angular.module('jtt_aping_jsonloader', []) } } + if (angular.isDefined(_requestObject.Authorization)) { + httpObject.headers = { + 'Authorization': _requestObject.Authorization + } + } + return $http.jsonp( _requestObject.path, httpObject ); - /* - return $http({ - method: 'JSONP', - url: _requestObject.path, - params: {callback: 'JSON_CALLBACK'}, - }); - */ - } else { var httpObject = { @@ -843,6 +846,12 @@ angular.module('jtt_aping_jsonloader', []) } } + if (angular.isDefined(_requestObject.Authorization)) { + httpObject.headers = { + 'Authorization': _requestObject.Authorization + } + } + return $http(httpObject); } }; diff --git a/dist/aping.min.js b/dist/aping.min.js index 1842693..8e3301d 100644 --- a/dist/aping.min.js +++ b/dist/aping.min.js @@ -1,8 +1,8 @@ /** @name: aping - @version: 1.4.2 (11-03-2018) + @version: 1.4.3 (03-05-2018) @author: Jonathan Hornung @url: https://github.com/JohnnyTheTank/apiNG @license: MIT */ -"use strict";angular.module("jtt_aping",["jtt_aping_jsonloader","jtt_aping_xml","jtt_aping_ng_array","jtt_aping_local_storage","jtt_aping_json_string"]),angular.module("jtt_aping").config(["$provide",function(a){a.value("apingDefaultSettings",{apingApiKeys:{}})}]).value("apingResults",{}).directive("aping",["apingResults","apingDefaultSettings","apingUtilityHelper","$templateRequest","$compile",function(a,b,c,d,e){return{restrict:"EA",transclude:!0,template:"",scope:{model:"@",getNativeData:"@",items:"@",maxItems:"@",orderBy:"@",orderReverse:"@",templateUrl:"@",payloadJson:"@",removeDoubles:"@",mergeDoubles:"@",idBy:"@",resultProperty:"@",resultName:"@",valueName:"@",protocol:"@"},link:function(a,c,f,g,h){function i(b){angular.isDefined(b)&&"$NONE"!==b?d(b).then(function(b){var d=angular.element(b);c.empty().append(d),e(d)(a)}):h(a,function(a,b){c.html(""),c.append(a),e(a)(b)}),a.$broadcast("apiNG.templateRendered")}var j=a.templateUrl;a.$watch("templateUrl",function(){i(a.templateUrl)}),angular.isUndefined(j)&&angular.isDefined(b.templateUrl)&&(j=b.templateUrl,i(j))},controller:["$scope",function(d){angular.isUndefined(d.resultName)&&(angular.isUndefined(d.resultProperty)?d.resultName="results":d.resultName=d.resultProperty),d[d.resultName]=[],d.payload=d.payloadJson?c.replaceSingleQuotesAndParseJson(d.payloadJson):{},this.getAppSettings=function(){var a,c,e,f,g,h,i,j,k,l;return j=angular.isDefined(d.valueName)?d.valueName:void 0,a=angular.isDefined(d.items)?d.items:angular.isDefined(b.items)?b.items:void 0,c=angular.isDefined(d.maxItems)?d.maxItems:angular.isDefined(b.maxItems)?b.maxItems:void 0,e=angular.isDefined(d.getNativeData)?d.getNativeData:!!angular.isDefined(b.getNativeData)&&b.getNativeData,c=angular.isDefined(d.maxItems)?d.maxItems:angular.isDefined(b.maxItems)?b.maxItems:void 0,g=angular.isDefined(d.orderBy)?d.orderBy:angular.isDefined(b.orderBy)?b.orderBy:void 0,f=angular.isDefined(d.orderReverse)?d.orderReverse:!!angular.isDefined(b.orderReverse)&&b.orderReverse,h=angular.isDefined(d.removeDoubles)?d.removeDoubles:!!angular.isDefined(b.removeDoubles)&&b.removeDoubles,i=angular.isDefined(d.mergeDoubles)?d.mergeDoubles:!!angular.isDefined(b.mergeDoubles)&&b.mergeDoubles,k=angular.isDefined(d.idBy)?d.idBy:angular.isDefined(b.idBy)?b.idBy:void 0,l=angular.isDefined(d.protocol)?d.protocol:angular.isDefined(b.protocol)?b.protocol:void 0,{model:d.model||b.model||"native",getNativeData:e,items:a,maxItems:c,orderBy:g,orderReverse:f,removeDoubles:h,mergeDoubles:i,idBy:k,valueName:j,protocol:l}},this.concatToResults=function(b){angular.isUndefined(d.resultName)&&(angular.isUndefined(d.resultProperty)?d.resultName="results":d.resultName=d.resultProperty);var e=d[d.resultName].concat(b),f=this.getAppSettings();angular.isDefined(f.idBy)&&(e=c.createIdByPropertiesForArray(e,f.idBy),f.mergeDoubles!==!0&&"true"!==f.mergeDoubles||(e=c.mergeDuplicateObjectsFromArray(e,f.orderBy===!1||"false"===f.orderBy||"$NONE"===f.orderBy))),f.removeDoubles!==!0&&"true"!==f.removeDoubles||f.mergeDoubles!==!0&&"true"!==f.mergeDoubles&&(e=c.removeDuplicateObjectsFromArray(e,f.orderBy===!1||"false"===f.orderBy||"$NONE"===f.orderBy,angular.isDefined(f.idBy))),angular.isDefined(f.orderBy)&&f.orderBy!==!1&&"false"!==f.orderBy&&"$NONE"!==f.orderBy&&("$RANDOM"===f.orderBy?e=c.shuffleArray(e):e.sort(c.sortArrayByProperty(f.orderBy))),f.orderReverse!==!0&&"true"!==f.orderReverse||"$RANDOM"===f.orderBy||e.reverse(),f.maxItems>-1&&e.length>f.maxItems&&(e=e.splice(0,f.maxItems)),angular.isDefined(f.valueName)&&(a[f.valueName]=e),d[d.resultName]=e,d.$broadcast("apiNG.resultMerged",{resultName:d.resultName,valueName:d.valueName}),d.$emit("apiNG.resultMerged",{resultName:d.resultName,valueName:d.valueName})},this.apply=function(){d.$apply()}}]}}]),angular.module("jtt_aping").service("apingTimeHelper",function(){this.getTimestampFromDateString=function(a,b,c){if((angular.isUndefined(b)||isNaN(b))&&(b=1),(angular.isUndefined(c)||isNaN(c))&&(c=0),"string"==typeof a){var d=a.split(/[^0-9]/);try{return parseInt(Math.round(new Date(d[0],d[1]-1,d[2],d[3],d[4],d[5])/1e3*b)+c,10)}catch(e){return 0}}return 0}}).service("apingUtilityHelper",["apingInputObjects","apingDefaultSettings","$parse",function(a,b,c){this.getApiCredentials=function(a,c){return!(!b.apingApiKeys||!b.apingApiKeys[a])&&b.apingApiKeys[a][Math.floor(Math.random()*b.apingApiKeys[a].length)][c]},this.parseJsonFromAttributes=function(a,b,c){return this.parseRequestsFromAttributes(a,b,c)},this.getDifference=function(a,b){return a>b?a-b:b-a},this.parseRequestsFromAttributes=function(b,c,d){if("string"!=typeof b||!b)return[];var e=[],f=this.replaceSingleQuotesAndParseJson(b);return f.constructor===Array?angular.forEach(f,function(b){b.platform=c,d&&(angular.isUndefined(b.items)&&angular.isDefined(d.items)&&(b.items=d.items),angular.isUndefined(b.model)&&angular.isDefined(d.model)&&(b.model=d.model));var f=a.getNew("request",b);e.push(f)}):e.push(f),e},this.replaceSingleQuotesAndParseJson=function(a){return c(a)()},this.sortArrayByProperty=function(a){var b=1;return"-"===a[0]&&(b=-1,a=a.substr(1)),function(c,d){var e=c[a]d[a]?1:0;return e*b}},this.shuffleArray=function(a){for(var b=a.length-1;b>0;b--){var c=Math.floor(Math.random()*(b+1)),d=a[b];a[b]=a[c],a[c]=d}return a},this.removeNullIn=function(a,b){var c=b[a];if(null===c||void 0===c)delete b[a];else if("object"==typeof c)for(var d in c)this.removeNullIn(d,c)},this.removeNull=function(a){for(var b in a)this.removeNullIn(b,a)},this.removeDuplicateObjectsFromArray=function(a,b,c){var d,e=[];d=c?"aping_id":"apingStringified";var f="apingTempOrder";if(1===a.length)return a;angular.forEach(a,function(a,g){c||(a.$$hashKey=void 0,a[d]=JSON.stringify(a)),b===!0&&(a[f]=g),e.push(a)}),e.sort(this.sortArrayByProperty(d));var g,h=[];return angular.forEach(e,function(a){angular.isDefined(g)?angular.isDefined(a[d])&&a[d]!==g&&h.push(a):h.push(a),g=a[d],c||(a[d]=void 0)}),b===!0&&(h.sort(this.sortArrayByProperty(f)),angular.forEach(h,function(a){a[f]=void 0})),h},this.mergeDuplicateObjectsFromArray=function(a,b){var c=this,d=[],e="aping_id",f="apingTempOrder";if(1===a.length)return a;angular.forEach(a,function(a,c){b===!0&&(a[f]=c),d.push(a)}),d.sort(this.sortArrayByProperty(e));var g,h=[];return angular.forEach(d,function(a){c.removeNull(a),angular.isDefined(g)?angular.isDefined(a[e])&&a[e]!==g?h.push(a):h[h.length-1]=angular.merge(h[h.length-1],h[h.length-1],a):h.push(a),g=a[e]}),b===!0&&(h.sort(this.sortArrayByProperty(f)),angular.forEach(h,function(a){a[f]=void 0})),h},this.getTextFromHtml=function(a){return a=a.replace(/<br ?\/\>|<br ?\/&rt;|\
/g," "),a=a.replace(/<(?:.|\n)*?>/gm,"")},this.getFirstImageFromHtml=function(a){var b=/]+src="([^">]+)/g;return b.exec(a)},this.parseParametersFromUrl=function(a){var b={};return"string"==typeof a&&(b=JSON.parse('{"'+decodeURI(a.replace(/&/g,'","').replace(/=/g,'":"'))+'"}')),b},this.createIdByPropertiesForArray=function(a,b,c){var d=this;return!angular.isUndefined(c)&&angular.isString(c)||(c="aping_id"),angular.isDefined(a)&&angular.isArray(a)&&angular.forEach(a,function(a){a[c]=d.getIdByPropertiesForObject(a,b)}),a},this.getIdByPropertiesForObject=function(a,b){var c=this,d="";if(angular.isDefined(a)&&angular.isObject(a)){var e=[];"["===b.substr(0,1)?e=this.replaceSingleQuotesAndParseJson(b):e.push(b),angular.forEach(e,function(b){d+=c.getValueFromObjectByPropertyString(a,b)})}return d},this.getValueFromObjectByPropertyString=function(a,b,c){var d="";if(angular.isDefined(a)&&angular.isObject(a)){var e=b.split("."),f=a;angular.forEach(e,function(a){angular.isDefined(f[a])&&(f=f[a])}),angular.isDefined(f)&&(d=c&&angular.isObject(f)?JSON.stringify(f):f)}return d}}]),angular.module("jtt_aping").service("apingInputObjects",["apingDefaultSettings",function(a){this.getNew=function(b,c){var d={};switch(b){case"request":d=angular.extend({model:a.model},c)}return d}}]),angular.module("jtt_aping").service("apingModels",[function(){this.getNew=function(a,b){var c={platform:b,model:a};return c}}]),angular.module("jtt_aping_jsonloader",[]).directive("apingJsonloader",["apingUtilityHelper","jsonloaderFactory",function(a,b){return{require:"?aping",restrict:"A",replace:"false",link:function(c,d,e,f){var g=f.getAppSettings(),h=a.parseJsonFromAttributes(e.apingJsonloader,"jsonloader",g);h.forEach(function(c){if(c.path){var d={path:c.path};if(c.format&&"jsonp"===c.format.toLowerCase()?d.format="jsonp":d.format="json",angular.isUndefined(c.items)&&(c.items=g.items),0===c.items||"0"===c.items)return!1;(c.items<0||isNaN(c.items))&&(c.items=void 0),angular.isDefined(c.orderBy)&&!angular.isString(c.orderBy)&&(c.orderBy=void 0),!angular.isDefined(c.orderReverse)||c.orderReverse!==!0&&"true"!==c.orderReverse||(c.orderReverse=!0),angular.isDefined(c.xAuthToken)&&(d.xAuthToken=c.xAuthToken),b.getJsonData(d).then(function(b){var d=[];if(b.data){var e=b.data;angular.isDefined(c.resultProperty)&&(e=a.getValueFromObjectByPropertyString(e,c.resultProperty,!1)),e.constructor!==Array?d.push(e):(angular.extend(d,e),angular.isDefined(c.orderBy)&&("$RANDOM"===c.orderBy?d=a.shuffleArray(d):d.sort(a.sortArrayByProperty(c.orderBy))),angular.isDefined(c.orderReverse)&&c.orderReverse===!0&&"$RANDOM"!==c.orderBy&&d.reverse(),angular.isUndefined(c.items)?d=e:c.items>0&&d.length>c.items&&(d=d.splice(0,c.items)))}f.concatToResults(d)})}})}}}]).factory("jsonloaderFactory",["$http",function(a){var b={};return b.getJsonData=function(b){var c={};if("jsonp"===b.format){var d={method:"GET",params:{callback:"JSON_CALLBACK"}};return angular.isDefined(b.xAuthToken)&&(d.headers={"X-Auth-Token":b.xAuthToken}),a.jsonp(b.path,d)}var d={method:"GET",url:b.path,params:c};return angular.isDefined(b.xAuthToken)&&(d.headers={"X-Auth-Token":b.xAuthToken}),a(d)},b}]),angular.module("jtt_aping_json_string",[]).directive("apingJsonString",["apingUtilityHelper",function(a){return{require:"?aping",restrict:"A",replace:"false",link:function(b,c,d,e){var f=e.getAppSettings(),g=a.parseJsonFromAttributes(d.apingJsonString,"apingJsonString",f),h=[];g&&(g.constructor===Array?h=g:h.push(g),h.length>0&&e.concatToResults(h))}}}]),angular.module("jtt_aping_ng_array",[]).directive("apingNgArray",["apingUtilityHelper",function(a){return{require:"?aping",restrict:"A",replace:"false",link:function(b,c,d,e){var f=e.getAppSettings(),g=a.parseJsonFromAttributes(d.apingNgArray,"ngArray",f);g.forEach(function(c){if(c.name&&b[c.name]){if(angular.isUndefined(c.items)&&(c.items=f.items),0===c.items||"0"===c.items)return!1;(c.items<0||isNaN(c.items))&&(c.items=void 0);var d=[];b[c.name].constructor===Array?(d=b[c.name],angular.isDefined(c.orderBy)&&("$RANDOM"===c.orderBy?d=a.shuffleArray(d):d.sort(a.sortArrayByProperty(c.orderBy))),angular.isDefined(c.orderReverse)&&c.orderReverse===!0&&"$RANDOM"!==c.orderBy&&d.reverse(),angular.isDefined(c.items)&&c.items>0&&d.length>c.items&&(d=d.splice(0,c.items))):"object"==typeof b[c.name]&&null!==b[c.name]&&d.push(b[c.name]),d.length>0&&e.concatToResults(d)}})}}}]),angular.module("jtt_aping_local_storage",[]).directive("apingLocalStorage",["apingUtilityHelper","apingLocalStorage",function(a,b){return{require:"?aping",restrict:"A",replace:"false",link:function(c,d,e,f){var g=f.getAppSettings(),h=a.parseJsonFromAttributes(e.apingLocalStorage,"localStorage",g);h.forEach(function(c){if(c.key){if(angular.isUndefined(c.items)&&(c.items=g.items),0===c.items||"0"===c.items)return!1;(c.items<0||isNaN(c.items))&&(c.items=void 0),angular.isDefined(c.orderBy)&&!angular.isString(c.orderBy)&&(c.orderBy=void 0),!angular.isDefined(c.orderReverse)||c.orderReverse!==!0&&"true"!==c.orderReverse||(c.orderReverse=!0),b.get(c.key).then(function(b){var d=[];if(b){var e=b;angular.isDefined(c.resultProperty)&&(e=a.getValueFromObjectByPropertyString(b,c.resultProperty,!1)),angular.isArray(b)?(d=e,angular.isDefined(c.orderBy)&&("$RANDOM"===c.orderBy?d=a.shuffleArray(d):d.sort(a.sortArrayByProperty(c.orderBy))),angular.isDefined(c.orderReverse)&&c.orderReverse===!0&&"$RANDOM"!==c.orderBy&&d.reverse(),angular.isUndefined(c.items)?d=e:c.items>0&&d.length>c.items&&(d=d.splice(0,c.items))):d.push(e)}f.concatToResults(d)})}})}}}]).factory("apingLocalStorage",["$window","$q",function(a,b){var c=function(c,d){var e=b.defer();return e.resolve(a.localStorage&&a.localStorage.setItem(c,angular.toJson(d))),e.promise},d=function(c){var d=b.defer();return d.resolve(a.localStorage&&angular.fromJson(a.localStorage.getItem(c))),d.promise};return{set:c,get:d}}]),angular.module("jtt_aping_xml",[]).directive("apingXml",["apingUtilityHelper","xmlFactory","xmlService",function(a,b,c){return{require:"?aping",restrict:"A",replace:"false",link:function(d,e,f,g){var h=g.getAppSettings(),i=a.parseJsonFromAttributes(f.apingXml,"xml",h);i.forEach(function(d){if(d.path){var e={path:d.path};if(angular.isUndefined(d.items)&&(d.items=h.items),0===d.items||"0"===d.items)return!1;(d.items<0||isNaN(d.items))&&(d.items=void 0),angular.isDefined(d.orderBy)&&!angular.isString(d.orderBy)&&(d.orderBy=void 0),!angular.isDefined(d.orderReverse)||d.orderReverse!==!0&&"true"!==d.orderReverse||(d.orderReverse=!0),b.getData(e).then(function(b){var e=[];if(b.data){var f=c.xml2json(b.data);angular.isDefined(d.resultProperty)&&(f=a.getValueFromObjectByPropertyString(f,d.resultProperty,!1)),f.constructor!==Array?e.push(f):(angular.extend(e,f),angular.isDefined(d.orderBy)&&("$RANDOM"===d.orderBy?e=a.shuffleArray(e):e.sort(a.sortArrayByProperty(d.orderBy))),angular.isDefined(d.orderReverse)&&d.orderReverse===!0&&"$RANDOM"!==d.orderBy&&e.reverse(),angular.isUndefined(d.items)?e=f:d.items>0&&e.length>d.items&&(e=e.splice(0,d.items)))}g.concatToResults(e)})}})}}}]).factory("xmlFactory",["$http",function(a){var b={};return b.getData=function(b){var c={};return a({method:"GET",url:b.path,params:c})},b}]).service("xmlService",function(){var a=this;this.cleanXML=function(b){return b=b.replace(/\n|\t|\r/g,""),b=b.replace(/ {1,}<|\t{1,} {1,}|>\t{1,}/g,">"),b=b.replace(/<\?[^>]*\?>/g,""),b=a.replaceSelfClosingTags(b),b=a.replaceAloneValues(b),b=a.replaceAttributes(b)},this.replaceSelfClosingTags=function(a){var b=a.match(/<[^\/][^>]*\/>/g);if(b)for(var c=0;c";var f=d.match(/[^<][\w+$]*/)[0],g="",h="<"+f+">",i=e.match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g);if(i)for(var j=0;j"+m+""}h+=g,a=a.replace(d,h)}return a},this.replaceAloneValues=function(a){var b=a.match(/<[^\/][^>][^<]+\s+.[^<]+[=][^<]+>{1}([^<]+)/g);if(b)for(var c=0;c")+1),f=d.substring(d.indexOf(">")+1),g=e+"<_@ttribute>"+f+"";a=a.replace(d,g)}return a},this.replaceAttributes=function(a){var b=a.match(/<[^\/][^>][^<]+\s+.[^<]+[=][^<]+>/g);if(b)for(var c=0;c",g=d.match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g);if(g)for(var h=0;h"+k+""}a=a.replace(d,f)}return a},this.xml2json=function(b){b=this.cleanXML(b);for(var c,d,e,f,g,h={};b.match(/<[^\/][^>]*>/);)g=b.match(/<[^\/][^>]*>/)[0],c=g.substring(1,g.length-1),d=b.indexOf(g.replace("<","]*>/)?a.xml2json(e):e,void 0===h[c]?h[c]=f:Array.isArray(h[c])?h[c].push(f):h[c]=[h[c],f],b=b.substring(2*g.length+1+e.length);return h}}); \ No newline at end of file +"use strict";angular.module("jtt_aping",["jtt_aping_jsonloader","jtt_aping_xml","jtt_aping_ng_array","jtt_aping_local_storage","jtt_aping_json_string"]),angular.module("jtt_aping").config(["$provide",function(a){a.value("apingDefaultSettings",{apingApiKeys:{}})}]).value("apingResults",{}).directive("aping",["apingResults","apingDefaultSettings","apingUtilityHelper","$templateRequest","$compile",function(a,b,c,d,e){return{restrict:"EA",transclude:!0,template:"",scope:{model:"@",getNativeData:"@",items:"@",maxItems:"@",orderBy:"@",orderReverse:"@",templateUrl:"@",payloadJson:"@",removeDoubles:"@",mergeDoubles:"@",idBy:"@",resultProperty:"@",resultName:"@",valueName:"@",protocol:"@"},link:function(a,c,f,g,h){function i(b){angular.isDefined(b)&&"$NONE"!==b?d(b).then(function(b){var d=angular.element(b);c.empty().append(d),e(d)(a)}):h(a,function(a,b){c.html(""),c.append(a),e(a)(b)}),a.$broadcast("apiNG.templateRendered")}var j=a.templateUrl;a.$watch("templateUrl",function(){i(a.templateUrl)}),angular.isUndefined(j)&&angular.isDefined(b.templateUrl)&&(j=b.templateUrl,i(j))},controller:["$scope",function(d){angular.isUndefined(d.resultName)&&(angular.isUndefined(d.resultProperty)?d.resultName="results":d.resultName=d.resultProperty),d[d.resultName]=[],d.payload=d.payloadJson?c.replaceSingleQuotesAndParseJson(d.payloadJson):{},this.getAppSettings=function(){var a,c,e,f,g,h,i,j,k,l;return j=angular.isDefined(d.valueName)?d.valueName:void 0,a=angular.isDefined(d.items)?d.items:angular.isDefined(b.items)?b.items:void 0,c=angular.isDefined(d.maxItems)?d.maxItems:angular.isDefined(b.maxItems)?b.maxItems:void 0,e=angular.isDefined(d.getNativeData)?d.getNativeData:!!angular.isDefined(b.getNativeData)&&b.getNativeData,c=angular.isDefined(d.maxItems)?d.maxItems:angular.isDefined(b.maxItems)?b.maxItems:void 0,g=angular.isDefined(d.orderBy)?d.orderBy:angular.isDefined(b.orderBy)?b.orderBy:void 0,f=angular.isDefined(d.orderReverse)?d.orderReverse:!!angular.isDefined(b.orderReverse)&&b.orderReverse,h=angular.isDefined(d.removeDoubles)?d.removeDoubles:!!angular.isDefined(b.removeDoubles)&&b.removeDoubles,i=angular.isDefined(d.mergeDoubles)?d.mergeDoubles:!!angular.isDefined(b.mergeDoubles)&&b.mergeDoubles,k=angular.isDefined(d.idBy)?d.idBy:angular.isDefined(b.idBy)?b.idBy:void 0,l=angular.isDefined(d.protocol)?d.protocol:angular.isDefined(b.protocol)?b.protocol:void 0,{model:d.model||b.model||"native",getNativeData:e,items:a,maxItems:c,orderBy:g,orderReverse:f,removeDoubles:h,mergeDoubles:i,idBy:k,valueName:j,protocol:l}},this.concatToResults=function(b){angular.isUndefined(d.resultName)&&(angular.isUndefined(d.resultProperty)?d.resultName="results":d.resultName=d.resultProperty);var e=d[d.resultName].concat(b),f=this.getAppSettings();angular.isDefined(f.idBy)&&(e=c.createIdByPropertiesForArray(e,f.idBy),f.mergeDoubles!==!0&&"true"!==f.mergeDoubles||(e=c.mergeDuplicateObjectsFromArray(e,f.orderBy===!1||"false"===f.orderBy||"$NONE"===f.orderBy))),f.removeDoubles!==!0&&"true"!==f.removeDoubles||f.mergeDoubles!==!0&&"true"!==f.mergeDoubles&&(e=c.removeDuplicateObjectsFromArray(e,f.orderBy===!1||"false"===f.orderBy||"$NONE"===f.orderBy,angular.isDefined(f.idBy))),angular.isDefined(f.orderBy)&&f.orderBy!==!1&&"false"!==f.orderBy&&"$NONE"!==f.orderBy&&("$RANDOM"===f.orderBy?e=c.shuffleArray(e):e.sort(c.sortArrayByProperty(f.orderBy))),f.orderReverse!==!0&&"true"!==f.orderReverse||"$RANDOM"===f.orderBy||e.reverse(),f.maxItems>-1&&e.length>f.maxItems&&(e=e.splice(0,f.maxItems)),angular.isDefined(f.valueName)&&(a[f.valueName]=e),d[d.resultName]=e,d.$broadcast("apiNG.resultMerged",{resultName:d.resultName,valueName:d.valueName}),d.$emit("apiNG.resultMerged",{resultName:d.resultName,valueName:d.valueName})},this.apply=function(){d.$apply()}}]}}]),angular.module("jtt_aping").service("apingTimeHelper",function(){this.getTimestampFromDateString=function(a,b,c){if((angular.isUndefined(b)||isNaN(b))&&(b=1),(angular.isUndefined(c)||isNaN(c))&&(c=0),"string"==typeof a){var d=a.split(/[^0-9]/);try{return parseInt(Math.round(new Date(d[0],d[1]-1,d[2],d[3],d[4],d[5])/1e3*b)+c,10)}catch(e){return 0}}return 0}}).service("apingUtilityHelper",["apingInputObjects","apingDefaultSettings","$parse",function(a,b,c){this.getApiCredentials=function(a,c){return!(!b.apingApiKeys||!b.apingApiKeys[a])&&b.apingApiKeys[a][Math.floor(Math.random()*b.apingApiKeys[a].length)][c]},this.parseJsonFromAttributes=function(a,b,c){return this.parseRequestsFromAttributes(a,b,c)},this.getDifference=function(a,b){return a>b?a-b:b-a},this.parseRequestsFromAttributes=function(b,c,d){if("string"!=typeof b||!b)return[];var e=[],f=this.replaceSingleQuotesAndParseJson(b);return f.constructor===Array?angular.forEach(f,function(b){b.platform=c,d&&(angular.isUndefined(b.items)&&angular.isDefined(d.items)&&(b.items=d.items),angular.isUndefined(b.model)&&angular.isDefined(d.model)&&(b.model=d.model));var f=a.getNew("request",b);e.push(f)}):e.push(f),e},this.replaceSingleQuotesAndParseJson=function(a){return c(a)()},this.sortArrayByProperty=function(a){var b=1;return"-"===a[0]&&(b=-1,a=a.substr(1)),function(c,d){var e=c[a]d[a]?1:0;return e*b}},this.shuffleArray=function(a){for(var b=a.length-1;b>0;b--){var c=Math.floor(Math.random()*(b+1)),d=a[b];a[b]=a[c],a[c]=d}return a},this.removeNullIn=function(a,b){var c=b[a];if(null===c||void 0===c)delete b[a];else if("object"==typeof c)for(var d in c)this.removeNullIn(d,c)},this.removeNull=function(a){for(var b in a)this.removeNullIn(b,a)},this.removeDuplicateObjectsFromArray=function(a,b,c){var d,e=[];d=c?"aping_id":"apingStringified";var f="apingTempOrder";if(1===a.length)return a;angular.forEach(a,function(a,g){c||(a.$$hashKey=void 0,a[d]=JSON.stringify(a)),b===!0&&(a[f]=g),e.push(a)}),e.sort(this.sortArrayByProperty(d));var g,h=[];return angular.forEach(e,function(a){angular.isDefined(g)?angular.isDefined(a[d])&&a[d]!==g&&h.push(a):h.push(a),g=a[d],c||(a[d]=void 0)}),b===!0&&(h.sort(this.sortArrayByProperty(f)),angular.forEach(h,function(a){a[f]=void 0})),h},this.mergeDuplicateObjectsFromArray=function(a,b){var c=this,d=[],e="aping_id",f="apingTempOrder";if(1===a.length)return a;angular.forEach(a,function(a,c){b===!0&&(a[f]=c),d.push(a)}),d.sort(this.sortArrayByProperty(e));var g,h=[];return angular.forEach(d,function(a){c.removeNull(a),angular.isDefined(g)?angular.isDefined(a[e])&&a[e]!==g?h.push(a):h[h.length-1]=angular.merge(h[h.length-1],h[h.length-1],a):h.push(a),g=a[e]}),b===!0&&(h.sort(this.sortArrayByProperty(f)),angular.forEach(h,function(a){a[f]=void 0})),h},this.getTextFromHtml=function(a){return a=a.replace(/<br ?\/\>|<br ?\/&rt;|\
/g," "),a=a.replace(/<(?:.|\n)*?>/gm,"")},this.getFirstImageFromHtml=function(a){var b=/]+src="([^">]+)/g;return b.exec(a)},this.parseParametersFromUrl=function(a){var b={};return"string"==typeof a&&(b=JSON.parse('{"'+decodeURI(a.replace(/&/g,'","').replace(/=/g,'":"'))+'"}')),b},this.createIdByPropertiesForArray=function(a,b,c){var d=this;return!angular.isUndefined(c)&&angular.isString(c)||(c="aping_id"),angular.isDefined(a)&&angular.isArray(a)&&angular.forEach(a,function(a){a[c]=d.getIdByPropertiesForObject(a,b)}),a},this.getIdByPropertiesForObject=function(a,b){var c=this,d="";if(angular.isDefined(a)&&angular.isObject(a)){var e=[];"["===b.substr(0,1)?e=this.replaceSingleQuotesAndParseJson(b):e.push(b),angular.forEach(e,function(b){d+=c.getValueFromObjectByPropertyString(a,b)})}return d},this.getValueFromObjectByPropertyString=function(a,b,c){var d="";if(angular.isDefined(a)&&angular.isObject(a)){var e=b.split("."),f=a;angular.forEach(e,function(a){angular.isDefined(f[a])&&(f=f[a])}),angular.isDefined(f)&&(d=c&&angular.isObject(f)?JSON.stringify(f):f)}return d}}]),angular.module("jtt_aping").service("apingInputObjects",["apingDefaultSettings",function(a){this.getNew=function(b,c){var d={};switch(b){case"request":d=angular.extend({model:a.model},c)}return d}}]),angular.module("jtt_aping").service("apingModels",[function(){this.getNew=function(a,b){var c={platform:b,model:a};return c}}]),angular.module("jtt_aping_jsonloader",[]).directive("apingJsonloader",["apingUtilityHelper","jsonloaderFactory",function(a,b){return{require:"?aping",restrict:"A",replace:"false",link:function(c,d,e,f){var g=f.getAppSettings(),h=a.parseJsonFromAttributes(e.apingJsonloader,"jsonloader",g);h.forEach(function(c){if(c.path){var d={path:c.path};if(c.format&&"jsonp"===c.format.toLowerCase()?d.format="jsonp":d.format="json",angular.isUndefined(c.items)&&(c.items=g.items),0===c.items||"0"===c.items)return!1;(c.items<0||isNaN(c.items))&&(c.items=void 0),angular.isDefined(c.orderBy)&&!angular.isString(c.orderBy)&&(c.orderBy=void 0),!angular.isDefined(c.orderReverse)||c.orderReverse!==!0&&"true"!==c.orderReverse||(c.orderReverse=!0),angular.isDefined(c.xAuthToken)&&(d.xAuthToken=c.xAuthToken),angular.isDefined(c.Authorization)&&(d.Authorization=c.Authorization),b.getJsonData(d).then(function(b){var d=[];if(b.data){var e=b.data;angular.isDefined(c.resultProperty)&&(e=a.getValueFromObjectByPropertyString(e,c.resultProperty,!1)),e.constructor!==Array?d.push(e):(angular.extend(d,e),angular.isDefined(c.orderBy)&&("$RANDOM"===c.orderBy?d=a.shuffleArray(d):d.sort(a.sortArrayByProperty(c.orderBy))),angular.isDefined(c.orderReverse)&&c.orderReverse===!0&&"$RANDOM"!==c.orderBy&&d.reverse(),angular.isUndefined(c.items)?d=e:c.items>0&&d.length>c.items&&(d=d.splice(0,c.items)))}f.concatToResults(d)})}})}}}]).factory("jsonloaderFactory",["$http",function(a){var b={};return b.getJsonData=function(b){var c={};if("jsonp"===b.format){var d={method:"GET",params:{callback:"JSON_CALLBACK"}};return angular.isDefined(b.xAuthToken)&&(d.headers={"X-Auth-Token":b.xAuthToken}),angular.isDefined(b.Authorization)&&(d.headers={Authorization:b.Authorization}),a.jsonp(b.path,d)}var d={method:"GET",url:b.path,params:c};return angular.isDefined(b.xAuthToken)&&(d.headers={"X-Auth-Token":b.xAuthToken}),angular.isDefined(b.Authorization)&&(d.headers={Authorization:b.Authorization}),a(d)},b}]),angular.module("jtt_aping_json_string",[]).directive("apingJsonString",["apingUtilityHelper",function(a){return{require:"?aping",restrict:"A",replace:"false",link:function(b,c,d,e){var f=e.getAppSettings(),g=a.parseJsonFromAttributes(d.apingJsonString,"apingJsonString",f),h=[];g&&(g.constructor===Array?h=g:h.push(g),h.length>0&&e.concatToResults(h))}}}]),angular.module("jtt_aping_ng_array",[]).directive("apingNgArray",["apingUtilityHelper",function(a){return{require:"?aping",restrict:"A",replace:"false",link:function(b,c,d,e){var f=e.getAppSettings(),g=a.parseJsonFromAttributes(d.apingNgArray,"ngArray",f);g.forEach(function(c){if(c.name&&b[c.name]){if(angular.isUndefined(c.items)&&(c.items=f.items),0===c.items||"0"===c.items)return!1;(c.items<0||isNaN(c.items))&&(c.items=void 0);var d=[];b[c.name].constructor===Array?(d=b[c.name],angular.isDefined(c.orderBy)&&("$RANDOM"===c.orderBy?d=a.shuffleArray(d):d.sort(a.sortArrayByProperty(c.orderBy))),angular.isDefined(c.orderReverse)&&c.orderReverse===!0&&"$RANDOM"!==c.orderBy&&d.reverse(),angular.isDefined(c.items)&&c.items>0&&d.length>c.items&&(d=d.splice(0,c.items))):"object"==typeof b[c.name]&&null!==b[c.name]&&d.push(b[c.name]),d.length>0&&e.concatToResults(d)}})}}}]),angular.module("jtt_aping_local_storage",[]).directive("apingLocalStorage",["apingUtilityHelper","apingLocalStorage",function(a,b){return{require:"?aping",restrict:"A",replace:"false",link:function(c,d,e,f){var g=f.getAppSettings(),h=a.parseJsonFromAttributes(e.apingLocalStorage,"localStorage",g);h.forEach(function(c){if(c.key){if(angular.isUndefined(c.items)&&(c.items=g.items),0===c.items||"0"===c.items)return!1;(c.items<0||isNaN(c.items))&&(c.items=void 0),angular.isDefined(c.orderBy)&&!angular.isString(c.orderBy)&&(c.orderBy=void 0),!angular.isDefined(c.orderReverse)||c.orderReverse!==!0&&"true"!==c.orderReverse||(c.orderReverse=!0),b.get(c.key).then(function(b){var d=[];if(b){var e=b;angular.isDefined(c.resultProperty)&&(e=a.getValueFromObjectByPropertyString(b,c.resultProperty,!1)),angular.isArray(b)?(d=e,angular.isDefined(c.orderBy)&&("$RANDOM"===c.orderBy?d=a.shuffleArray(d):d.sort(a.sortArrayByProperty(c.orderBy))),angular.isDefined(c.orderReverse)&&c.orderReverse===!0&&"$RANDOM"!==c.orderBy&&d.reverse(),angular.isUndefined(c.items)?d=e:c.items>0&&d.length>c.items&&(d=d.splice(0,c.items))):d.push(e)}f.concatToResults(d)})}})}}}]).factory("apingLocalStorage",["$window","$q",function(a,b){var c=function(c,d){var e=b.defer();return e.resolve(a.localStorage&&a.localStorage.setItem(c,angular.toJson(d))),e.promise},d=function(c){var d=b.defer();return d.resolve(a.localStorage&&angular.fromJson(a.localStorage.getItem(c))),d.promise};return{set:c,get:d}}]),angular.module("jtt_aping_xml",[]).directive("apingXml",["apingUtilityHelper","xmlFactory","xmlService",function(a,b,c){return{require:"?aping",restrict:"A",replace:"false",link:function(d,e,f,g){var h=g.getAppSettings(),i=a.parseJsonFromAttributes(f.apingXml,"xml",h);i.forEach(function(d){if(d.path){var e={path:d.path};if(angular.isUndefined(d.items)&&(d.items=h.items),0===d.items||"0"===d.items)return!1;(d.items<0||isNaN(d.items))&&(d.items=void 0),angular.isDefined(d.orderBy)&&!angular.isString(d.orderBy)&&(d.orderBy=void 0),!angular.isDefined(d.orderReverse)||d.orderReverse!==!0&&"true"!==d.orderReverse||(d.orderReverse=!0),b.getData(e).then(function(b){var e=[];if(b.data){var f=c.xml2json(b.data);angular.isDefined(d.resultProperty)&&(f=a.getValueFromObjectByPropertyString(f,d.resultProperty,!1)),f.constructor!==Array?e.push(f):(angular.extend(e,f),angular.isDefined(d.orderBy)&&("$RANDOM"===d.orderBy?e=a.shuffleArray(e):e.sort(a.sortArrayByProperty(d.orderBy))),angular.isDefined(d.orderReverse)&&d.orderReverse===!0&&"$RANDOM"!==d.orderBy&&e.reverse(),angular.isUndefined(d.items)?e=f:d.items>0&&e.length>d.items&&(e=e.splice(0,d.items)))}g.concatToResults(e)})}})}}}]).factory("xmlFactory",["$http",function(a){var b={};return b.getData=function(b){var c={};return a({method:"GET",url:b.path,params:c})},b}]).service("xmlService",function(){var a=this;this.cleanXML=function(b){return b=b.replace(/\n|\t|\r/g,""),b=b.replace(/ {1,}<|\t{1,} {1,}|>\t{1,}/g,">"),b=b.replace(/<\?[^>]*\?>/g,""),b=a.replaceSelfClosingTags(b),b=a.replaceAloneValues(b),b=a.replaceAttributes(b)},this.replaceSelfClosingTags=function(a){var b=a.match(/<[^\/][^>]*\/>/g);if(b)for(var c=0;c";var f=d.match(/[^<][\w+$]*/)[0],g="",h="<"+f+">",i=e.match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g);if(i)for(var j=0;j"+m+""}h+=g,a=a.replace(d,h)}return a},this.replaceAloneValues=function(a){var b=a.match(/<[^\/][^>][^<]+\s+.[^<]+[=][^<]+>{1}([^<]+)/g);if(b)for(var c=0;c")+1),f=d.substring(d.indexOf(">")+1),g=e+"<_@ttribute>"+f+"";a=a.replace(d,g)}return a},this.replaceAttributes=function(a){var b=a.match(/<[^\/][^>][^<]+\s+.[^<]+[=][^<]+>/g);if(b)for(var c=0;c",g=d.match(/(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g);if(g)for(var h=0;h"+k+""}a=a.replace(d,f)}return a},this.xml2json=function(b){b=this.cleanXML(b);for(var c,d,e,f,g,h={};b.match(/<[^\/][^>]*>/);)g=b.match(/<[^\/][^>]*>/)[0],c=g.substring(1,g.length-1),d=b.indexOf(g.replace("<","]*>/)?a.xml2json(e):e,void 0===h[c]?h[c]=f:Array.isArray(h[c])?h[c].push(f):h[c]=[h[c],f],b=b.substring(2*g.length+1+e.length);return h}}); \ No newline at end of file diff --git a/src/plugin-jsonloader.js b/src/plugin-jsonloader.js index 2bc39ec..7c45983 100644 --- a/src/plugin-jsonloader.js +++ b/src/plugin-jsonloader.js @@ -48,6 +48,11 @@ angular.module('jtt_aping_jsonloader', []) if (angular.isDefined(request.xAuthToken)) { requestObject.xAuthToken = request.xAuthToken; } + + if (angular.isDefined(request.Authorization)) { + requestObject.Authorization = request.Authorization; + } + jsonloaderFactory.getJsonData(requestObject) .then(function (_data) { var resultArray = []; @@ -112,19 +117,17 @@ angular.module('jtt_aping_jsonloader', []) } } + if (angular.isDefined(_requestObject.Authorization)) { + httpObject.headers = { + 'Authorization': _requestObject.Authorization + } + } + return $http.jsonp( _requestObject.path, httpObject ); - /* - return $http({ - method: 'JSONP', - url: _requestObject.path, - params: {callback: 'JSON_CALLBACK'}, - }); - */ - } else { var httpObject = { @@ -139,6 +142,12 @@ angular.module('jtt_aping_jsonloader', []) } } + if (angular.isDefined(_requestObject.Authorization)) { + httpObject.headers = { + 'Authorization': _requestObject.Authorization + } + } + return $http(httpObject); } };