Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues when styleVariables is undefined #319

Merged
merged 1 commit into from
Dec 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions lib/app/js/services/Variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,42 @@
};

this.refreshValues = function() {
for (var i = 0; i < serverData.length; i++) {
var oldIndex;
if (this.variables[i] && this.variables[i].name !== serverData[i].name) {
if (!this.getServerVarByName(this.variables[i].name)) {
// This variable does not exists anymore on the server. Remove it
this.variables.splice(i, 1);
} else if (this.getLocalVarByName(serverData[i].name) && !this.getLocalVarByName(serverData[i].name).dirty) {
// The variable already exists but in another position
// It is not changed so we can just remove it
oldIndex = this.getLocalIndexByName(serverData[i].name);
this.variables.splice(oldIndex, 1);
this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value});
} else if (this.getLocalVarByName(serverData[i].name)) {
// The variable already exists but in another position
// It is changed so we need to keep the old values
oldIndex = this.getLocalIndexByName(serverData[i].name);
var oldValue = this.variables[oldIndex].value;
this.variables.splice(oldIndex, 1);
this.variables.splice(i, 0, {name: serverData[i].name, value: oldValue});
} else {
// The variable does not exists anywhere else. Just add it
this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value});
}
} else if (this.variables[i] && this.variables[i].name === serverData[i].name) {
// Variable exists already locally
// Update value if variable does not have any local changes
if (!this.variables[i].dirty) {
this.variables[i].value = serverData[i].value;
if (serverData.length === 0) {
this.variables = [];
} else {
for (var i = 0; i < serverData.length; i++) {
var oldIndex;
if (this.variables[i] && this.variables[i].name !== serverData[i].name) {
if (!this.getServerVarByName(this.variables[i].name)) {
// This variable does not exists anymore on the server. Remove it
this.variables.splice(i, 1);
} else if (this.getLocalVarByName(serverData[i].name) && !this.getLocalVarByName(serverData[i].name).dirty) {
// The variable already exists but in another position
// It is not changed so we can just remove it
oldIndex = this.getLocalIndexByName(serverData[i].name);
this.variables.splice(oldIndex, 1);
this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value});
} else if (this.getLocalVarByName(serverData[i].name)) {
// The variable already exists but in another position
// It is changed so we need to keep the old values
oldIndex = this.getLocalIndexByName(serverData[i].name);
var oldValue = this.variables[oldIndex].value;
this.variables.splice(oldIndex, 1);
this.variables.splice(i, 0, {name: serverData[i].name, value: oldValue});
} else {
// The variable does not exists anywhere else. Just add it
this.variables.splice(i, 0, {name: serverData[i].name, value: serverData[i].value});
}
} else if (this.variables[i] && this.variables[i].name === serverData[i].name) {
// Variable exists already locally
// Update value if variable does not have any local changes
if (!this.variables[i].dirty) {
this.variables[i].value = serverData[i].value;
}
} else if (!this.variables[i]) {
// Add new local variable
this.variables.push({name: serverData[i].name, value: serverData[i].value});
}
} else if (!this.variables[i]) {
// Add new local variable
this.variables.push({name: serverData[i].name, value: serverData[i].value});
}
}
};
Expand Down Expand Up @@ -150,11 +154,13 @@
$rootScope.$watch(function() {
return Styleguide.config.data;
}, function(newValue) {
if (newValue) {
if (newValue && newValue.settings) {
serverData = newValue.settings;
_this.refreshValues();
_this.refreshDirtyStates();
} else {
serverData = [];
}
_this.refreshValues();
_this.refreshDirtyStates();
});
};

Expand Down
16 changes: 16 additions & 0 deletions test/angular/services/Variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ describe('Service: Variables', function() {
]);
});

it('should set empty array as server data if variables do not exists', function() {
rootScope.$digest();
styleguideMock.config.data = {};
rootScope.$digest();
expect(Variables.variables).to.eql([]);
});

it('should set empty array as server data no vairables are found', function() {
rootScope.$digest();
styleguideMock.config.data = {
settings: []
};
rootScope.$digest();
expect(Variables.variables).to.eql([]);
});

it('should not mark server side changes as dirty', function() {
rootScope.$digest();
styleguideMock.config.data = {
Expand Down