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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/ui/button/contract.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,62 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +* License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +const { contract } = require('../../util/contract');
    1.10 +const { isLocalURL } = require('../../url');
    1.11 +const { isNil, isObject, isString } = require('../../lang/type');
    1.12 +const { required, either, string, boolean, object } = require('../../deprecated/api-utils');
    1.13 +const { merge } = require('../../util/object');
    1.14 +const { freeze } = Object;
    1.15 +
    1.16 +function isIconSet(icons) {
    1.17 +  return Object.keys(icons).
    1.18 +    every(size => String(size >>> 0) === size && isLocalURL(icons[size]))
    1.19 +}
    1.20 +
    1.21 +let iconSet = {
    1.22 +  is: either(object, string),
    1.23 +  map: v => isObject(v) ? freeze(merge({}, v)) : v,
    1.24 +  ok: v => (isString(v) && isLocalURL(v)) || (isObject(v) && isIconSet(v)),
    1.25 +  msg: 'The option "icon" must be a local URL or an object with ' +
    1.26 +    'numeric keys / local URL values pair.'
    1.27 +}
    1.28 +
    1.29 +let id = {
    1.30 +  is: string,
    1.31 +  ok: v => /^[a-z-_][a-z0-9-_]*$/i.test(v),
    1.32 +  msg: 'The option "id" must be a valid alphanumeric id (hyphens and ' +
    1.33 +        'underscores are allowed).'
    1.34 +};
    1.35 +
    1.36 +let label = {
    1.37 +  is: string,
    1.38 +  ok: v => isNil(v) || v.trim().length > 0,
    1.39 +  msg: 'The option "label" must be a non empty string'
    1.40 +}
    1.41 +
    1.42 +let stateContract = contract({
    1.43 +  label: label,
    1.44 +  icon: iconSet,
    1.45 +  disabled: boolean
    1.46 +});
    1.47 +
    1.48 +exports.stateContract = stateContract;
    1.49 +
    1.50 +let buttonContract = contract(merge({}, stateContract.rules, {
    1.51 +  id: required(id),
    1.52 +  label: required(label),
    1.53 +  icon: required(iconSet)
    1.54 +}));
    1.55 +
    1.56 +exports.buttonContract = buttonContract;
    1.57 +
    1.58 +exports.toggleStateContract = contract(merge({
    1.59 +  checked: boolean
    1.60 +}, stateContract.rules));
    1.61 +
    1.62 +exports.toggleButtonContract = contract(merge({
    1.63 +  checked: boolean
    1.64 +}, buttonContract.rules));
    1.65 +

mercurial