Moved PaletteService from pskl.service to piskel.service.palette

This commit is contained in:
jdescottes 2014-09-07 12:27:06 +02:00
parent 92d7109ef7
commit 8643f4402a
3 changed files with 63 additions and 2 deletions

View File

@ -38,7 +38,7 @@
this.piskelController = new pskl.controller.piskel.PublicPiskelController(this.corePiskelController);
this.piskelController.init();
this.paletteService = new pskl.service.PaletteService();
this.paletteService = new pskl.service.palette.PaletteService();
this.paletteService.init();
this.paletteController = new pskl.controller.PaletteController();

View File

@ -0,0 +1,61 @@
(function () {
var ns = $.namespace('pskl.service.palette');
ns.PaletteService = function () {
this.palettes = [];
this.localStorageService = window.localStorage;
};
ns.PaletteService.prototype.init = function () {};
ns.PaletteService.prototype.getPalettes = function () {
var palettesString = this.localStorageService.getItem('piskel.palettes');
return JSON.parse(palettesString) || [];
};
ns.PaletteService.prototype.getPaletteById = function (paletteId) {
var palettes = this.getPalettes();
return this.findPaletteInArray_(paletteId, palettes);
};
ns.PaletteService.prototype.savePalette = function (palette) {
var palettes = this.getPalettes();
var existingPalette = this.findPaletteInArray_(palette.id, palettes);
if (existingPalette) {
var currentIndex = palettes.indexOf(existingPalette);
palettes.splice(currentIndex, 1, palette);
} else {
palettes.push(palette);
}
this.savePalettes_(palettes);
$.publish(Events.SHOW_NOTIFICATION, [{"content": "Palette " + palette.name + " successfully saved !"}]);
window.setTimeout($.publish.bind($, Events.HIDE_NOTIFICATION), 2000);
};
ns.PaletteService.prototype.deletePaletteById = function (id) {
var palettes = this.getPalettes();
var filteredPalettes = palettes.filter(function (palette) {
return palette.id !== id;
});
this.savePalettes_(filteredPalettes);
};
ns.PaletteService.prototype.savePalettes_ = function (palettes) {
this.localStorageService.setItem('piskel.palettes', JSON.stringify(palettes));
$.publish(Events.PALETTE_LIST_UPDATED);
};
ns.PaletteService.prototype.findPaletteInArray_ = function (paletteId, palettes) {
var match = null;
palettes.forEach(function (palette) {
if (palette.id === paletteId) {
match = palette;
}
});
return match;
};
})();

View File

@ -119,7 +119,7 @@
"js/service/BackupService.js",
"js/service/BeforeUnloadService.js",
"js/service/HistoryService.js",
"js/service/PaletteService.js",
"js/service/palette/PaletteService.js",
"js/service/SavedStatusService.js",
"js/service/keyboard/ShortcutService.js",
"js/service/keyboard/KeycodeTranslator.js",