addon-sdk/source/lib/sdk/ui/toolbar/model.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 { EventTarget } = require("../../event/target");
michael@0 15 const { off, setListeners, emit } = require("../../event/core");
michael@0 16 const { Reactor, foldp, merges, send } = require("../../event/utils");
michael@0 17 const { Disposable } = require("../../core/disposable");
michael@0 18 const { InputPort } = require("../../input/system");
michael@0 19 const { OutputPort } = require("../../output/system");
michael@0 20 const { identify } = require("../id");
michael@0 21 const { pairs, object, map, each } = require("../../util/sequence");
michael@0 22 const { patch, diff } = require("diffpatcher/index");
michael@0 23 const { contract } = require("../../util/contract");
michael@0 24 const { id: addonID } = require("../../self");
michael@0 25
michael@0 26 // Input state is accumulated from the input received form the toolbar
michael@0 27 // view code & local output. Merging local output reflects local state
michael@0 28 // changes without complete roundloop.
michael@0 29 const input = foldp(patch, {}, new InputPort({ id: "toolbar-changed" }));
michael@0 30 const output = new OutputPort({ id: "toolbar-change" });
michael@0 31
michael@0 32 // Takes toolbar title and normalizes is to an
michael@0 33 // identifier, also prefixes with add-on id.
michael@0 34 const titleToId = title =>
michael@0 35 ("toolbar-" + addonID + "-" + title).
michael@0 36 toLowerCase().
michael@0 37 replace(/\s/g, "-").
michael@0 38 replace(/[^A-Za-z0-9_\-]/g, "");
michael@0 39
michael@0 40 const validate = contract({
michael@0 41 title: {
michael@0 42 is: ["string"],
michael@0 43 ok: x => x.length > 0,
michael@0 44 msg: "The `option.title` string must be provided"
michael@0 45 },
michael@0 46 items: {
michael@0 47 is:["undefined", "object", "array"],
michael@0 48 msg: "The `options.items` must be iterable sequence of items"
michael@0 49 },
michael@0 50 hidden: {
michael@0 51 is: ["boolean", "undefined"],
michael@0 52 msg: "The `options.hidden` must be boolean"
michael@0 53 }
michael@0 54 });
michael@0 55
michael@0 56 // Toolbars is a mapping between `toolbar.id` & `toolbar` instances,
michael@0 57 // which is used to find intstance for dispatching events.
michael@0 58 let toolbars = new Map();
michael@0 59
michael@0 60 const Toolbar = Class({
michael@0 61 extends: EventTarget,
michael@0 62 implements: [Disposable],
michael@0 63 initialize: function(params={}) {
michael@0 64 const options = validate(params);
michael@0 65 const id = titleToId(options.title);
michael@0 66
michael@0 67 if (toolbars.has(id))
michael@0 68 throw Error("Toolbar with this id already exists: " + id);
michael@0 69
michael@0 70 // Set of the items in the toolbar isn't mutable, as a matter of fact
michael@0 71 // it just defines desired set of items, actual set is under users
michael@0 72 // control. Conver test to an array and freeze to make sure users won't
michael@0 73 // try mess with it.
michael@0 74 const items = Object.freeze(options.items ? [...options.items] : []);
michael@0 75
michael@0 76 const initial = {
michael@0 77 id: id,
michael@0 78 title: options.title,
michael@0 79 // By default toolbars are visible when add-on is installed, unless
michael@0 80 // add-on authors decides it should be hidden. From that point on
michael@0 81 // user is in control.
michael@0 82 collapsed: !!options.hidden,
michael@0 83 // In terms of state only identifiers of items matter.
michael@0 84 items: items.map(identify)
michael@0 85 };
michael@0 86
michael@0 87 this.id = id;
michael@0 88 this.items = items;
michael@0 89
michael@0 90 toolbars.set(id, this);
michael@0 91 setListeners(this, params);
michael@0 92
michael@0 93 // Send initial state to the host so it can reflect it
michael@0 94 // into a user interface.
michael@0 95 send(output, object([id, initial]));
michael@0 96 },
michael@0 97
michael@0 98 get title() {
michael@0 99 const state = reactor.value[this.id];
michael@0 100 return state && state.title;
michael@0 101 },
michael@0 102 get hidden() {
michael@0 103 const state = reactor.value[this.id];
michael@0 104 return state && state.collapsed;
michael@0 105 },
michael@0 106
michael@0 107 destroy: function() {
michael@0 108 send(output, object([this.id, null]));
michael@0 109 },
michael@0 110 // `JSON.stringify` serializes objects based of the return
michael@0 111 // value of this method. For convinienc we provide this method
michael@0 112 // to serialize actual state data. Note: items will also be
michael@0 113 // serialized so they should probably implement `toJSON`.
michael@0 114 toJSON: function() {
michael@0 115 return {
michael@0 116 id: this.id,
michael@0 117 title: this.title,
michael@0 118 hidden: this.hidden,
michael@0 119 items: this.items
michael@0 120 };
michael@0 121 }
michael@0 122 });
michael@0 123 exports.Toolbar = Toolbar;
michael@0 124 identify.define(Toolbar, toolbar => toolbar.id);
michael@0 125
michael@0 126 const dispose = toolbar => {
michael@0 127 toolbars.delete(toolbar.id);
michael@0 128 emit(toolbar, "detach");
michael@0 129 off(toolbar);
michael@0 130 };
michael@0 131
michael@0 132 const reactor = new Reactor({
michael@0 133 onStep: (present, past) => {
michael@0 134 const delta = diff(past, present);
michael@0 135
michael@0 136 each(([id, update]) => {
michael@0 137 const toolbar = toolbars.get(id);
michael@0 138
michael@0 139 // Remove
michael@0 140 if (!update)
michael@0 141 dispose(toolbar);
michael@0 142 // Add
michael@0 143 else if (!past[id])
michael@0 144 emit(toolbar, "attach");
michael@0 145 // Update
michael@0 146 else
michael@0 147 emit(toolbar, update.collapsed ? "hide" : "show", toolbar);
michael@0 148 }, pairs(delta));
michael@0 149 }
michael@0 150 });
michael@0 151 reactor.run(input);

mercurial