Wed, 31 Dec 2014 06:09:35 +0100
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 { buttonContract, stateContract } = require('./contract'); |
michael@0 | 22 | const { properties, render, state, register, unregister, |
michael@0 | 23 | 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 | ('action-button--' + addonID.toLowerCase()+ '-' + id). |
michael@0 | 37 | replace(/[^a-z0-9_-]/g, ''); |
michael@0 | 38 | |
michael@0 | 39 | const ActionButton = Class({ |
michael@0 | 40 | extends: EventTarget, |
michael@0 | 41 | implements: [ |
michael@0 | 42 | properties(stateContract), |
michael@0 | 43 | state(stateContract), |
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 | }, buttonContract(options)); |
michael@0 | 50 | |
michael@0 | 51 | let id = toWidgetId(options.id); |
michael@0 | 52 | |
michael@0 | 53 | register(this, state); |
michael@0 | 54 | |
michael@0 | 55 | // Setup listeners. |
michael@0 | 56 | setListeners(this, options); |
michael@0 | 57 | |
michael@0 | 58 | buttons.set(id, this); |
michael@0 | 59 | |
michael@0 | 60 | view.create(merge({}, state, { id: id })); |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | dispose: function dispose() { |
michael@0 | 64 | let id = toWidgetId(this.id); |
michael@0 | 65 | buttons.delete(id); |
michael@0 | 66 | |
michael@0 | 67 | off(this); |
michael@0 | 68 | |
michael@0 | 69 | view.dispose(id); |
michael@0 | 70 | |
michael@0 | 71 | unregister(this); |
michael@0 | 72 | }, |
michael@0 | 73 | |
michael@0 | 74 | get id() this.state().id, |
michael@0 | 75 | |
michael@0 | 76 | click: function click() { view.click(toWidgetId(this.id)) } |
michael@0 | 77 | }); |
michael@0 | 78 | exports.ActionButton = ActionButton; |
michael@0 | 79 | |
michael@0 | 80 | identify.define(ActionButton, ({id}) => toWidgetId(id)); |
michael@0 | 81 | |
michael@0 | 82 | getNodeView.define(ActionButton, button => |
michael@0 | 83 | view.nodeFor(toWidgetId(button.id)) |
michael@0 | 84 | ); |
michael@0 | 85 | |
michael@0 | 86 | let actionButtonStateEvents = events.filter(stateEvents, |
michael@0 | 87 | e => e.target instanceof ActionButton); |
michael@0 | 88 | |
michael@0 | 89 | let actionButtonViewEvents = events.filter(viewEvents, |
michael@0 | 90 | e => buttons.has(e.target)); |
michael@0 | 91 | |
michael@0 | 92 | let clickEvents = events.filter(actionButtonViewEvents, e => e.type === 'click'); |
michael@0 | 93 | let updateEvents = events.filter(actionButtonViewEvents, e => e.type === 'update'); |
michael@0 | 94 | |
michael@0 | 95 | on(clickEvents, 'data', ({target: id, window}) => { |
michael@0 | 96 | let button = buttons.get(id); |
michael@0 | 97 | let state = getDerivedStateFor(button, getActiveTab(window)); |
michael@0 | 98 | |
michael@0 | 99 | emit(button, 'click', state); |
michael@0 | 100 | }); |
michael@0 | 101 | |
michael@0 | 102 | on(updateEvents, 'data', ({target: id, window}) => { |
michael@0 | 103 | render(buttons.get(id), window); |
michael@0 | 104 | }); |
michael@0 | 105 | |
michael@0 | 106 | on(actionButtonStateEvents, 'data', ({target, window, state}) => { |
michael@0 | 107 | let id = toWidgetId(target.id); |
michael@0 | 108 | view.setIcon(id, window, state.icon); |
michael@0 | 109 | view.setLabel(id, window, state.label); |
michael@0 | 110 | view.setDisabled(id, window, state.disabled); |
michael@0 | 111 | }); |