npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-angular-preferences

v1.0.15

Published

Electron extendable preference window build in angular 7

Downloads

10

Readme

Electron Angular Preferences

Inspiration & Base code

Thank you to -> Electron Preferences for the inspiration & base structure

Introduction

Provides Electron developers with with a simple, consistent interface for managing user preferences. It includes two primary components:

  • A GUI interface within which the users of your application can manage their preferences.
  • An API for interacting with the service.

Using the API, developers can:

  • Define default preferences
  • Create custom validations
  • Read / write values on demand
  • Define the layout of the preferences window.

To see the library in action, clone this repository and see the demo application that is included within the example folder:

$ git clone https://github.com/ddanninger/electron-angular-preferences.git
$ cd electron-angular-preferences
$ npm i --no-optional
$ npm run build
$ npm run example

Getting Started

Initializing the Preferences Service

Install with

npm install electron-angular-preferences

Within your application's main process, create a new instance of the ElectronPreferences class, as shown below. preferences.js

const electron = require('electron');
const app = electron.app;
const path = require('path');
const os = require('os');
const ElectronPreferences = require('electron-angular-preferences');

const preferences = new ElectronPreferences({
    /**
     * Where should preferences be saved?
     */
    dataStore: path.resolve(app.getPath('userData'), 'preferences.json'),
    /**
     * Default values.
     */
    defaults: {
        'notes': {
            'folder': path.resolve(os.homedir(), 'Notes')
        },
        'markdown': {
            'auto_format_links': true,
            'show_gutter': false
        },
        'preview': {
            'show': true
        },
        'drawer': {
            'show': true
        }
    },
    /**
     * If the `onLoad` method is specified, this function will be called immediately after
     * preferences are loaded for the first time. The return value of this method will be stored as the
     * preferences object.
     */
    onLoad: (preferences) => {
        // ...
        return preferences;
    },
    /**
     * Extend the BrowserWindow of the Preference Window
     */
     window: {
         'icon': 'path-to-icon.png'
     },
     /**
     * Custom validators (value: string) => {}
     * value is the filled value
     */
     validators: {
     validate_me: val => {
            if (val === 'test') {
                return true;
            }
            return false;
        }
     },
    /**
    * Angular Validation Mechanism
    */
    validationOn: 'submit', // blur | submit | null or do not set empty for default behaviour
    /**
    * Actions to call for buttons ( e.g usecase you want to implement a "Test Connection" button)
    * (form: []) => {} will return the group of the button , with the values of all form fields in this group
    */
    actions: {
        btn_action: form => {
            return 'This message will be shown inline';
        }
    },
    /**
     * The preferences window is divided into sections. Each section has a label, an icon, and one or
     * more fields associated with it. Each section should also be given a unique ID.
     */
    sections: [
    {
      name: 'about',
      label: 'About You',
      icon: 'globe',
      form: {
        groups: [
          {
            label: 'About You',
            fields: [
              {
                label: 'Validate me',
                name: 'validate_me',
                type: 'text',
                help: 'field must be "test"',
                validator: 'validate_me',
                errorMessage: 'Field value is not "test"!'
              },
              {
                label: 'First Name',
                name: 'first_name',
                type: 'text',
                help: 'What is your first name?'
              },
              {
                label: 'Last Name',
                name: 'last_name',
                type: 'text',
                help: 'What is your last name?'
              },
              {
                label: 'Gender',
                name: 'gender',
                type: 'dropdown',
                options: [
                  { label: 'Male', value: 'male' },
                  { label: 'Female', value: 'female' },
                  { label: 'Unspecified', value: 'unspecified' }
                ],
                help: 'What is your gender?'
              },
              {
                label: 'Age',
                name: 'age',
                type: 'text',
                inputType: 'number'
              },
              {
                label: 'Which of the following foods do you like?',
                name: 'foods',
                type: 'checkbox',
                options: [
                  { label: 'Ice Cream', value: 'ice_cream' },
                  { label: 'Carrots', value: 'carrots' },
                  { label: 'Cake', value: 'cake' },
                  { label: 'Spinach', value: 'spinach' }
                ],
                help: 'Select one or more foods that you like.'
              }
            ]
          }
        ]
      }
    },
    {
      name: 'notes',
      label: 'Notes',
      icon: 'certificate',
      form: {
        groups: [
          {
            label: 'Stuff',
            fields: [
              {
                label: 'Read notes from folder',
                name: 'folder',
                type: 'directory',
                help: 'The location where your notes will be stored.'
              },
              {
                heading: 'Important Message',
                content:
                  '<p>The quick brown fox jumps over the long white fence. The quick brown fox jumps over the long white fence. The quick brown fox jumps over the long white fence. The quick brown fox jumps over the long white fence.</p>',
                type: 'message'
              }
            ]
          }
        ]
      }
    },
    {
      name: 'space',
      label: 'Other Settings',
      icon: 'folder',
      form: {
        groups: [
          {
            label: 'Other Settings',
            fields: [
              {
                label: 'Enable',
                name: 'enable_debug',
                type: 'boolean',
                help: 'Do you want to enable the debug mode?'
              },
              {
                label: 'Test button',
                name: 'button',
                type: 'button',
                action: 'btn_action',
                help: 'Click me to do something'
              },
              {
                label: 'Phone Number',
                name: 'phone_number',
                type: 'text',
                help: 'What is your phone number?'
              },
              {
                label: 'Foo or Bar?',
                name: 'foobar',
                type: 'radio',
                options: [
                  { label: 'Foo', value: 'foo' },
                  { label: 'Bar', value: 'bar' },
                  { label: 'FooBar', value: 'foobar' }
                ],
                help: 'Foo? Bar?'
              }
            ]
          }
        ]
      }
    }
  ]
});

