Browse Source

Added better and functional mocky

sisyphus
JezerM 4 years ago
parent
commit
a3c7cd0a87
  1. 179
      themes/_vendor/js/mocky.js

179
themes/_vendor/js/mocky.js vendored

@ -1,4 +1,4 @@
class Language { class LightDMLanguage {
constructor( {code, name, territory} ) { constructor( {code, name, territory} ) {
this.code = code; this.code = code;
this.name = name; this.name = name;
@ -6,7 +6,7 @@ class Language {
} }
} }
class Layout { class LightDMLayout {
constructor( {name, description, short_description} ) { constructor( {name, description, short_description} ) {
this.name = name; this.name = name;
this.description = description; this.description = description;
@ -14,7 +14,7 @@ class Layout {
} }
} }
class Session { class LightDMSession {
constructor( {key, name, comment} ) { constructor( {key, name, comment} ) {
this.key = key; this.key = key;
this.name = name; this.name = name;
@ -22,7 +22,7 @@ class Session {
} }
} }
class User { class LightDMUser {
constructor( {display_name, username, language, layout, image, home_directory, session, logged_in} ) { constructor( {display_name, username, language, layout, image, home_directory, session, logged_in} ) {
this.display_name = display_name; this.display_name = display_name;
this.username = username; this.username = username;
@ -35,7 +35,7 @@ class User {
} }
} }
class Battery { class LightDMBattery {
constructor( {name, level, status}) { constructor( {name, level, status}) {
this.name = name; this.name = name;
this.level = level; this.level = level;
@ -43,75 +43,121 @@ class Battery {
} }
} }
let allSignals = [];
class Signal {
constructor(name) {
this._name = name;
this._callbacks = [];
allSignals.push(this);
}
/**
* Connects a callback to the signal.
* @param {Function} callback The callback to attach.
*/
connect(callback) {
if (typeof callback !== 'function') return;
this._callbacks.push(callback);
}
/**
* Disconnects a callback to the signal.
* @param {Function} callback The callback to disattach.
*/
disconnect(callback) {
var ind = this._callbacks.findIndex( (cb) => {return cb === callback});
if (ind == -1) return;
this._callbacks.splice(ind, 1);
}
_run() {
this._callbacks.forEach( (cb) => {
if (typeof cb !== 'function') return;
cb()
})
}
}
/**
* Emits a signal.
* @param {String} name The signal's name.
*/
function emitSignal(name) {
var signal = allSignals.find( (s) => {
return s._name === name;
})
signal._run()
}
_mockData = { _mockData = {
languages: [ languages: [
Language({ new LightDMLanguage({
name: 'English', name: 'English',
code: 'en_US.utf8', code: 'en_US.utf8',
territory: 'USA' territory: 'USA'
}), }),
Language({ new LightDMLanguage({
name: 'Catalan', name: 'Catalan',
code: 'ca_ES.utf8', code: 'ca_ES.utf8',
territory: 'Spain' territory: 'Spain'
}), }),
Language({ new LightDMLanguage({
name: 'French', name: 'French',
code: 'fr_FR.utf8', code: 'fr_FR.utf8',
territory: 'France' territory: 'France'
}) })
], ],
layouts: [ layouts: [
Layout({ new LightDMLayout({
name: 'us', name: 'us',
short_description: 'en', short_description: 'en',
description: 'English (US)' description: 'English (US)'
}), }),
Layout({ new LightDMLayout({
name: 'at', name: 'at',
short_description: 'de', short_description: 'de',
description: 'German (Austria)' description: 'German (Austria)'
}), }),
Layout({ new LightDMLayout({
name: 'us rus', name: 'us rus',
short_description: 'ru', short_description: 'ru',
description: 'Russian (US, phonetic)' description: 'Russian (US, phonetic)'
}) })
], ],
sessions: [ sessions: [
Session({ new LightDMSession({
key: 'gnome', key: 'gnome',
name: 'GNOME', name: 'GNOME',
comment: 'This session logs you into GNOME' comment: 'This session logs you into GNOME'
}), }),
Session({ new LightDMSession({
key: 'cinnamon', key: 'cinnamon',
name: 'Cinnamon', name: 'Cinnamon',
comment: 'This session logs you into Cinnamon' comment: 'This session logs you into Cinnamon'
}), }),
Session({ new LightDMSession({
key: 'plasma', key: 'plasma',
name: 'Plasma', name: 'Plasma',
comment: 'Plasma by KDE' comment: 'Plasma by KDE'
}), }),
Session({ new LightDMSession({
key: 'awesome', key: 'awesome',
name: 'Awesome wm', name: 'Awesome wm',
comment: 'An Awesome WM' comment: 'An Awesome WM'
}), }),
Session({ new LightDMSession({
key: 'mate', key: 'mate',
name: 'MATE', name: 'MATE',
comment: 'This session logs you into MATE' comment: 'This session logs you into MATE'
}), }),
Session({ new LightDMSession({
key: 'openbox', key: 'openbox',
name: 'Openbox', name: 'Openbox',
comment: 'This session logs you into Openbox' comment: 'This session logs you into Openbox'
}) })
], ],
users: [ users: [
User({ new LightDMUser({
display_name: 'Clark Kent', display_name: 'Clark Kent',
username: 'superman', username: 'superman',
language: null, language: null,
@ -121,7 +167,7 @@ _mockData = {
session: 'gnome', session: 'gnome',
logged_in: false, logged_in: false,
}), }),
User({ new LightDMUser({
display_name: 'Bruce Wayne', display_name: 'Bruce Wayne',
username: 'batman', username: 'batman',
language: null, language: null,
@ -131,7 +177,7 @@ _mockData = {
session: 'cinnamon', session: 'cinnamon',
logged_in: false, logged_in: false,
}), }),
User({ new LightDMUser({
display_name: 'Peter Parker', display_name: 'Peter Parker',
username: 'spiderman', username: 'spiderman',
language: null, language: null,
@ -142,11 +188,11 @@ _mockData = {
logged_in: false, logged_in: false,
}) })
], ],
battery: Battery({ battery: new LightDMBattery({
name: "Battery 0", name: "Battery 0",
level: 85, level: 85,
state: "Discharging" state: "Discharging"
}) }),
} }
@ -161,6 +207,8 @@ class Greeter {
return window.lightdm; return window.lightdm;
} }
mock = true;
_authentication_user = null; _authentication_user = null;
/** /**
* The username of the user being authenticated or {@link null} * The username of the user being authenticated or {@link null}
@ -206,7 +254,7 @@ class Greeter {
_batteryData = _mockData.battery; _batteryData = _mockData.battery;
/** /**
* Gets the battery data. * Gets the battery data.
* @type {Battery} * @type {LightDMBattery}
* @readonly * @readonly
*/ */
get batteryData() { get batteryData() {
@ -226,7 +274,10 @@ class Greeter {
* @param {Number} quantity The quantity to set * @param {Number} quantity The quantity to set
*/ */
set brightness( quantity ) { set brightness( quantity ) {
if (quantity > 100) quantity = 100;
if (quantity < 0) quantity = 0;
this._brightness = quantity; this._brightness = quantity;
emitSignal("brightness_update");
} }
_can_access_battery = true; _can_access_battery = true;
@ -352,7 +403,7 @@ class Greeter {
_language = null; _language = null;
/** /**
* The current language or {@link null} if no language. * The current language or {@link null} if no language.
* @type {Language|null} * @type {LightDMLanguage|null}
* @readonly * @readonly
*/ */
get language() { get language() {
@ -362,7 +413,7 @@ class Greeter {
_languages = _mockData.languages; _languages = _mockData.languages;
/** /**
* A list of languages to present to the user. * A list of languages to present to the user.
* @type {Language[]} * @type {LightDMLanguage[]}
* @readonly * @readonly
*/ */
get languages() { get languages() {
@ -372,7 +423,7 @@ class Greeter {
_layout = _mockData.layouts[0]; _layout = _mockData.layouts[0];
/** /**
* The currently active layout for the selected user. * The currently active layout for the selected user.
* @type {Layout} * @type {LightDMLayout}
*/ */
get layout() { get layout() {
return this._layout; return this._layout;
@ -381,7 +432,7 @@ class Greeter {
_layouts = _mockData.layouts; _layouts = _mockData.layouts;
/** /**
* A list of keyboard layouts to present to the user. * A list of keyboard layouts to present to the user.
* @type {Layout[]} * @type {LightDMLayout[]}
* @readonly * @readonly
*/ */
get layouts() { get layouts() {
@ -421,7 +472,7 @@ class Greeter {
_sessions = _mockData.sessions; _sessions = _mockData.sessions;
/** /**
* List of available sessions. * List of available sessions.
* @type {Session[]} * @type {LightDMSession[]}
* @readonly * @readonly
*/ */
get sessions() { get sessions() {
@ -455,11 +506,11 @@ class Greeter {
_users = _mockData.users; _users = _mockData.users;
/** /**
* List of available users. * List of available users.
* @type {User[]} * @type {LightDMUser[]}
* @readonly * @readonly
*/ */
get users() { get users() {
return this.authentication_user; return this._users;
} }
/** /**
@ -468,18 +519,23 @@ class Greeter {
* @param {String|null} username A username or {@link null} to prompt for a username. * @param {String|null} username A username or {@link null} to prompt for a username.
*/ */
authenticate( username ) { authenticate( username ) {
this._in_authentication = true;
this._authentication_user = username;
} }
/** /**
* Starts the authentication procedure for the guest user. * Starts the authentication procedure for the guest user.
*/ */
authenticate_as_guest() { authenticate_as_guest() {
this._in_authentication = true;
this._authentication_user = "guest";
} }
/** /**
* Updates the battery data * Updates the battery data
*/ */
batteryUpdate() { batteryUpdate() {
console.log("Battery updated")
} }
/** /**
@ -487,6 +543,7 @@ class Greeter {
* @param {Number} quantity The quantity to set * @param {Number} quantity The quantity to set
*/ */
brightnessSet( quantity ) { brightnessSet( quantity ) {
this.brightness = quantity;
} }
/** /**
@ -494,6 +551,7 @@ class Greeter {
* @param {Number} quantity The quantity to increase * @param {Number} quantity The quantity to increase
*/ */
brightnessIncrease( quantity ) { brightnessIncrease( quantity ) {
this.brightness += quantity;
} }
/** /**
@ -501,12 +559,15 @@ class Greeter {
* @param {Number} quantity The quantity to decrease * @param {Number} quantity The quantity to decrease
*/ */
brightnessDecrease( quantity ) { brightnessDecrease( quantity ) {
this.brightness -= quantity;
} }
/** /**
* Cancel user authentication that is currently in progress. * Cancel user authentication that is currently in progress.
*/ */
cancel_authentication() { cancel_authentication() {
this._in_authentication = false;
this._authentication_user = "";
} }
/** /**
@ -515,26 +576,31 @@ class Greeter {
cancel_autologin() { cancel_autologin() {
} }
/**
* Get the value of a hint.
* @param {String} name The name of the hint to get.
* @returns {String|Boolean|Number|null}
*/
get_hint( name ) {
}
/** /**
* Triggers the system to hibernate. * Triggers the system to hibernate.
* @returns {Boolean} {@link true} if hibernation initiated, otherwise {@link false} * @returns {Boolean} {@link true} if hibernation initiated, otherwise {@link false}
*/ */
hibernate() { hibernate() {
alert("Hibernating system");
location.reload();
} }
_default_password = "justice";
/** /**
* Provide a response to a prompt. * Provide a response to a prompt.
* @param {*} response * @param {*} response
*/ */
respond( response ) { respond( response ) {
console.log("Response:", response);
if (response === this._default_password) {
this._is_authenticated = true;
emitSignal("authentication_complete")
} else {
this._is_authenticated = false;
setTimeout(() => {
emitSignal("authentication_complete")
}, 2000)
}
} }
/** /**
@ -542,6 +608,8 @@ class Greeter {
* @returns {Boolean} {@link true} if restart initiated, otherwise {@link false} * @returns {Boolean} {@link true} if restart initiated, otherwise {@link false}
*/ */
restart() { restart() {
alert("Restarting system");
location.reload();
} }
/** /**
@ -558,6 +626,8 @@ class Greeter {
* @returns {Boolean} {@link true} if shutdown initiated, otherwise {@link false} * @returns {Boolean} {@link true} if shutdown initiated, otherwise {@link false}
*/ */
shutdown() { shutdown() {
alert("Shutting down system");
location.reload();
} }
/** /**
@ -566,13 +636,33 @@ class Greeter {
* @returns {Boolean} {@link true} if successful, otherwise {@link false} * @returns {Boolean} {@link true} if successful, otherwise {@link false}
*/ */
start_session( session ) { start_session( session ) {
if (!this._is_authenticated) return;
alert(`Session started with: "${session}"`);
location.reload();
} }
/** /**
* Triggers the system to suspend/sleep. * Triggers the system to suspend/sleep.
* @returns {Boolean} {@link true} if suspend/sleep initiated, otherwise {@link false} * @returns {Boolean} {@link true} if suspend/sleep initiated, otherwise {@link false}
*/ */
suspend() {} suspend() {
alert("Suspending system");
location.reload();
}
authentication_complete = new Signal("authentication_complete");
autologin_timer_expired = new Signal("autologin_timer_expired");
brightness_update = new Signal("brightness_update");
idle = new Signal("idle");
reset = new Signal("reset");
show_message = new Signal("show_message");
show_prompt = new Signal("show_prompt");
} }
@ -661,6 +751,10 @@ class GreeterConfig {
} }
let localized_invalid_date = null,
time_language = null,
time_format = null
class ThemeUtils { class ThemeUtils {
constructor() { constructor() {
if ("theme_utils" in window) { if ("theme_utils" in window) {
@ -745,7 +839,12 @@ class ThemeUtils {
} }
} }
new ThemeUtils(); new ThemeUtils();
new GreeterConfig(); new GreeterConfig();
new Greeter(); new Greeter();
const GreeterReady = new Event("GreeterReady")
setTimeout(() => {
window.dispatchEvent(GreeterReady)
}, 1000)

Loading…
Cancel
Save