Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Main Application Services
- * Some functions require jQuery
- * PJW 20/11/2014
- */
- 'use strict';
- (function(window, $, a8Helper){
- var app = angular.module('dashboard-services',[]);
- app.factory('htmlScreenshot', function(){
- return {
- /**
- * [create description]
- * @param {[type]} domElem [description]
- * @param {[type]} params [description]
- * @param {Function} callback [description]
- * @return {[type]} [description]
- */
- create : function(domElem, params, callback){
- if(window.html2canvas===undefined) return false;
- window.html2canvas( domElem, {
- onrendered: function(canvas) {
- //clip the resulting image to remove transparent areas
- //http://stackoverflow.com/a/8358434/2640741
- var smallCanvas = window.document.createElement('canvas');
- smallCanvas.width = canvas.width;
- smallCanvas.height = $(domElem).parent().height();
- var smCtx = smallCanvas.getContext('2d');
- smCtx.drawImage(canvas, 0,0);
- callback(smallCanvas.toDataURL());
- }
- });
- }
- };
- });
- app.factory('sol', function(){
- return {
- create : function(scope, objectString, init){
- var targetObj = scope.$eval(objectString);
- if(targetObj===undefined) {
- return false;
- }
- var targetObjCopy = $.extend(true,{},targetObj);
- var SyncObject = {
- sync : function(){
- var obj = scope.$eval(objectString + ' = newVal', { newVal : this.o });
- },
- update : function(){
- var pulledValue = scope.$eval(objectString);
- this.o = $.extend(true,{},pulledValue);
- },
- o : (angular.equals({}, targetObjCopy) && typeof init === "object" ? init : targetObjCopy)
- };
- return Object.create(SyncObject);
- }
- };
- });
- app.factory('jsonStylesheet', ['APP_CONFIG', function(APP_CONFIG){
- function updateStylesheet($styleElem, value){
- var css = jsonToCSS(value);
- $styleElem.html(css);
- }
- function jsonToCSS(json){
- if(typeof json !== 'object' || json===null)
- return false;
- var output = '';
- $.each(json, function(rule, prop){
- output += '\n'+rule + '{';
- $.each(prop, function(propname, propval){
- output += propname + ' : ' + propval + ';';
- });
- output+= '}';
- });
- return output;
- }
- return {
- create : function(scope, objectString){
- var id = 'jsonStylesheet-'+a8Helper.uniqueInst('jsonStylesheet');
- var $styleElem = $('<style id="'+ id +'"></style>');
- $('head').append($styleElem);
- scope.$watch(objectString, function(newVal, oldVal){
- updateStylesheet($styleElem, newVal);
- }, true);
- return $styleElem;
- }
- };
- }]);
- app.factory('a8Database', ['$http', '$q', 'APP_CONFIG', function($http, $q, APP_CONFIG){
- //set global timeout for requests
- var timeout = 100000;
- return {
- /**
- * Object containing the methods of get and set for the data properties
- * @type {Object}
- */
- data : {
- /**
- * Constructs and executes an $http request to the Indic8 database server
- * @param {Object} data - A JSON object to store in the database
- * @return {Object} - Returns a promise
- */
- makeRequest : function(data){
- var encodedData = JSON.stringify(data);
- var req = {
- method: 'POST',
- url: APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.serverletPath,
- withCredentials : true,
- data : encodedData,
- cache : false,
- timeout : timeout
- };
- var defer = $q.defer();
- $http(req).then(function(response){
- if(response.data.error!==undefined){
- defer.reject(response.data.error.message, response.data.error.code);
- }else {
- defer.resolve(response);
- }
- }, function(e){
- defer.reject(e);
- });
- return defer.promise;
- },
- /**
- * Get data stored in the Indic8 properties database
- * @param {String} key - The key of the property
- * @param {Boolean} isPrivate - Whether or not the data is publically accessible
- * @param {Boolean} isLarge - If true, will store the property as type Blob
- * @return {Object} - Promise from the request
- */
- get : function(key, isPrivate, isLarge){
- isPrivate = (isPrivate!==undefined ? isPrivate : false);
- isLarge = (isLarge!==undefined ? isLarge : false);
- var data = {
- apiVersion: "2.0",
- method :"property.get",
- params : {
- property: APP_CONFIG.dataPrefix + key,
- private : isPrivate,
- blob : isLarge
- }
- };
- return this.makeRequest(data);
- },
- /**
- * Set a property to the Indic8 properties database
- * @param {String} key - The key of the property
- * @param {Object} value - An object to store in the database
- * @param {Boolean} isPrivate - Whether or not the data is publically accessible
- * @param {Boolean} isLarge - If true, will store the property as type Blob
- * @return {Object} - Promise from the request
- */
- set : function(key, value, isPrivate, isLarge){
- isPrivate = (isPrivate!==undefined ? isPrivate : false);
- isLarge = (isLarge!==undefined ? isLarge : true);
- var data = {
- apiVersion: "2.0",
- method :"property.update",
- params : {
- property: APP_CONFIG.dataPrefix + key,
- value : $.trim(value),
- private : isPrivate,
- blob : isLarge
- }
- };
- return this.makeRequest(data);
- }
- },
- file : {
- download : function(key, isPrivate, returnURL){
- /**
- * download file
- */
- isPrivate = (isPrivate!==undefined ? isPrivate : false);
- var data = {
- apiVersion: "2.0",
- method :"file.download",
- params : {
- file: key,//APP_CONFIG.dataPrefix + key,
- private : isPrivate
- }
- };
- var encoded = encodeURIComponent(JSON.stringify(data));
- var url = APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.fileServerletPath + '?data='+ encoded;
- if(returnURL){
- return url;
- }
- var req = {
- method: 'GET',
- url: url,
- withCredentials : true,
- cache : false,
- timeout : timeout
- };
- var defer = $q.defer();
- $http(req).then(function(response){
- if(response.data.error!==undefined){
- defer.reject(response.data.error.message, response.data.error.code);
- }else {
- defer.resolve(response);
- }
- }, function(e){
- defer.reject(e);
- });
- return defer.promise;
- },
- upload : function(key, blob, isPrivate){
- /**
- * upload file - currently taken care of by the dropzone module
- */
- isPrivate = (isPrivate!==undefined ? isPrivate : false);
- var uploadUrl = APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.fileServerletPath;
- var fd = new FormData();
- fd.append('file', blob, APP_CONFIG.dataPrefix + key);
- fd.append('data', "{'method':'file.upload', 'params':{'private':true}}");
- var req = {
- withCredentials : true,
- cache : false,
- transformRequest: angular.identity,
- headers: {'Content-Type': undefined},
- timeout : timeout
- };
- return $http.post(uploadUrl, fd, req);
- },
- delete : function(key, isPrivate){
- isPrivate = (isPrivate!==undefined ? isPrivate : false);
- var uploadUrl = APP_CONFIG.protocol + APP_CONFIG.a8DatabaseURL+APP_CONFIG.endpoint+APP_CONFIG.fileServerletPath;
- var blob = new Blob([0]);
- var fd = new FormData();
- fd.append('file', blob, APP_CONFIG.dataPrefix + key);
- fd.append('data', "{'method':'file.upload', 'params':{'private':true}}");
- var req = {
- withCredentials : true,
- cache : false,
- transformRequest: angular.identity,
- headers: {'Content-Type': undefined},
- timeout : timeout
- };
- return $http.post(uploadUrl, fd, req);
- }
- }
- };
- }]);
- })(window, jQuery, a8Helper);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement