(function() { angular.module('appSdk') .factory('DataShareBusiness', ['$location', function ($location) { var data = { Groups: null, SearchSeparator: "~", CurrentGroups: [], DateFormat: "dd MMM yyyy", HourFormat: "HH'h'mm", setCurrentGroup: function(group) { data.CurrentGroups = []; while (group != null) { data.CurrentGroups.push(group); group = group.Parent; } data.CurrentGroups.reverse(); $location.search(data.getSearchString(-1)); }, getCurrentGroup: function() { if (data.CurrentGroups.length == 0) { return null; } return data.CurrentGroups[data.CurrentGroups.length - 1]; }, getCurrentGroupOrRoot: function() { var group = data.getCurrentGroup(); if (group == null) { return { Id: null, Groups: data.Groups, Name: "Root", Type: 1, ParentId: -1 } } return group; }, getSearchString: function(group) { if (group == -1 && data.CurrentGroups.length != 0) { group = data.CurrentGroups[data.CurrentGroups.length - 1]; } if (group == null) { return ""; } var search = []; while (group != null){ search.push(group.Name); group = group.Parent; } return search.reverse().join(data.SearchSeparator); }, getGroupUrl: function(group) { var search = data.getSearchString(group); return "#" + $location.path() + $location.hash() + "?" + search; }, setFromSearchString: function(string) { if (string == null) { data.setCurrentGroup(null); return; } var group = {Groups: data.Groups}; var names = string.split(data.SearchSeparator); names.forEach(function(name) { var g = group.Groups.find(function(g) { return g.Name == name; }); if (g != null) { group = g; } }); data.setCurrentGroup(group); }, upCurrentGroup: function() { if (data.CurrentGroups.length > 1) { data.setCurrentGroup(data.CurrentGroups[data.CurrentGroups.length - 2]); } else if (data.CurrentGroups.length == 1) { data.setCurrentGroup(null); } } }; return data; }]); })();