|
| 1 | +class Backgrounds { |
| 2 | + constructor() { |
| 3 | + this._localStorage = window.localStorage; |
| 4 | + this._defaultBackgroundArr = [ |
| 5 | + "img/wallpapers/milky-way-robiei.jpg", |
| 6 | + "img/wallpapers/star-cloud-blue-red.jpg", |
| 7 | + "img/wallpapers/nathan-anderson-268995.jpg", |
| 8 | + "img/wallpapers/hugo-kemmel-289805.jpg", |
| 9 | + "img/wallpapers/jamison-mcandie-112376.jpg", |
| 10 | + ]; |
| 11 | + this._backgroundImages = null; |
| 12 | + this._backgroundImagesDir = null; |
| 13 | + this._backgroundPath = ""; |
| 14 | + } |
| 15 | + |
| 16 | + async _createBackgroundArray() { |
| 17 | + let images = await this._getImages(); |
| 18 | + this._backgroundImages = this._defaultBackgroundArr.concat(images); |
| 19 | + return new Promise((resolve) => resolve()); |
| 20 | + } |
| 21 | + |
| 22 | + _updateOnStartup() { |
| 23 | + this._backgroundPath = |
| 24 | + this._localStorage.getItem("defaultBackgroundImage") || |
| 25 | + this._backgroundImages[0]; |
| 26 | + this._updateBackgroundImages(); |
| 27 | + this._addBackgroundButtonsHandler(); |
| 28 | + } |
| 29 | + |
| 30 | + _updateBackgroundImages() { |
| 31 | + this._backgroundImages.forEach(function (file) { |
| 32 | + let background = {}; |
| 33 | + background.image = file; |
| 34 | + background.thumb = 'img/thumbs/' + file.split(/(\\|\/)/g).pop(); |
| 35 | + $('.bgs').append(` |
| 36 | + <a href="#" data-img="${background.image}" class="background"> |
| 37 | + <img src="${background.thumb}" /> |
| 38 | + </a> |
| 39 | + `); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + _addBackgroundButtonsHandler() { |
| 44 | + let backgroundButtons = $(".bg-switch .background"); |
| 45 | + backgroundButtons.click(function (e) { |
| 46 | + e.preventDefault(); |
| 47 | + backgroundButtons.removeClass("active"); |
| 48 | + $(".bgs .background .default").first().removeClass('active'); |
| 49 | + $(this).addClass("active"); |
| 50 | + switchBackground($(this).data("img")); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | + _getImages(path) { |
| 55 | + this._backgroundImagesDir = |
| 56 | + greeter_config.branding.background_images_dir || "/usr/share/backgrounds"; |
| 57 | + return new Promise((resolve) => { |
| 58 | + theme_utils.dirlist( |
| 59 | + path ? path : this._backgroundImagesDir, |
| 60 | + true, |
| 61 | + (result) => { |
| 62 | + resolve(result); |
| 63 | + } |
| 64 | + ); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + async _init() { |
| 69 | + await this._createBackgroundArray(); |
| 70 | + this._updateOnStartup(); |
| 71 | + |
| 72 | + return new Promise((resolve) => resolve()); |
| 73 | + } |
| 74 | +} |
0 commit comments