addon-sdk/source/lib/sdk/ui/button/toggle.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 'stability': 'experimental',
michael@0 8 'engines': {
michael@0 9 'Firefox': '> 28'
michael@0 10 }
michael@0 11 };
michael@0 12
michael@0 13 const { Class } = require('../../core/heritage');
michael@0 14 const { merge } = require('../../util/object');
michael@0 15 const { Disposable } = require('../../core/disposable');
michael@0 16 const { on, off, emit, setListeners } = require('../../event/core');
michael@0 17 const { EventTarget } = require('../../event/target');
michael@0 18 const { getNodeView } = require('../../view/core');
michael@0 19
michael@0 20 const view = require('./view');
michael@0 21 const { toggleButtonContract, toggleStateContract } = require('./contract');
michael@0 22 const { properties, render, state, register, unregister,
michael@0 23 setStateFor, getStateFor, getDerivedStateFor } = require('../state');
michael@0 24 const { events: stateEvents } = require('../state/events');
michael@0 25 const { events: viewEvents } = require('./view/events');
michael@0 26 const events = require('../../event/utils');
michael@0 27
michael@0 28 const { getActiveTab } = require('../../tabs/utils');
michael@0 29
michael@0 30 const { id: addonID } = require('../../self');
michael@0 31 const { identify } = require('../id');
michael@0 32
michael@0 33 const buttons = new Map();
michael@0 34
michael@0 35 const toWidgetId = id =>
michael@0 36 ('toggle-button--' + addonID.toLowerCase()+ '-' + id).
michael@0 37 replace(/[^a-z0-9_-]/g, '');
michael@0 38
michael@0 39 const ToggleButton = Class({
michael@0 40 extends: EventTarget,
michael@0 41 implements: [
michael@0 42 properties(toggleStateContract),
michael@0 43 state(toggleStateContract),
michael@0 44 Disposable
michael@0 45 ],
michael@0 46 setup: function setup(options) {
michael@0 47 let state = merge({
michael@0 48 disabled: false,
michael@0 49 checked: false
michael@0 50 }, toggleButtonContract(options));
michael@0 51
michael@0 52 let id = toWidgetId(options.id);
michael@0 53
michael@0 54 register(this, state);
michael@0 55
michael@0 56 // Setup listeners.
michael@0 57 setListeners(this, options);
michael@0 58
michael@0 59 buttons.set(id, this);
michael@0 60
michael@0 61 view.create(merge({ type: 'checkbox' }, state, { id: id }));
michael@0 62 },
michael@0 63
michael@0 64 dispose: function dispose() {
michael@0 65 let id = toWidgetId(this.id);
michael@0 66 buttons.delete(id);
michael@0 67
michael@0 68 off(this);
michael@0 69
michael@0 70 view.dispose(id);
michael@0 71
michael@0 72 unregister(this);
michael@0 73 },
michael@0 74
michael@0 75 get id() this.state().id,
michael@0 76
michael@0 77 click: function click() view.click(toWidgetId(this.id))
michael@0 78 });
michael@0 79 exports.ToggleButton = ToggleButton;
michael@0 80
michael@0 81 identify.define(ToggleButton, ({id}) => toWidgetId(id));
michael@0 82
michael@0 83 getNodeView.define(ToggleButton, button =>
michael@0 84 view.nodeFor(toWidgetId(button.id))
michael@0 85 );
michael@0 86
michael@0 87 let toggleButtonStateEvents = events.filter(stateEvents,
michael@0 88 e => e.target instanceof ToggleButton);
michael@0 89
michael@0 90 let toggleButtonViewEvents = events.filter(viewEvents,
michael@0 91 e => buttons.has(e.target));
michael@0 92
michael@0 93 let clickEvents = events.filter(toggleButtonViewEvents, e => e.type === 'click');
michael@0 94 let updateEvents = events.filter(toggleButtonViewEvents, e => e.type === 'update');
michael@0 95
michael@0 96 on(toggleButtonStateEvents, 'data', ({target, window, state}) => {
michael@0 97 let id = toWidgetId(target.id);
michael@0 98
michael@0 99 view.setIcon(id, window, state.icon);
michael@0 100 view.setLabel(id, window, state.label);
michael@0 101 view.setDisabled(id, window, state.disabled);
michael@0 102 view.setChecked(id, window, state.checked);
michael@0 103 });
michael@0 104
michael@0 105 on(clickEvents, 'data', ({target: id, window, checked }) => {
michael@0 106 let button = buttons.get(id);
michael@0 107 let windowState = getStateFor(button, window);
michael@0 108
michael@0 109 let newWindowState = merge({}, windowState, { checked: checked });
michael@0 110
michael@0 111 setStateFor(button, window, newWindowState);
michael@0 112
michael@0 113 let state = getDerivedStateFor(button, getActiveTab(window));
michael@0 114
michael@0 115 emit(button, 'click', state);
michael@0 116
michael@0 117 emit(button, 'change', state);
michael@0 118 });
michael@0 119
michael@0 120 on(updateEvents, 'data', ({target: id, window}) => {
michael@0 121 render(buttons.get(id), window);
michael@0 122 });

mercurial