Initial commit

This commit is contained in:
Isaac Parenteau committed 2018-07-07 20:43:51 -05:00
commit 880e39de2e
511 files changed
+38363

No files matched your search

@@ -0,0 +1,276 @@
angular.module('portal.common.services.portalUtils', ['ui.router'])
.service('portalUtils', function ($sce, $window, $mdToast, $mdDialog) {
this.trustAsHtml = function (string){
return $sce.trustAsHtml(string);
};
this.setScopeValue = function(scope, key, value, recurse) {
if (scope === undefined || scope === null) {
return;
}
if (key in scope) {
scope[key] = value;
return;
}
if (recurse) {
this.setScopeValue(scope.$parent, key, value, recurse);
}
};
this.hashSize = function(hash) {
var size = 0, key;
for (key in hash) {
if (hash.hasOwnProperty(key)) {
size++;
}
}
return size;
};
this.stripHtml = function(htmlStr) {
var newStr = htmlStr.replace(/<(?:.|\n)*?>/gm, '');
return newStr;
};
function inArray(array, comparer) {
for (var i = 0; i < array.length; i++) {
if (comparer(array[i])) {
return true;
}
}
return false;
}
/**
* Push a value onto an array if it doesnt exist
*/
this.arrayPushIfNotExist = function(array, element, comparer) {
if (!inArray(array, comparer)) {
array.push(element);
}
};
this.isIE = function() {
var sAgent = $window.navigator.userAgent;
var idx = sAgent.indexOf('MSIE');
// If IE, return version number.
if (idx > 0) {
return true;
}
// If IE 11 then look for Updated user agent string.
else if (sAgent.match(/Trident\/7\./)) {
return true;
}
else {
return false; //It is not IE
}
};
this.confirmBox = function(id, title, msg, showConfirm, returnFunction, newTemplate) {
var template, size;
if(newTemplate !== null && newTemplate !== undefined) {
template = newTemplate;
} else {
template = 'common/partials/templates/confirm-window.tmpl.html';
}
$mdDialog.show({
templateUrl: template,
skipHide: true,
multiple: true,
locals: {
modalInstance: {
btnId: id,
confirmTitle: title,
confirmMessage: msg,
showConfirmMessage: showConfirm
}
},
controller: ['$scope', 'modalInstance' , '$mdDialog', function($scope, modalInstance, $mdDialog) {
$scope.confirmTitle = modalInstance.confirmTitle;
$scope.confirmMessage = [];
if (angular.isArray(modalInstance.confirmMessage)) {
$scope.confirmMessage = modalInstance.confirmMessage;
} else {
$scope.confirmMessage.push(modalInstance.confirmMessage);
}
$scope.showConfirmMessage = modalInstance.showConfirmMessage;
$scope.btnId = modalInstance.btnId;
$scope.data = {
checkbox: false,
confirm: false,
choice: 'merge'
};
$scope.confirm = function() {
$scope.data.confirm = true;
$mdDialog.hide($scope.data);
};
$scope.cancel = function() {
$scope.data.confirm = false;
$mdDialog.hide($scope.data);
};
}]
}).then(function(disabledConfirm) {
returnFunction(disabledConfirm);
});
};
var toastPos = {
bottom: false,
top: false,
left: false,
right: false
};
function validPosition(position) {
return toastPos[position] !== undefined;
}
/**
* Allows for a toast message to be displayed on screen
* @param message: The message to be displayed
* @param position: The Position of the toast box (valid values are ['top'|'bottom'|'left'|'right'])
* @param duration: How long the toast message should be displayed
*/
this.toast = function(message, position, duration) {
toastPos.bottom = false;
toastPos.top = false;
toastPos.left = false;
toastPos.right = false;
if (Array.isArray(position)) {
for (var i = 0; i < position.length; i++) {
if (validPosition(position[i])) {
toastPos[position[i]] = true;
}
}
} else if (validPosition(position)) {
toastPos[position] = true;
}
var toastPosition = angular.extend({}, toastPos);
var pinTo = Object.keys(toastPosition)
.filter(function(pos) { return toastPosition[pos]; })
.join(' ');
$mdToast.show(
$mdToast.simple()
.textContent(message)
.position(pinTo)
.hideDelay(duration)
);
};
/**
* Converts space separated string to title case.
*
* See: http://stackoverflow.com/a/196991/1516011
*
* @param {type} str Any 'space separated string'
* @returns {undefined} returns 'space separated string' as 'Space Separated String'
*/
this.toTitleCase = function (str) {
if (str === null) {
return;
}
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
/**
* Helper function to make calls to possibly undefined functions.
* First argument is the function to call.
* Rest of the arguements get passed to the callee
* @returns {undefined}
*/
this.call = function (callee) {
if (angular.isFunction(callee)) {
var calleeArgs = Array.prototype.slice.call(arguments, 1);
return callee.apply(this, calleeArgs);
}
};
this.createHash = function(array, Constructor, keyField) {
var hash = {};
for (var i in array) {
var obj = new Constructor(array[i]);
hash[obj[keyField]] = obj;
}
return hash;
} ;
/**
* Create an array
*
* @param {type} array An array of server objects;
* @param {type} Constructor A constructor to wrap the given data.
* @returns {Array} A wrapped array of the given type.
*/
this.createArray = function (array, Constructor) {
var objectArray = [];
for (var i in array) {
objectArray.push(new Constructor(array[i]));
}
return objectArray;
};
this.validEmail = function(text) {
var EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var test = EMAIL_REGEX.test(text);
return test;
};
/**
* Allows for quick and easy inheritance of a parent class.
* @param {type} sub
* @param {type} parent
* @returns {unresolved}
*/
this.inherit = function (sub, parent) {
sub.prototype = Object.create(parent.prototype); //Available in Chrome (5), FF (4), IE (9), Safari (5).
sub.prototype.constructor = sub;
return sub;
};
this.arrayLength = function(array) {
return Object.keys(array).length;
};
this.createMap = function(data, Constructor, accessor) {
var map = {};
if (angular.isArray(data)) {
for (var i = 0; i < data.length; i++) {
var obj = new Constructor(data[i]);
map[obj[accessor]] = obj;
}
}
return map;
};
/* jshint ignore:start */
this.sortHashTable = function(hash) {
var tmpArray = [];
$.each(hash, function(key, value){
tmpArray[key] = value;
});
tmpArray = tmpArray.sort();
var tmpHash = {};
for (var index in tmpArray) {
tmpHash[index] = tmpArray[index];
}
return tmpHash;
};
/* jshint ignore:end */
});