From 8643f4402a621581b1b3a1be3edb95d1722acfda Mon Sep 17 00:00:00 2001 From: jdescottes Date: Sun, 7 Sep 2014 12:27:06 +0200 Subject: [PATCH] Moved PaletteService from pskl.service to piskel.service.palette --- src/js/app.js | 2 +- src/js/service/palette/PaletteService.js | 61 ++++++++++++++++++++++++ src/piskel-script-list.js | 2 +- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/js/service/palette/PaletteService.js diff --git a/src/js/app.js b/src/js/app.js index 4371d6c5..efa720ae 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -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(); diff --git a/src/js/service/palette/PaletteService.js b/src/js/service/palette/PaletteService.js new file mode 100644 index 00000000..2f410ff2 --- /dev/null +++ b/src/js/service/palette/PaletteService.js @@ -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; + }; +})(); \ No newline at end of file diff --git a/src/piskel-script-list.js b/src/piskel-script-list.js index a699101a..6e5f35e2 100644 --- a/src/piskel-script-list.js +++ b/src/piskel-script-list.js @@ -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",