Interacting with the Preferences Service from the Main Process

// Show the preferences window on demand.
preferences.show();

// Get a value from the preferences data store
const myPref = preferences.value('some.nested.key');

// Save a value within the preferences data store
preferences.value('some.nested.key', 'my-value');

// Subscribing to preference changes.
preferences.on('save', (preferences) => {
    console.log(`Preferences were saved.`, JSON.stringify(preferences, null, 4));
});

Interacting with the Preferences Service from the Renderer Process

const { ipcRenderer, remote } = require('electron');

// Fetch the preferences object
const preferences = ipcRenderer.sendSync('getPreferences');

// Display the preferences window
ipcRenderer.send('showPreferences');

// Listen to the `preferencesUpdated` event to be notified when preferences are changed.
ipcRenderer.on('preferencesUpdated', (e, preferences) => {
    console.log('Preferences were updated', preferences);
});

// Instruct the preferences service to update the preferences object from within the renderer.
ipcRenderer.sendSync('setPreferences', { ... });

Actions

Defining actions via the action attribute An action can return either a string or an object

{ message: string, color: string }

where color stands for the text color.

Observable Actions

Observable action names should end with $

btn_actionObject$: form => {
  const domain = form.domain;
  return interval(1000).pipe(
    take(1),
    switchMap(() => from(ping.promise.probe(domain))),
    map(({ alive, avg }) => {
      if (!alive) {
        return { message: `I couldnt ping ${domain}`, color: 'red' };
      }
      return { message: `I pinged ${domain} and avg=${avg}`, color: 'green' };
    })
  );
}

Usecases

e.g you want to implement a test connection button to validate if the domain is reachable

Field Types

The library includes built-in support for the following field types:

  • Text ( text )
  • Dropdown ( dropdown )
  • Message ( message )
  • Folder selection ( directory )
  • Checkbox ( checkbox )
  • Radio ( radio )
  • Switch ( boolean )
  • Spinner Button ( spinner-button )
  • Button ( button )
  • Shortcut ( shortcut )

Icons

Font Awesome 5 Free, Solid icons can be used

Known issues

  • Validators and Action buttons still need some testing and improvements