Advertisement
Guest User

services

a guest
Apr 17th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Main Application Services
  3.  * Some functions require jQuery
  4.  * PJW 20/11/2014
  5.  */
  6.  
  7. 'use strict';
  8.  
  9. (function(window, $, a8Helper){
  10.  
  11.   var app = angular.module('dashboard-services',[]);
  12.  
  13.   app.factory('htmlScreenshot', function(){
  14.     return {
  15.         /**
  16.          * [create description]
  17.          * @param  {[type]}   domElem  [description]
  18.          * @param  {[type]}   params   [description]
  19.          * @param  {Function} callback [description]
  20.          * @return {[type]}            [description]
  21.          */
  22.         create : function(domElem, params, callback){
  23.           if(window.html2canvas===undefined) return false;
  24.           window.html2canvas( domElem, {
  25.             onrendered: function(canvas) {
  26.               //clip the resulting image to remove transparent areas
  27.               //http://stackoverflow.com/a/8358434/2640741
  28.  
  29.               var smallCanvas = window.document.createElement('canvas');
  30.               smallCanvas.width = canvas.width;
  31.               smallCanvas.height = $(domElem).parent().height();
  32.               var smCtx = smallCanvas.getContext('2d');
  33.  
  34.               smCtx.drawImage(canvas, 0,0);
  35.  
  36.               callback(smallCanvas.toDataURL());
  37.             }
  38.           });
  39.       }
  40.     };
  41.   });
  42.  
  43.  
  44.   app.factory('sol', function(){
  45.     return {
  46.       create : function(scope, objectString, init){
  47.         var targetObj = scope.$eval(objectString);
  48.         if(targetObj===undefined) {
  49.           return false;
  50.         }
  51.         var targetObjCopy = $.extend(true,{},targetObj);
  52.  
  53.         var SyncObject = {
  54.           sync : function(){
  55.             var obj = scope.$eval(objectString + ' = newVal', { newVal : this.o });
  56.           },
  57.           update : function(){
  58.             var pulledValue = scope.$eval(objectString);
  59.             this.o = $.extend(true,{},pulledValue);
  60.           },
  61.           o : (angular.equals({}, targetObjCopy) && typeof init === "object" ? init : targetObjCopy)
  62.         };
  63.  
  64.         return Object.create(SyncObject);
  65.       }
  66.     };
  67.   });
  68.  
  69.  
  70.   app.factory('jsonStylesheet', ['APP_CONFIG', function(APP_CONFIG){
  71.  
  72.     function updateStylesheet($styleElem, value){
  73.       var css = jsonToCSS(value);
  74.       $styleElem.html(css);
  75.     }
  76.  
  77.     function jsonToCSS(json){
  78.       if(typeof json !== 'object' || json===null)
  79.         return false;
  80.       var output = '';
  81.       $.each(json, function(rule, prop){
  82.         output += '\n'+rule + '{';
  83.         $.each(prop, function(propname, propval){
  84.           output += propname + ' : ' + propval + ';';
  85.         });
  86.         output+= '}';
  87.       });
  88.       return output;
  89.     }
  90.  
  91.     return {
  92.       create : function(scope, objectString){
  93.         var id = 'jsonStylesheet-'+a8Helper.uniqueInst('jsonStylesheet');
  94.         var $styleElem = $('<style id="'+ id +'"></style>');
  95.         $('head').append($styleElem);
  96.         scope.$watch(objectString, function(newVal, oldVal){
  97.           updateStylesheet($styleElem, newVal);
  98.         }, true);
  99.  
  100.         return $styleElem;
  101.       }
  102.     };
  103.  
  104.   }]);
  105.  
  106.  
  107.   app.factory('a8Database', ['$http', '$q', 'APP_CONFIG', function($http, $q, APP_CONFIG){
  108.     //set global timeout for requests
  109.     var timeout = 100000;
  110.  
  111.     return {
  112.       /**
  113.        * Object containing the methods of get and set for the data properties
  114.        * @type {Object}
  115.        */
  116.       data : {
  117.         /**
  118.          * Constructs and executes an $http request to the Indic8 database server
  119.          * @param  {Object} data  - A JSON object to store in the database
  120.          * @return {Object}       - Returns a promise
  121.          */
  122.         makeRequest : function(data){
  123.  
  124.           var encodedData = JSON.stringify(data);
  125.           var req = {
  126.             method: 'POST',
  127.             url: APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.serverletPath,
  128.             withCredentials : true,
  129.             data : encodedData,
  130.             cache : false,
  131.             timeout : timeout
  132.           };
  133.  
  134.           var defer = $q.defer();
  135.           $http(req).then(function(response){
  136.             if(response.data.error!==undefined){
  137.               defer.reject(response.data.error.message, response.data.error.code);
  138.             }else {
  139.               defer.resolve(response);
  140.             }
  141.           }, function(e){
  142.             defer.reject(e);
  143.           });
  144.           return defer.promise;
  145.  
  146.         },
  147.         /**
  148.          * Get data stored in the Indic8 properties database
  149.          * @param  {String}  key       - The key of the property
  150.          * @param  {Boolean} isPrivate - Whether or not the data is publically accessible
  151.          * @param  {Boolean} isLarge   - If true, will store the property as type Blob
  152.          * @return {Object}            - Promise from the request
  153.          */
  154.         get : function(key, isPrivate, isLarge){
  155.           isPrivate = (isPrivate!==undefined ? isPrivate : false);
  156.           isLarge = (isLarge!==undefined ? isLarge : false);
  157.  
  158.           var data = {
  159.             apiVersion: "2.0",
  160.             method :"property.get",
  161.             params : {
  162.               property: APP_CONFIG.dataPrefix + key,
  163.               private : isPrivate,
  164.               blob : isLarge
  165.             }
  166.           };
  167.  
  168.           return this.makeRequest(data);
  169.         },
  170.         /**
  171.          * Set a property to the Indic8 properties database
  172.          * @param  {String}  key       - The key of the property
  173.          * @param  {Object}  value     - An object to store in the database
  174.          * @param  {Boolean} isPrivate - Whether or not the data is publically accessible
  175.          * @param  {Boolean} isLarge   - If true, will store the property as type Blob
  176.          * @return {Object}            - Promise from the request
  177.          */
  178.         set : function(key, value, isPrivate, isLarge){
  179.  
  180.           isPrivate = (isPrivate!==undefined ? isPrivate : false);
  181.           isLarge = (isLarge!==undefined ? isLarge : true);
  182.  
  183.           var data = {
  184.             apiVersion: "2.0",
  185.             method :"property.update",
  186.             params : {
  187.               property: APP_CONFIG.dataPrefix + key,
  188.               value : $.trim(value),
  189.               private : isPrivate,
  190.               blob : isLarge
  191.             }
  192.           };
  193.  
  194.           return this.makeRequest(data);
  195.         }
  196.       },
  197.  
  198.  
  199.  
  200.       file : {
  201.         download : function(key, isPrivate, returnURL){
  202.           /**
  203.            * download file
  204.            */
  205.  
  206.           isPrivate = (isPrivate!==undefined ? isPrivate : false);
  207.  
  208.           var data = {
  209.             apiVersion: "2.0",
  210.             method :"file.download",
  211.             params : {
  212.               file: key,//APP_CONFIG.dataPrefix + key,
  213.               private : isPrivate
  214.             }
  215.           };
  216.  
  217.           var encoded = encodeURIComponent(JSON.stringify(data));
  218.  
  219.           var url = APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.fileServerletPath + '?data='+ encoded;
  220.  
  221.           if(returnURL){
  222.             return url;
  223.           }
  224.  
  225.           var req = {
  226.             method: 'GET',
  227.             url: url,
  228.             withCredentials : true,
  229.             cache : false,
  230.             timeout : timeout
  231.           };
  232.  
  233.           var defer = $q.defer();
  234.  
  235.           $http(req).then(function(response){
  236.             if(response.data.error!==undefined){
  237.               defer.reject(response.data.error.message, response.data.error.code);
  238.             }else {
  239.  
  240.               defer.resolve(response);
  241.             }
  242.           }, function(e){
  243.             defer.reject(e);
  244.           });
  245.  
  246.           return defer.promise;
  247.         },
  248.         upload : function(key, blob, isPrivate){
  249.  
  250.           /**
  251.            * upload file - currently taken care of by the dropzone module
  252.            */
  253.  
  254.           isPrivate = (isPrivate!==undefined ? isPrivate : false);
  255.  
  256.           var uploadUrl = APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.fileServerletPath;
  257.  
  258.           var fd = new FormData();
  259.           fd.append('file', blob, APP_CONFIG.dataPrefix + key);
  260.           fd.append('data', "{'method':'file.upload', 'params':{'private':true}}");
  261.  
  262.           var req = {
  263.             withCredentials : true,
  264.             cache : false,
  265.             transformRequest: angular.identity,
  266.             headers: {'Content-Type': undefined},
  267.             timeout : timeout
  268.           };
  269.  
  270.           return $http.post(uploadUrl, fd, req);
  271.  
  272.         },
  273.         delete : function(key, isPrivate){
  274.  
  275.           isPrivate = (isPrivate!==undefined ? isPrivate : false);
  276.  
  277.           var uploadUrl = APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.fileServerletPath;
  278.           var blob = new Blob([0]);
  279.           var fd = new FormData();
  280.           fd.append('file', blob, APP_CONFIG.dataPrefix + key);
  281.           fd.append('data', "{'method':'file.upload', 'params':{'private':true}}");
  282.  
  283.           var req = {
  284.             withCredentials : true,
  285.             cache : false,
  286.             transformRequest: angular.identity,
  287.             headers: {'Content-Type': undefined},
  288.             timeout : timeout
  289.           };
  290.  
  291.           return $http.post(uploadUrl, fd, req);
  292.         }
  293.       }
  294.  
  295.     };
  296.  
  297.   }]);
  298.  
  299.  
  300. })(window, jQuery, a8Helper);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement