From c757070a488c97f5307f1f318b1f772db4edd032 Mon Sep 17 00:00:00 2001 From: Hannu Pelkonen Date: Tue, 4 Nov 2014 13:52:53 +0200 Subject: [PATCH] Implement possibility to reset local variable changes --- lib/app/js/directives/design.js | 4 ++++ lib/app/js/services/Variables.js | 10 ++++++++++ lib/app/views/partials/design.html | 1 + test/angular/services/variables.js | 9 +++++++++ 4 files changed, 24 insertions(+) diff --git a/lib/app/js/directives/design.js b/lib/app/js/directives/design.js index 623f3828..627f58aa 100644 --- a/lib/app/js/directives/design.js +++ b/lib/app/js/directives/design.js @@ -17,6 +17,10 @@ angular.module('sgApp') Variables.saveVariables(); } + scope.resetLocal = function() { + Variables.resetLocal(); + } + scope.isColor = function(value) { if (/^(#|rgb)/.test(value)) return true; return false; diff --git a/lib/app/js/services/Variables.js b/lib/app/js/services/Variables.js index c6d27788..c11ee50d 100644 --- a/lib/app/js/services/Variables.js +++ b/lib/app/js/services/Variables.js @@ -38,6 +38,16 @@ return this; }; + this.resetLocal = function() { + var _this = this; + // We need to remove each key individually to keep the object reference the same + angular.forEach(this.variables, function(value, key) { + delete _this.variables[key]; + }); + // Call setValues so that the server data is used + this.setValues({}); + }; + this.setSocket = function(newSocket) { var _this = this; this.socket = newSocket; diff --git a/lib/app/views/partials/design.html b/lib/app/views/partials/design.html index c2439eda..61b17208 100644 --- a/lib/app/views/partials/design.html +++ b/lib/app/views/partials/design.html @@ -14,6 +14,7 @@ diff --git a/test/angular/services/variables.js b/test/angular/services/variables.js index d2849186..e4e4896d 100644 --- a/test/angular/services/variables.js +++ b/test/angular/services/variables.js @@ -52,6 +52,15 @@ describe('Service: Variables', function() { expect(Variables.variables).to.eql({setting2: 'value2'}); }); + it('should properly reset local changes', function() { + rootScope.$digest(); + Variables.setValues({setting1: 'changed', setting2: 'changed'}); + rootScope.$digest(); + Variables.resetLocal(); + rootScope.$digest(); + expect(Variables.variables).to.eql({setting1: 'value1', setting2: 'value2'}); + }); + it('should allow new server side keys with new values', function() { rootScope.$digest(); styleguideMock.config.data = {