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,200 @@
(function () {
'use strict';
/**
* DESCRIPTION:
*
* Special map object for easy access to both an object map and an array.
*
* This object mimics maps as defined by the Java map interface.
*/
angular.module('portal.common.services.map', ['ui.router'])
.factory('Map', mapService);
function mapService() {
/**
* Defines a map object that allows storage of data as array and object.
* @param {type} accessor The value used on elements for the key, e.g. element.id => "id"
* @returns {map_L35.mapService.Map}
*/
function Map(accessor, groupKeys) {
var map = this;
/** Array Storage. */
map.values = [];
/** Object Storage. */
map.object = {};
/** Group Storage. */
map.groups = {};
/** Group keys. */
map.groupKeys = [];
if (angular.isArray(groupKeys)) {
map.groupKeys = groupKeys.slice();
} else if (angular.isDefined(groupKeys)) {
map.groupKeys.push(groupKeys);
}
//Initialize Groups
for (var i = 0; i < map.groupKeys.length; i++) {
map.groups[map.groupKeys[i]] = {};
}
/**
* defines the part of the element that comprises the key.
* May be undefined, not guaranteed.
*/
map.accessor = accessor;
/**
* @returns {Boolean} True if emtpy.
*/
map.isEmpty = function () {
return map.values.length === 0;
};
/**
* Determins if an item is stored inside based on its key.
* @param {type} key
* @returns {Boolean} True if contained.
*/
map.containsKey = function (key) {
return angular.isDefined(map.object[key]);
};
/**
* Searches for element, via loop.
* @param {type} value
* @returns {Boolean} True if element found.
*/
map.containsValue = function (value) {
for (var i = 0; i < map.values.length; i++) {
if (map.values[i] === value) {
return true;
}
}
return false;
};
/**
* @param {type} key
* @returns {type} element found by key.
*/
map.get = function (key) {
return map.object[key];
};
/**
* Inserts element into both array and object storage.
* @param {type} key
* @param {type} value
*/
map.put = function (key, value) {
//Remove elements.
map.removeByKey(key);
//Insert.
map.values.push(value);
map.object[key] = value;
//Group
for (var i = 0; i < map.groupKeys.length; i++) {
var groupKey = map.groupKeys[i];
var group = value[groupKey];
if (!angular.isDefined(map.groups[groupKey][group])) {
map.groups[groupKey][group] = new Map(map.accessor);
}
map.groups[groupKey][group].put(key, value);
}
return value;
};
/**
* Allows access to a group map.
* Always returns a valid map, just may be empty.
* @param {type} group
* @returns {map_L35.mapService.Map}
*/
map.getGroup = function (groupKey, group) {
if (angular.isDefined(map.groups[groupKey][group])) {
return map.groups[groupKey][group];
}
return new Map();
};
/**
* Removes an element from both storage interfaces.
* @param {type} key
* @returns {Boolean} T/F if removed or not.
*/
map.removeByKey = function (key) {
if (!map.containsKey(key)) {
return false;
}
var value = map.object[key];
//Remove from Array.
for (var i = 0; i < map.values.length; i++) {
if (map.values[i] === value) {
map.values.splice(i, 1);
break;
}
}
//Remove from object.
delete map.object[key];
//Remove from groups.
for (var j = 0; j < map.groupKeys.length; j++) {
var groupKey = map.groupKeys[j];
var group = value[groupKey];
if (angular.isDefined(map.groups[groupKey][group])) {
map.groups[groupKey][group].removeByKey(key);
}
}
return true;
};
/**
* Empties the storage objects.
*/
map.clear = function () {
map.object = {};
map.values = [];
};
map.init = function (data, Constructor, accessor) {
map.clear();
map.accessor = accessor;
if (angular.isArray(data)) {
for (var i = 0; i < data.length; i++) {
var obj = new Constructor(data[i]);
map.put(obj[accessor], obj);
}
}
};
Object.defineProperties(map, {
/**
* Returns the keys from the object.
*/
keySet: {
get: function () {
return Object.keys(map.object);
}
},
length: {
get: function () {
return map.values.length;
}
}
});
}
Map.create = function (data, Constructor, accessor, groupKeys) {
var map = new Map(accessor, groupKeys);
map.init(data, Constructor, accessor);
return map;
};
return Map;
} //end mapService function
})();