jhanink
8/23/2013 - 4:48 AM

a preferences service

a preferences service

services.factory('PrefsService', ['$http', 'MacAppUtils', '$location',function($http, MacAppUtils, $location)
//-----------------------------------------------------------------------------------
{
    var REDIS_PREFS_KEY_ANNOTATIONS = "__annotations";
    var REDIS_PREFS_KEY_HISTORY = "__history";
    return {
        _isEnabled:true,
        _data: {},
        isEnabled: function() {
            return this._isEnabled;
        },
        init: function(scope, prefs) {
            this._isEnabled = scope.gState.isRedisEnabled;
            if (this.isEnabled()) {
                this._data = eval(prefs);
                if (!this._data) {
                    gLog.info("There was a problem getting Redis Preferences");
                    gLog.info("Disabling redis prefs. Using Mock Prefs");
                    this._isEnabled = false;
                    scope.prefs = {
                        get:function() {},
                        set:function() {},
                        del:function() {}
                    };
                } else {
                    gLog.debug("Redis Enabled. Loaded Prefs data", prefs);
                    scope.prefs = this;
                }
            } else {
                gLog.debug("Redis Disabled. Using Mock Prefs");
                scope.prefs = {
                    get:function() {},
                    set:function() {},
                    del:function() {}
                };
            }
        },
        get: function(key) {
            return this._data[key];
        },
        // these are real app prefs values
        set: function(key, value) {
            this._data[key] = value;
            this._updateHistory(key, value);
            this._saveRemote();
        },
        setAnnotation: function(key, value) {
            var annotations = this._data[REDIS_PREFS_KEY_ANNOTATIONS];
            if (!annotations) {
                this._data[REDIS_PREFS_KEY_ANNOTATIONS] = {};
            }
            this._data[REDIS_PREFS_KEY_ANNOTATIONS][key] = value;
            this._saveRemote();
        },
        del: function(key) {
            delete this._data[key];
            this._saveRemote();
        },
        _updateHistory: function(key, value) {
            var updates = this._data[REDIS_PREFS_KEY_HISTORY]; // keep track of up to 20 prefs updates
            if (!updates) {
                this._data[REDIS_PREFS_KEY_HISTORY] = [];
            }
            this._data[REDIS_PREFS_KEY_HISTORY].unshift({"key":key,"value":value,'_time': new Date().toString()});
            if (this._data[REDIS_PREFS_KEY_HISTORY].length > 20) {
                this._data[REDIS_PREFS_KEY_HISTORY].pop();
            }
        },
        _saveRemote: function() {
            var url = '/proxy/prefs?callingContext='+$location.path();
            if (gLog) {
                gLog.debug('save preferences url', url);
            }
            $http.post(url,this._data);
        },
        // TD: change this to return a promise instead of accepting a callback
        _loadRemote: function(afterLoadCallbackFn) {
            var thisObj = this;
            var url = '/proxy/prefs?r='+MacAppUtils.getNextRandom()+'&callingContext='+$location.path();
            if (gLog) {
                gLog.debug('load preferences url', url);
            }
            $http.get(url).success(function(data) {
                if (data != "") {
                    thisObj._data = (eval(data));
                }
                if (gLog) {
                    gLog.debug('load preferences data', thisObj);
                }

                if (afterLoadCallbackFn) {
                    afterLoadCallbackFn();
                }
            });
        }
    };

}]);