addon-sdk/source/lib/sdk/ui/button/contract.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 const { contract } = require('../../util/contract');
michael@0 7 const { isLocalURL } = require('../../url');
michael@0 8 const { isNil, isObject, isString } = require('../../lang/type');
michael@0 9 const { required, either, string, boolean, object } = require('../../deprecated/api-utils');
michael@0 10 const { merge } = require('../../util/object');
michael@0 11 const { freeze } = Object;
michael@0 12
michael@0 13 function isIconSet(icons) {
michael@0 14 return Object.keys(icons).
michael@0 15 every(size => String(size >>> 0) === size && isLocalURL(icons[size]))
michael@0 16 }
michael@0 17
michael@0 18 let iconSet = {
michael@0 19 is: either(object, string),
michael@0 20 map: v => isObject(v) ? freeze(merge({}, v)) : v,
michael@0 21 ok: v => (isString(v) && isLocalURL(v)) || (isObject(v) && isIconSet(v)),
michael@0 22 msg: 'The option "icon" must be a local URL or an object with ' +
michael@0 23 'numeric keys / local URL values pair.'
michael@0 24 }
michael@0 25
michael@0 26 let id = {
michael@0 27 is: string,
michael@0 28 ok: v => /^[a-z-_][a-z0-9-_]*$/i.test(v),
michael@0 29 msg: 'The option "id" must be a valid alphanumeric id (hyphens and ' +
michael@0 30 'underscores are allowed).'
michael@0 31 };
michael@0 32
michael@0 33 let label = {
michael@0 34 is: string,
michael@0 35 ok: v => isNil(v) || v.trim().length > 0,
michael@0 36 msg: 'The option "label" must be a non empty string'
michael@0 37 }
michael@0 38
michael@0 39 let stateContract = contract({
michael@0 40 label: label,
michael@0 41 icon: iconSet,
michael@0 42 disabled: boolean
michael@0 43 });
michael@0 44
michael@0 45 exports.stateContract = stateContract;
michael@0 46
michael@0 47 let buttonContract = contract(merge({}, stateContract.rules, {
michael@0 48 id: required(id),
michael@0 49 label: required(label),
michael@0 50 icon: required(iconSet)
michael@0 51 }));
michael@0 52
michael@0 53 exports.buttonContract = buttonContract;
michael@0 54
michael@0 55 exports.toggleStateContract = contract(merge({
michael@0 56 checked: boolean
michael@0 57 }, stateContract.rules));
michael@0 58
michael@0 59 exports.toggleButtonContract = contract(merge({
michael@0 60 checked: boolean
michael@0 61 }, buttonContract.rules));
michael@0 62

mercurial