DataStoreSrvc.js

angular.module('homepage').
// Store templates, location and widget data
factory('DataStoreSrvc', function (ArrayUtility, ProcessTimelineService, SidebarHelperSrvc, TemplatePreprocessor, TemplateTimelineService, WidgetHelperSrvc) {
	/**
	 * Store all the data received till now from the server
	 */
	var DataStore = {
		templates: []
	};

	/**
	 * Since we need to update the UI on change of items,
	 * we maintain seperate filtered object which would be updated
	 * on change of date.
	 */
	var FilteredData = {
		templates: [],
		habits: [],
		widgets: WidgetHelperSrvc.getWidgetsData(),
		sidebar: SidebarHelperSrvc.getSidebarData()
	};

	return {
		/**
		 * Add new template to DataStore.
		 * Get templates for today and build new timeline based on it.
		 */
		add: function (template) {
			if(!template) return;

			TemplatePreprocessor.removeEmptyWorkoutAndSet(template);

			DataStore.templates.push(template);
			// Update timeline data
			var templates = TemplateTimelineService.getItemsByDate(DataStore.templates);
			templates = TemplateTimelineService.excludeItemsByType(templates, 'HABT');
			var habits = TemplateTimelineService.getItemsByDate(DataStore.templates);
			habits = TemplateTimelineService.filterItemsByType(habits, 'HABT');
			// Preserve array reference after filtering
			FilteredData.templates = ArrayUtility.reassignArray(FilteredData.templates, templates);
			ProcessTimelineService.processTimeline(FilteredData.templates);
			FilteredData.habits = ArrayUtility.reassignArray(FilteredData.habits, habits);

			// Update widgets data
			FilteredData.widgets = WidgetHelperSrvc.update(template);

			// Update sidebar data
			FilteredData.sidebar = SidebarHelperSrvc.update(template);
		},
		/**
		 * Get posts based on current selected date
		 */
		getPosts: function () {
			var templates = TemplateTimelineService.getItemsByDate(DataStore.templates);
			templates = TemplateTimelineService.excludeItemsByType(templates, 'HABT');
			// Preserve array reference after filtering
			FilteredData.templates = ArrayUtility.reassignArray(FilteredData.templates, templates);
			ProcessTimelineService.processTimeline(FilteredData.templates);
			return FilteredData.templates;
		},
		/**
		 * Get habits based on current selected date
		 */
		getHabits: function () {
			var templates = TemplateTimelineService.getItemsByDate(DataStore.templates);
			templates = TemplateTimelineService.filterItemsByType(templates, 'HABT');
			// Preserve array reference after filtering
			FilteredData.habits = ArrayUtility.reassignArray(FilteredData.habits, templates);
			return FilteredData.habits;
		},
		/**
		 * Get widgets based on current selected date
		 */
		getWidgetsData: function () {
			FilteredData.widgets = WidgetHelperSrvc.getWidgetsByDate();
			return FilteredData.widgets;
		},
		/**
		 * Get sidebar based on current selected date
		 */
		getSidebarData: function () {
			FilteredData.sidebar = SidebarHelperSrvc.getSidebarByDate();
			return FilteredData.sidebar;
		},
		/**
		 * Update all content based on current selected date
		 */
		updateAll: function () {
			// Update data based on current date
			this.getPosts();
			this.getHabits();
			this.getWidgetsData();
			this.getSidebarData();
		},
		/**
		 * Reset data store
		 */
		resetAll: function () {
			DataStore.templates.length = 0;
			WidgetHelperSrvc.resetAll();
			SidebarHelperSrvc.resetAll();
		}
	};
});