mirror of
https://github.com/pebble-dev/clay.git
synced 2026-09-01 06:46:49 -04:00
@@ -0,0 +1,4 @@
|
||||
coverage/
|
||||
dist/
|
||||
tmp/
|
||||
src/scripts/vendor/
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"extends": "pebble",
|
||||
"env": {
|
||||
"node": true,
|
||||
"browser": true
|
||||
},
|
||||
"globals": {
|
||||
"Pebble": true
|
||||
},
|
||||
"rules": {
|
||||
"new-cap": [0],
|
||||
"max-params": [2, 4],
|
||||
"require-jsdoc": [2, {
|
||||
"require": {
|
||||
"FunctionDeclaration": true,
|
||||
"MethodDefinition": true,
|
||||
"ClassDeclaration": false
|
||||
}
|
||||
}],
|
||||
"valid-jsdoc": [2, {
|
||||
"requireParamDescription": false,
|
||||
"requireReturnDescription": false,
|
||||
"requireReturnType": true
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
Regular → Executable
+2
@@ -25,3 +25,5 @@ build/Release
|
||||
# Dependency directory
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
tmp
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
language: node_js
|
||||
sudo: false
|
||||
node_js:
|
||||
- '4'
|
||||
|
||||
before_script:
|
||||
# Needed to include Chrome in Travis for karma tests
|
||||
- export CHROME_BIN=chromium-browser
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
|
||||
script: 'npm run test-travis'
|
||||
@@ -1,340 +1,21 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
Copyright (c) 2016 Pebble Technology
|
||||
|
||||
Preamble
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
{description}
|
||||
Copyright (C) {year} {fullname}
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
{signature of Ty Coon}, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
@@ -0,0 +1,682 @@
|
||||
# Clay
|
||||
Clay is a JavaScript library that makes it super easy to add offline configuration pages to your Pebble apps. All you need to get started is a couple lines of JavaScript and a JSON file, no servers or HTML required.
|
||||
|
||||
**Clay is still in early development and missing some features. We would love your feedback so please submit any ideas or features you would like via GitHub issues.**
|
||||
|
||||
# Getting Started
|
||||
|
||||
Clay will eventually be built into the Pebble SDK. However while it is still in beta, you will need to follow some steps
|
||||
|
||||
1. Download the Clay distribution file from: [dist/clay.js](dist/clay.js)
|
||||
2. Drop `clay.js` in your project's `src/js` directory.
|
||||
3. Create a JSON file called `config.json` and place it in your `src/js` directory.
|
||||
4. in order for JSON files to work you may need to change the line in your `wscript` from `ctx.pbl_bundle(binaries=binaries, js=ctx.path.ant_glob('src/js/**/*.js'))` to `ctx.pbl_bundle(binaries=binaries, js=ctx.path.ant_glob('src/js/**/*.js*'))`
|
||||
5. Your `app.js` needs to `require` clay and your config file, then be initialized. Clay will by default, automatically handle the 'showConfiguration' and 'webviewclosed' events:
|
||||
```javascript
|
||||
var Clay = require('clay');
|
||||
var clayConfig = require('config.json');
|
||||
var clay = new Clay(clayConfig);
|
||||
```
|
||||
|
||||
8. Next is the fun part. Creating your config page. Edit your `config.json` file using the instructions below
|
||||
|
||||
# Creating Your Config File
|
||||
|
||||
Clay uses JavaScript objects (or JSON) to generate the config page for you. The structure of the page is totally up to you, but you do need to follow some basic rules.
|
||||
|
||||
## Basic JSON structure
|
||||
|
||||
Your root element should be an array. This represents the entire page. Inside this array you place your config items. Each config item is an object with some properties that configure how each item should be displayed.
|
||||
|
||||
#### Example:
|
||||
```javascript
|
||||
[
|
||||
{ "type": "heading", "defaultValue": "Example Config Page" },
|
||||
{ "type": "text", "defaultValue": "Clay makes things easy." }
|
||||
//... etc etc
|
||||
]
|
||||
```
|
||||
|
||||
## Components
|
||||
|
||||

|
||||
|
||||
### Section
|
||||
|
||||
Sections help divide up the page into logical separations. It is recommended that you place all your input based items in a section for maximum prettiness.
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description |
|
||||
|----------|------|-------------|
|
||||
| type | string | set to "section"
|
||||
| items | array | array of items to include in the section
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "section",
|
||||
"items": [
|
||||
{
|
||||
"type": "heading",
|
||||
"defaultValue": "This is a section"
|
||||
},
|
||||
{
|
||||
"type": "input",
|
||||
"appKey": "email",
|
||||
"label": "Email"
|
||||
},
|
||||
{
|
||||
"type": "toggle",
|
||||
"appKey": "enableThings",
|
||||
"label": "Enable things"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Heading
|
||||
|
||||
**Manipulator:** `html`
|
||||
|
||||
Headings can be used in anywhere and can have their size adjusted to suit the context. If you place a heading item at the first position of a **section's** `items` array then it will automatically be styled as a header for that section.
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "heading"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| defaultValue | string/HTML | The heading text.
|
||||
| size | int | Defaults to `4`. An integer from 1 to 6 where 1 is the largest size and 6 is the smallest. (represents HTML `<h1>`, `<h2>`, `<h3>`, etc)
|
||||
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "heading",
|
||||
"id": "main-heading",
|
||||
"defaultValue": "My Cool Watchface",
|
||||
"size": 1
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Text
|
||||
|
||||
**Manipulator:** `html`
|
||||
|
||||
Text is used to provide descriptions of sections or to explain complex parts of your page. Feel free to add any extra HTML you require to the `defaultValue`
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "text"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| defaultValue | string/HTML | The content of the text.
|
||||
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "text",
|
||||
"defaultValue": "An explanation of how things work",
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Input
|
||||
|
||||
**Manipulator:** `val`
|
||||
|
||||
Standard text input field.
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "input"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| label | string | The label that should appear next to the item
|
||||
| defaultValue | string | The default value of the text field
|
||||
| attributes | object | A hash of HTML attributes to set on the input field. You can add basic HTML5 validation this way by setting attributes such as `required` or `type` .
|
||||
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "input",
|
||||
"appKey": "email",
|
||||
"defaultValue": "",
|
||||
"label": "Email",
|
||||
"attributes": {
|
||||
"placeholder": "eg: name@domain.com",
|
||||
"limit": 10,
|
||||
"required": "required",
|
||||
"type": "email"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Toggle
|
||||
|
||||
**Manipulator:** `checked`
|
||||
|
||||
Switch for a single item.
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "toggle"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| label | string | The label that should appear next to the item
|
||||
| defaultValue | boolean | The default value of the toggle. Defaults to `false`
|
||||
| attributes | object | A hash of HTML attributes to set on the input field. You can add basic HTML5 validation this way by setting attribute such as `required`.
|
||||
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "toggle",
|
||||
"appKey": "invert",
|
||||
"label": "Invert Colors",
|
||||
"defaultValue": true,
|
||||
"attributes": {
|
||||
"required": "required"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Select
|
||||
|
||||
**Manipulator:** `val`
|
||||
|
||||
A dropdown menu
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "select"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| label | string | The label that should appear next to the item
|
||||
| defaultValue | string | The default value of dropdown. Must match a value in the `options` array
|
||||
| attributes | object | A hash of HTML attributes to set on the input field. You can add basic HTML5 validation this way by setting attribute such as `required`.
|
||||
| options | array of objects | The options you want to appear in the dropdown. Each option is an object with a `label` and `value` property.
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "select",
|
||||
"appKey": "flavor",
|
||||
"defaultValue": "grape",
|
||||
"label": "Favorite Flavor",
|
||||
"options": [
|
||||
{ "label": "", "value": "" },
|
||||
{ "label": "Berry", "value": "berry" },
|
||||
{ "label": "Grape", "value": "grape" },
|
||||
{ "label": "Banana", "value": "banana" }
|
||||
],
|
||||
"attributes": {
|
||||
"required": "required"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Color
|
||||
|
||||
**Manipulator:** `color`
|
||||
|
||||
A color picker.
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "color"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| label | string | The label that should appear next to the item
|
||||
| defaultValue | string OR int | The default color. Always use the uncorrected value even if `sunlight` is true. The component will do the conversion internally.
|
||||
| sunlight | boolean | Switch between uncorrected and sunlight color palette. Defaults to `true`
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "color",
|
||||
"appKey": "background",
|
||||
"defaultValue": "FF0000",
|
||||
"label": "Background Color",
|
||||
"sunlight": true
|
||||
},
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### RadioGroup
|
||||
|
||||
**Manipulator:** `radiogroup`
|
||||
|
||||
A list of options where a user can only choose one
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "radiogroup"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| label | string | The label that should appear next to the item
|
||||
| defaultValue | string | The default selected item. Must match a value in the `options` array
|
||||
| attributes | object | A hash of HTML attributes to set on the input field. You can add basic HTML5 validation this way by setting attribute such as `required`.
|
||||
| options | array of objects | The options you want to appear in the dropdown. Each option is an object with a `label` and `value` property.
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "radiogroup",
|
||||
"appKey": "favorite_food",
|
||||
"label": "Favorite Food",
|
||||
"options": [
|
||||
{ "label": "Sushi", "value": "sushi" },
|
||||
{ "label": "Pizza", "value": "pizza" },
|
||||
{ "label": "Burgers", "value": "burgers" }
|
||||
]
|
||||
},
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### CheckboxGroup
|
||||
|
||||
**Manipulator:** `checkboxgroup`
|
||||
|
||||
A list of options where a user may choose multiple
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "checkboxgroup"
|
||||
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your custom function.
|
||||
| appKey | string (unique) | The appKey matching what is defined in your `appinfo.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByAppKey()` in your custom function. You must set this if you wish for the value of this item to be saved after the user closes the config page.
|
||||
| label | string | The label that should appear next to the item
|
||||
| defaultValue | array of strings | The default selected items. Must match the values in the `options` array
|
||||
| attributes | object | A hash of HTML attributes to set on the input field. You can add basic HTML5 validation this way by setting attribute such as `required`.
|
||||
| options | array of objects | The options you want to appear in the dropdown. Each option is an object with a `label` and `value` property.
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "checkboxgroup",
|
||||
"appKey": "favorite_food",
|
||||
"label": "Favorite Food",
|
||||
"defaultValue": ["sushi", "burgers"],
|
||||
"options": [
|
||||
{ "label": "Sushi", "value": "sushi" },
|
||||
{ "label": "Pizza", "value": "pizza" },
|
||||
{ "label": "Burgers", "value": "burgers" }
|
||||
]
|
||||
},
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Submit
|
||||
|
||||
**Manipulator:** `html`
|
||||
|
||||
The submit button for the page. You **MUST** include this component somewhere or users will not be able to save the form.
|
||||
|
||||
##### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|-------------
|
||||
| type | string | set to "submit"
|
||||
| defaultValue | string | The text displayed in the button
|
||||
| attributes | object | A hash of HTML attributes to set on the input field.
|
||||
|
||||
##### Example
|
||||
```javascript
|
||||
{
|
||||
"type": "submit",
|
||||
"label": "Save"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Coming Soon...
|
||||
|
||||
- Range Slider
|
||||
- Generic Button
|
||||
- Tabs
|
||||
- Footer
|
||||
- dynamic + draggable list
|
||||
|
||||
---
|
||||
|
||||
## Manipulators
|
||||
|
||||
Each component has a **manipulator.** This is a set of methods used to talk to the item on the page. At a minimum, manipulators must have a `.get()` and `.set(value)` method. When the config page is closed, the `.get()` method is run on all components registered with an `appKey`. Many of these methods fire an event when the method is called. You can listen for these events with `ClayItem.on()`
|
||||
|
||||
#### html
|
||||
| Method | Returns | Event Fired | Description
|
||||
|--------|---------|-------------| ----------
|
||||
| `.set( [string/HTML] value)` | `ClayItem` | `change` | sets the content of the item
|
||||
| `.get()` | `string` | | gets the content of the item
|
||||
|
||||
#### val
|
||||
| Method | Returns | Event Fired | Description
|
||||
|--------|---------|-------------| ----------
|
||||
| `.set( [string] value)` | `ClayItem` | `change` | sets the value of the item
|
||||
| `.get()` | `string` | | gets the content of the item
|
||||
| `.disable()` | `ClayItem` | `disabled` | Prevents the item from being edited by the user
|
||||
| `.enable()` | `ClayItem` | `enabled` | Allows the item to be edited by the user
|
||||
|
||||
#### checked
|
||||
| Method | Returns | Event Fired | Description
|
||||
|--------|---------|-------------| ----------
|
||||
| `.set( [boolean] value)` | `ClayItem` | `change` | check/uncheck the state of the item
|
||||
| `.get()` | `string` | | gets the content of the item
|
||||
| `.disable()` | `ClayItem` | `disabled` | Prevents the item from being edited by the user
|
||||
| `.enable()` | `ClayItem` | `enabled` | Allows the item to be edited by the user
|
||||
|
||||
#### color
|
||||
| Method | Returns | Event Fired | Description
|
||||
|--------|---------|-------------| ----------
|
||||
| `.set( [string \| int] value)` | `ClayItem` | `change` | sets the color picker to the provided color. If the value is a string, it must be provided in hex notation eg `'FF0000'`.
|
||||
| `.get()` | `int` | | The color is returned as an int in order to make it easy to be used on the watch side using `GColorFromHEX()`
|
||||
| `.disable()` | `ClayItem` | `disabled` | Prevents the item from being edited by the user
|
||||
| `.enable()` | `ClayItem` | `enabled` | Allows the item to be edited by the user
|
||||
|
||||
#### radiogroup
|
||||
| Method | Returns | Event Fired | Description
|
||||
|--------|---------|-------------| ----------
|
||||
| `.set( [string] value)` | `ClayItem` | `change` | checks the radio button that corresponds to the provided value
|
||||
| `.get()` | `string` | | gets the value of the checked radio button in the list
|
||||
| `.disable()` | `ClayItem` | `disabled` | Prevents the item from being edited by the user
|
||||
| `.enable()` | `ClayItem` | `enabled` | Allows the item to be edited by the user
|
||||
|
||||
#### checkboxgroup
|
||||
| Method | Returns | Event Fired | Description
|
||||
|--------|---------|-------------| ----------
|
||||
| `.set( [Array] value)` | `ClayItem` | `change` | checks the checkboxes that corresponds to the provided list of values
|
||||
| `.get()` | `string` | | gets the value of the checked radio button in the list
|
||||
| `.disable()` | `ClayItem` | `disabled` | Prevents the item from being edited by the user
|
||||
| `.enable()` | `ClayItem` | `enabled` | Allows the item to be edited by the user
|
||||
|
||||
|
||||
# Extending Clay
|
||||
|
||||
Clay is built to allow developers to add their own basic interactivity to the config page. This is done in a number of ways:
|
||||
|
||||
## Handling The 'showConfiguration' and 'webviewclosed' Events Manually
|
||||
|
||||
Clay will by default, automatically handle the 'showConfiguration' and 'webviewclosed' events. If you wish to override this behavior and handle the events yourself, pass an object as the 3rd parameter of the Clay constructor with `autoHandleEvents` set to `false`
|
||||
|
||||
Example:
|
||||
|
||||
```javascript
|
||||
var Clay = require('./clay');
|
||||
var clayConfig = require('./config');
|
||||
var clay = new Clay(clayConfig, null, {autoHandleEvents: false});
|
||||
|
||||
Pebble.addEventListener('showConfiguration', function(e) {
|
||||
Pebble.openURL(clay.generateUrl());
|
||||
});
|
||||
|
||||
Pebble.addEventListener('webviewclosed', function(e) {
|
||||
|
||||
if (e && !e.response) { return; }
|
||||
|
||||
// Send settings to Pebble watchapp
|
||||
Pebble.sendAppMessage(clay.getSettings(e.response), function(e) {
|
||||
console.log('Sent config data to Pebble');
|
||||
}, function() {
|
||||
console.log('Failed to send config data!');
|
||||
console.log(JSON.stringify(e));
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### `Clay([Array] config, [function] customFn, [object] options)`
|
||||
|
||||
#### Methods
|
||||
|
||||
| Method | Returns
|
||||
| ---
|
||||
| `Clay(Array] config, [function] customFn=null, [object] options={autoHandleEvents: true})` <br> `config` - an Array representing your config <br> `customFn` - function to be run in the context of the generated page <br> `options.autoHandleEvents` - set to `false` to prevent Clay from automatically handling the "showConfiguration" and "webviewclosed" events | `Clay` - a new instance of Clay
|
||||
| `.registerComponent( [ClayComponent] component )` <br> Registers a custom component. | `void`.
|
||||
| `.generateUrl()` | `string` - The URL to open with `Pebble.openURL()`
|
||||
| `.getSettings(response)` <br> `response` - the response object provided to the "webviewclosed" event | `Object` - hash where the key is the `appKey` and the value is the result from the config page.
|
||||
---
|
||||
|
||||
|
||||
## Custom Function
|
||||
|
||||
When initializing Clay in your `app.js`, you can optionally provide a function that will be copied and run on the generated config page. **IMPORTANT:** This function is injected by running `.toString()` on it. If you are making use of `require` or any other dynamic features, they will not work. You must make sure that everything the function needs to execute is available in the function body itself.
|
||||
|
||||
This function, when injected into the config page will be run with `ClayConfig` as its context (`this`), and **Minified** as its first parameter. Read below for more information on **Minified**
|
||||
|
||||
#### Example
|
||||
|
||||
##### app.js
|
||||
|
||||
```javascript
|
||||
var Clay = require('./clay');
|
||||
var clayConfig = require('./config');
|
||||
var customClay = require('./custom-clay');
|
||||
var clay = new Clay(clayConfig, customClay);
|
||||
```
|
||||
|
||||
##### custom-clay.js
|
||||
|
||||
```javascript
|
||||
module.exports = function(minified) {
|
||||
var Clay = this;
|
||||
var _ = minified._;
|
||||
var $ = minified.$;
|
||||
var HTML = minified.HTML;
|
||||
|
||||
function toggleBackground() {
|
||||
if (this.get()) {
|
||||
Clay.getItemByAppKey('background').enable();
|
||||
} else {
|
||||
Clay.getItemByAppKey('background').disable();
|
||||
}
|
||||
}
|
||||
|
||||
Clay.on(Clay.EVENTS.AFTER_BUILD, function() {
|
||||
var coolStuffToggle = Clay.getItemByAppKey('cool_stuff');
|
||||
toggleBackground.call(coolStuffToggle);
|
||||
coolStuffToggle.on('change', toggleBackground);
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
## Clay API
|
||||
|
||||
### `ClayConfig([Object] settings, [Array] config, [$Minified] $rootContainer)`
|
||||
|
||||
This is the main way of talking to your generated config page.
|
||||
|
||||
#### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|------------
|
||||
| `.EVENTS.BEFORE_BUILD` | String | Dispatched prior to building the page.
|
||||
| `.EVENTS.AFTER_BUILD` | String | Dispatched after building the page.
|
||||
| `.config` | Array | Reference to the config passed to the constructer and used for generating the page.
|
||||
|
||||
|
||||
#### Methods
|
||||
|
||||
| Method | Returns
|
||||
|--------|------
|
||||
| `.getAllItems()` | `Array.<ConfigItem>` - an array of all config items
|
||||
| `.getItemByAppKey( [string] appKey )` | `ConfigItem\|undefined` - a single `ConfigItem` that has the provided `appKey` otherwise `undefined`
|
||||
| `.getItemById( [string] id )` | `ConfigItem\|undefined` - a single `ConfigItem` that has the provided `id` otherwise `undefined`
|
||||
| `.getItemsByType( [string] type )` | `Array.<ConfigItem>` - an array of config items that match the provided `type`
|
||||
| `.getSettings()` | `Object` - a hash representing all items with an `appKey` where the key is the `appKey` and the value is the result of running `.get()` on the Clay item.
|
||||
| `.build()` <br> Builds the config page. Will dispatch the `BEFORE_BUILD` event prior to building the page, then the `AFTER_BUILD` event once it is complete. | `ClayConfig`
|
||||
| `.on( [string] events, [function] handler )` <br> Register an event to the provided handler. The handler will be called with this instance of `ClayConfig` as the context. If you wish to register multiple events to the same handler, then separate the events with a space | `ClayConfig`
|
||||
| `.off( [function] handler )` <br> Remove the given event handler. **NOTE:** This will remove the handler from all registered events | `ClayConfig`
|
||||
| `.trigger( [string] name, [object] eventObj={} )` <br> Trigger the provided event and optionally pass extra data to the handler. | `ClayConfig`
|
||||
| `.registerComponent( [ClayComponent] component )` <br> Registers a component. You must register all components prior to calling `.build()`. This method is available statically as well | `Boolean` - `true` if the component was registered successfully, otherwise `false`.
|
||||
|
||||
---
|
||||
|
||||
### `ClayItem( [Object] config )`
|
||||
|
||||
#### Properties
|
||||
|
||||
| Property | Type | Description
|
||||
|----------|------|------------
|
||||
| `.id` | String | The ID of the item if provided in the config.
|
||||
| `.appKey` | String | The ID of the item if provided in the config.
|
||||
| `.config` | Object | Reference to the config passed to the constructer.
|
||||
| `$element` | $Minified | A Minified list representing the root HTML element of the config item
|
||||
| `$manipulatorTarget` | A Minified list representing the HTML element with **data-manipulator-target** set. This is generally pointing to the main `<input>` element and will be used for binding events.
|
||||
|
||||
|
||||
#### Methods
|
||||
|
||||
| Method | Returns
|
||||
|--------|----
|
||||
| `.initialize( [ClayConfig] clay)` <br> You shouldn't ever need to run this method manually as it will automatically be called when the config is built | `ConfigItem`
|
||||
| `.on( [string] events, [function] handler )` <br> Register an event to the provided handler. The handler will be called with this instance of `ClayItem` as the context. If you wish to register multiple events to the same handler, then separate the events with a space. Events will be registered against the `$manipulatorTarget` so most DOM events such as **"change"** or **"click"** can be listened for. | `ClayItem`
|
||||
| `.off( [function] handler )` <br> Remove the given event handler. **NOTE:** This will remove the handler from all registered events | `ClayItem`
|
||||
| `.trigger( [string] name, [object] eventObj={} )` <br> Trigger the provided event and optionally pass extra data to the handler. | `ClayItem`
|
||||
|
||||
In addition to the methods above, all the methods from the item's manipulator will be attached to the `CLayItem`. This includes `.set()` and `.get()`
|
||||
|
||||
---
|
||||
|
||||
## Custom Components
|
||||
|
||||
Clay is also able to be extended using custom components. This allows developers to share components with each other.
|
||||
|
||||
### Component Structure
|
||||
|
||||
Components are simple objects with the following properties:
|
||||
|
||||
#### `ClayComponent`
|
||||
|
||||
| Property | Type | Required | Description
|
||||
|----------|------|----------|---
|
||||
| name | string | yes | This is the unique way to identify the component and what will be used by the config item's `type`
|
||||
| template | string (HTML) | yes | This is the actual HTML content of the component. Make sure there is only **one** root node in the HTML. This HTML will be passed to minified's `HTML()` method. Any properties provided by the config item will be made available to the template, eg: `label`. The template will also be provided with `clayId` as a unique way to set input `name` attributes.
|
||||
| style | string | no | Any extra css styles you want to inject into the page make sure to namespace your CSS with a class that is unique to your component in order to avoid conflicts with other components
|
||||
| manipulator | string / manipulator | yes | Provide a string here to use one of the built-in manipulators. eg `val`. If an object is provided, it must have both a `.set(value)` and `.get()` method.
|
||||
| defaults | object | Only if your template requires it | An object of all the defaults your template requires.
|
||||
| initialize | function | no | Method which will be called after the item has been added to the page. It will be called with the `ClayItem` as the context (`this`) and with `minified` as the first parameter.
|
||||
|
||||
### Registering a custom component.
|
||||
|
||||
Components must be registered before the config page is built. The easiest way to do this is in your `app.js` after you have initialized Clay
|
||||
|
||||
```javascript
|
||||
var Clay = require('clay');
|
||||
var clayConfig = require('config.json');
|
||||
var clay = new Clay(clayConfig);
|
||||
|
||||
clay.registerComponent(require('./my-custom-component'));
|
||||
```
|
||||
|
||||
## Minified
|
||||
|
||||
Minified is a super light JQuery-like library. We only bundle in a small subset of its functionality. Visit the [Minified Docs](http://minifiedjs.com/api/) for more info on how to use minified. Below is the subset of methods available.
|
||||
|
||||
- `$()`
|
||||
- `$$()`
|
||||
- `.get()`
|
||||
- `.select()`
|
||||
- `.set()`
|
||||
- `.add()`
|
||||
- `.ht()`
|
||||
- `HTML()`
|
||||
- `$.request()`
|
||||
- `promise.always()`
|
||||
- `promise.error()`
|
||||
- `$.off()`
|
||||
- `$.ready()`
|
||||
- `$.wait()`
|
||||
- `.on()`
|
||||
- `.each()`
|
||||
- `.find()`
|
||||
- `_()`
|
||||
- `_.copyObj()`
|
||||
- `_.eachObj()`
|
||||
- `_.extend()`
|
||||
- `_.format()`
|
||||
- `_.formatHtml()`
|
||||
- `_.template()`
|
||||
- `_.isObject()`
|
||||
|
||||
# Project Structure and Development
|
||||
|
||||
There are two main entry points for Clay. `index.js` and `src/scripts/config-page.js`.
|
||||
|
||||
#### index.js
|
||||
|
||||
This is the main entry point for the code that will run in the Pebble app's `src/js/app.js`. It is responsible for serializing the provided config into a data URI that will be opened using `Pebble.openURL()`. It also persists data to local storage.
|
||||
|
||||
#### src/scripts/config-page.js
|
||||
|
||||
This is the main entry point for the code that runs on the generated config page. It is its responsibility to pass the injected config and other components to the `ClayConfig` class.
|
||||
|
||||
|
||||
### Building
|
||||
|
||||
There are two ways to build Clay, production mode and development mode.
|
||||
|
||||
#### Production Mode
|
||||
|
||||
`$ npm run build` packages up the entire Clay project into `dist/clay.js` to be required in the developer's `app.js`
|
||||
|
||||
#### Development Mode
|
||||
|
||||
`$ npm run dev` packages up `src/scripts/config-page.js` and `dev/dev.js` into the `tmp/` directory so `dev/dev.html` can include them as script tags. This will also watch for changes in the project.
|
||||
|
||||
While developing components and other functionality for Clay, it is much easier to work with the files in the `dev/` directory than on a phone or emulator. Below is an explanation of the files and their purpose
|
||||
|
||||
| File | Purpose
|
||||
|----------|------
|
||||
| `dev.html` | open this page in a browser after running `$ npm run dev`
|
||||
| `dev.js` | injects the components and dependancies things into the window the same way `index.js` would.
|
||||
| `config.js` | Clay config to use as a sandbox for testing components.
|
||||
| `custom-fn.js` | Clay custom function to be injected by `dev.js`
|
||||
| `emulator.html` | Copy of the html page that is used to make the Pebble SDK emulator play nice with Clay
|
||||
| `uri-test.html` | Used to stress test URI creation for older browser versions.
|
||||
|
||||
## Functionality
|
||||
|
||||
Most of the magic happens in the `src/scripts/lib` directory. `config-page.js` initializes a new instance of `ClayConfig` and calls the injected custom function (`window.customFn`) with the ClayConfig as its context. This allows developers to add extra functionality to the config page, such as setting values of items dynamically or registering small custom components.
|
||||
|
||||
Once the `ClayConfig` is initialized, we run the `.build()` method. This iterates over the config and injects each item into the page. Each item is an instance of ClayItem. It also indexes the items to later be retrieved with `.getAllItems()`, `.getItemByAppKey()`, `.getItemById()`, `.getItemsByType()`
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
'use strict';
|
||||
/* eslint-disable quotes */
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
"type": "heading",
|
||||
"id": "main-heading",
|
||||
"defaultValue": "Clay Test Page",
|
||||
"size": 1
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"defaultValue": "Some arbitrary text explaining how this all works. " +
|
||||
"It's cool if this wraps across multiple lines"
|
||||
},
|
||||
{
|
||||
"type": "section",
|
||||
"items": [
|
||||
{
|
||||
"type": "heading",
|
||||
"defaultValue": "This is a section"
|
||||
},
|
||||
{
|
||||
"type": "input",
|
||||
"appKey": "email",
|
||||
"defaultValue": "",
|
||||
"label": "Email",
|
||||
"attributes": {
|
||||
"placeholder": "eg: name@domain.com",
|
||||
"limit": 10,
|
||||
"required": "required",
|
||||
type: "email"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "toggle",
|
||||
"appKey": "cool_stuff",
|
||||
"label": "Enable Cool Stuff",
|
||||
"defaultValue": false
|
||||
},
|
||||
{
|
||||
"type": "color",
|
||||
"appKey": "colorTest",
|
||||
"defaultValue": "FF0000",
|
||||
"label": "Background Color",
|
||||
"sunlight": false
|
||||
},
|
||||
{
|
||||
"type": "color",
|
||||
"appKey": "sunnyColorTest",
|
||||
"defaultValue": "00FF00",
|
||||
"label": "Sunny Color",
|
||||
"sunlight": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "section",
|
||||
"items": [
|
||||
{
|
||||
"type": "heading",
|
||||
"defaultValue": "More Settings"
|
||||
},
|
||||
{
|
||||
"type": "radiogroup",
|
||||
"appKey": "radiogroup-test",
|
||||
"label": "Radio test",
|
||||
"options": [
|
||||
{ "label": "Test thing one", "value": "one" },
|
||||
{ "label": "Another thing", "value": "two" },
|
||||
{ "label": "One really long thing that would not fit", "value": "three" },
|
||||
{ "label": "Small", "value": "quote' \"test" },
|
||||
{ "label": "Final thing", "value": "three" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "checkboxgroup",
|
||||
"appKey": "checkboxgroup-test",
|
||||
"defaultValue": ["quote' \"test", "two"],
|
||||
"label": "Checkbox test",
|
||||
"options": [
|
||||
{ "label": "Test <strong>thing</strong> one", "value": "one" },
|
||||
{ "label": "Another thing", "value": "two" },
|
||||
{ "label": "One really long thing that would not fit", "value": "three" },
|
||||
{ "label": "Small", "value": "quote' \"test" },
|
||||
{ "label": "Final thing", "value": "three" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "input",
|
||||
"appKey": "date",
|
||||
"defaultValue": "",
|
||||
"label": "Range",
|
||||
"attributes": {
|
||||
type: "range"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "select",
|
||||
"appKey": "flavor",
|
||||
"defaultValue": "grape",
|
||||
"label": "Favorite Flavor",
|
||||
"options": [
|
||||
{ "label": "", "value": "" },
|
||||
{ "label": "Berry", "value": "berry" },
|
||||
{ "label": "Grape", "value": "grape" },
|
||||
{ "label": "Banana", "value": "banana" }
|
||||
],
|
||||
"attributes": {
|
||||
"required": "required"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "submit",
|
||||
"defaultValue": "Save"
|
||||
}
|
||||
];
|
||||
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function() {
|
||||
|
||||
/** @type {ClayConfig} */
|
||||
var Clay = window.Clay = this;
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
*/
|
||||
function toggleBackground() {
|
||||
if (this.get()) {
|
||||
Clay.getItemByAppKey('background').enable();
|
||||
} else {
|
||||
Clay.getItemByAppKey('background').disable();
|
||||
}
|
||||
}
|
||||
|
||||
Clay.on(Clay.EVENTS.AFTER_BUILD, function() {
|
||||
var coolStuffToggle = Clay.getItemByAppKey('cool_stuff');
|
||||
toggleBackground.call(coolStuffToggle);
|
||||
coolStuffToggle.on('change', toggleBackground);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Pebble Clay Development Page</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||
<link rel="stylesheet" href="../tmp/config-page.css">
|
||||
</head>
|
||||
<body>
|
||||
<form id="main-form" class="inputs"></form>
|
||||
<script src="../tmp/dev.js"></script>
|
||||
<script src="../tmp/config-page.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
window.returnTo = '#';
|
||||
window.clayConfig = require('./config.js');
|
||||
window.claySettings = {};
|
||||
window.customFn = require('./custom-fn.js');
|
||||
window.clayComponents = require('../src/scripts/components');
|
||||
|
||||
var platform = window.navigator.userAgent.match(/Android/) ? 'android' : 'ios';
|
||||
document.documentElement.classList.add('platform-' + platform);
|
||||
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Config Page Emulator</title>
|
||||
</head>
|
||||
<script>
|
||||
function getQueryParam(variable, defaultValue) {
|
||||
var query = location.search.substring(1);
|
||||
var vars = query.split('&');
|
||||
for (var i = 0; i < vars.length; i++) {
|
||||
var pair = vars[i].split('=');
|
||||
|
||||
if (pair[0] === variable) {
|
||||
return decodeURIComponent(pair[1]);
|
||||
}
|
||||
}
|
||||
return defaultValue || null;
|
||||
}
|
||||
|
||||
var returnTo = getQueryParam('return_to');
|
||||
var data = decodeURIComponent(location.hash.substring(1));
|
||||
if (data) {
|
||||
data = window.atob(data).replace('$$RETURN_TO$$', returnTo);
|
||||
window.location.href = 'data:text/html;base64,' + encodeURIComponent(window.btoa(data));
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Pebble Clay Development Page</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
if (!String.prototype.repeat) {
|
||||
String.prototype.repeat = function(count) {
|
||||
'use strict';
|
||||
if (this == null) {
|
||||
throw new TypeError('can\'t convert ' + this + ' to object');
|
||||
}
|
||||
var str = '' + this;
|
||||
count = +count;
|
||||
if (count != count) {
|
||||
count = 0;
|
||||
}
|
||||
if (count < 0) {
|
||||
throw new RangeError('repeat count must be non-negative');
|
||||
}
|
||||
if (count == Infinity) {
|
||||
throw new RangeError('repeat count must be less than infinity');
|
||||
}
|
||||
count = Math.floor(count);
|
||||
if (str.length == 0 || count == 0) {
|
||||
return '';
|
||||
}
|
||||
// Ensuring count is a 31-bit integer allows us to heavily optimize the
|
||||
// main part. But anyway, most current (August 2014) browsers can't handle
|
||||
// strings 1 << 28 chars or longer, so:
|
||||
if (str.length * count >= 1 << 28) {
|
||||
throw new RangeError('repeat count must not overflow maximum string size');
|
||||
}
|
||||
var rpt = '';
|
||||
for (;;) {
|
||||
if ((count & 1) == 1) {
|
||||
rpt += str;
|
||||
}
|
||||
count >>>= 1;
|
||||
if (count == 0) {
|
||||
break;
|
||||
}
|
||||
str += str;
|
||||
}
|
||||
return rpt;
|
||||
}
|
||||
}
|
||||
|
||||
var test = encodeURIComponent(window.btoa('<html><body>' + '1 '.repeat(389550 / 2) + '</body></html>'));
|
||||
alert(test.length);
|
||||
document.write('<iframe src="data:text/html;base64,' + test + '">');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+3
File diff suppressed because one or more lines are too long
Executable
+117
@@ -0,0 +1,117 @@
|
||||
'use strict';
|
||||
|
||||
var gulp = require('gulp');
|
||||
var browserify = require('browserify');
|
||||
var source = require('vinyl-source-stream');
|
||||
var buffer = require('vinyl-buffer');
|
||||
var stringify = require('stringify');
|
||||
var del = require('del');
|
||||
var inline = require('gulp-inline');
|
||||
var htmlmin = require('gulp-htmlmin');
|
||||
var sass = require('gulp-sass');
|
||||
var sourceMaps = require('gulp-sourcemaps');
|
||||
var autoprefixer = require('gulp-autoprefixer');
|
||||
var uglify = require('gulp-uglify');
|
||||
var sassify = require('sassify');
|
||||
var autoprefixify = require('./src/scripts/vendor/autoprefixify');
|
||||
var insert = require('gulp-insert');
|
||||
|
||||
var sassIncludePaths = [].concat(
|
||||
require('bourbon').includePaths,
|
||||
'src/styles'
|
||||
);
|
||||
|
||||
var sassifyOptions = {
|
||||
base64Encode: false,
|
||||
sourceMap: false,
|
||||
sourceMapEmbed: false,
|
||||
sourceMapContents: false,
|
||||
outputStyle: 'compact',
|
||||
includePaths: sassIncludePaths
|
||||
};
|
||||
|
||||
var autoprefixerOptions = {
|
||||
browsers: ['Android 4', 'iOS 8'],
|
||||
cascade: false
|
||||
};
|
||||
|
||||
var stringifyOptions = ['.html', '.tpl'];
|
||||
var versionMessage = '/* Clay - https://github.com/pebble/clay - Version: ' +
|
||||
require('./package.json').version +
|
||||
' - Build Date: ' + new Date().toISOString() + ' */\n';
|
||||
|
||||
gulp.task('clean-js', function() {
|
||||
return del(['tmp/config-page.js']);
|
||||
});
|
||||
|
||||
gulp.task('js', ['clean-js'], function() {
|
||||
return browserify('src/scripts/config-page.js', { debug: true })
|
||||
.transform('deamdify')
|
||||
.bundle()
|
||||
.pipe(source('config-page.js'))
|
||||
.pipe(gulp.dest('./tmp/'));
|
||||
});
|
||||
|
||||
gulp.task('clean-sass', function() {
|
||||
return del(['tmp/config-page.scss']);
|
||||
});
|
||||
gulp.task('sass', ['clean-sass'], function() {
|
||||
gulp.src('./src/styles/config-page.scss')
|
||||
.pipe(sourceMaps.init())
|
||||
.pipe(sass({
|
||||
includePaths: sassIncludePaths
|
||||
}).on('error', sass.logError))
|
||||
.pipe(autoprefixer(autoprefixerOptions))
|
||||
.pipe(sourceMaps.write('./'))
|
||||
.pipe(gulp.dest('tmp'));
|
||||
});
|
||||
|
||||
gulp.task('inlineHtml', ['js', 'sass'], function() {
|
||||
return gulp.src('src/config-page.html')
|
||||
.pipe(inline())
|
||||
.pipe(htmlmin({
|
||||
collapseWhitespace: true,
|
||||
removeComments: true,
|
||||
removeTagWhitespace: true,
|
||||
removeRedundantAttributes: true,
|
||||
caseSensitive: true,
|
||||
minifyJS: true,
|
||||
minifyCSS: true
|
||||
}))
|
||||
.pipe(gulp.dest('tmp/'));
|
||||
});
|
||||
|
||||
gulp.task('clay', ['inlineHtml'], function() {
|
||||
return browserify('index.js', {
|
||||
debug: false,
|
||||
standalone: 'clay'
|
||||
})
|
||||
.transform(stringify(stringifyOptions))
|
||||
.transform(sassify, sassifyOptions)
|
||||
.transform(autoprefixify, autoprefixerOptions)
|
||||
.require(require.resolve('./index'), {expose: 'pebble-clay'})
|
||||
.bundle()
|
||||
.pipe(source('clay.js'))
|
||||
.pipe(buffer())
|
||||
.pipe(uglify())
|
||||
.pipe(insert.prepend(versionMessage))
|
||||
.pipe(gulp.dest('./dist/'));
|
||||
});
|
||||
|
||||
gulp.task('dev-js', ['js', 'sass'], function() {
|
||||
return browserify('dev/dev.js', { debug: true })
|
||||
.transform(stringify(stringifyOptions))
|
||||
.transform('deamdify')
|
||||
.transform(sassify, sassifyOptions)
|
||||
.bundle()
|
||||
.pipe(source('dev.js'))
|
||||
.pipe(gulp.dest('./tmp/'));
|
||||
});
|
||||
|
||||
gulp.task('default', ['clay']);
|
||||
|
||||
gulp.task('dev', ['dev-js'], function() {
|
||||
gulp.watch('src/styles/**/*.scss', ['sass']);
|
||||
gulp.watch(['src/scripts/**/*.js', 'src/templates/**/*.tpl'], ['js']);
|
||||
gulp.watch(['src/**', 'dev/**/*.js'], ['dev-js']);
|
||||
});
|
||||
@@ -0,0 +1,198 @@
|
||||
'use strict';
|
||||
|
||||
var configPageHtml = require('./tmp/config-page.html');
|
||||
var toSource = require('tosource');
|
||||
var standardComponents = require('./src/scripts/components');
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {string} [prefix]
|
||||
* @private
|
||||
* @returns {string}
|
||||
*/
|
||||
function encodeDataUri(input, prefix) {
|
||||
prefix = typeof prefix !== 'undefined' ? prefix : 'data:text/html;base64,';
|
||||
|
||||
if (window.btoa) {
|
||||
return prefix + encodeURIComponent(window.btoa(input));
|
||||
}
|
||||
|
||||
// iOS doesn't have a window so we need to polyfil window.btoa
|
||||
// extracted from https://github.com/inexorabletash/polyfill
|
||||
var B64_ALPHABET =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
||||
input = String(input);
|
||||
var position = 0;
|
||||
var out = [];
|
||||
var o1;
|
||||
var o2;
|
||||
var o3;
|
||||
var e1;
|
||||
var e2;
|
||||
var e3;
|
||||
var e4;
|
||||
|
||||
if (/[^\x00-\xFF]/.test(input)) { throw Error('InvalidCharacterError'); }
|
||||
|
||||
while (position < input.length) {
|
||||
o1 = input.charCodeAt(position++);
|
||||
o2 = input.charCodeAt(position++);
|
||||
o3 = input.charCodeAt(position++);
|
||||
|
||||
// 111111 112222 222233 333333
|
||||
e1 = o1 >> 2;
|
||||
e2 = ((o1 & 0x3) << 4) | (o2 >> 4);
|
||||
e3 = ((o2 & 0xf) << 2) | (o3 >> 6);
|
||||
e4 = o3 & 0x3f;
|
||||
|
||||
if (position === input.length + 2) {
|
||||
e3 = 64;
|
||||
e4 = 64;
|
||||
} else if (position === input.length + 1) {
|
||||
e4 = 64;
|
||||
}
|
||||
|
||||
out.push(B64_ALPHABET.charAt(e1),
|
||||
B64_ALPHABET.charAt(e2),
|
||||
B64_ALPHABET.charAt(e3),
|
||||
B64_ALPHABET.charAt(e4));
|
||||
}
|
||||
|
||||
return prefix + encodeURIComponent(out.join(''));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array} config - the Clay config
|
||||
* @param {function} [customFn] - Custom code to run from the config page. Will run
|
||||
* with the ClayConfig instance as context
|
||||
* @param {Object} [options] - Additional options to pass to Clay
|
||||
* @param {boolean} [options.autoHandleEvents] - If false, Clay will not
|
||||
* automatically handle the 'showConfiguration' and 'webviewclosed' events
|
||||
* @constructor
|
||||
*/
|
||||
function Clay(config, customFn, options) {
|
||||
var self = this;
|
||||
|
||||
if (!Array.isArray(config)) {
|
||||
throw new Error('config must be an Array');
|
||||
}
|
||||
|
||||
if (customFn && typeof customFn !== 'function') {
|
||||
throw new Error('customFn must be an function or "null"');
|
||||
}
|
||||
|
||||
options = options || {};
|
||||
|
||||
self.config = config;
|
||||
self.customFn = customFn || function() {};
|
||||
self.components = [];
|
||||
|
||||
// Let Clay handle all the magic
|
||||
if (options.autoHandleEvents !== false && Pebble) {
|
||||
|
||||
Pebble.addEventListener('showConfiguration', function(e) {
|
||||
Pebble.openURL(self.generateUrl());
|
||||
});
|
||||
|
||||
Pebble.addEventListener('webviewclosed', function(e) {
|
||||
|
||||
if (e && !e.response) { return; }
|
||||
|
||||
// Send settings to Pebble watchapp
|
||||
Pebble.sendAppMessage(self.getSettings(e.response), function(e) {
|
||||
console.log('Sent config data to Pebble');
|
||||
}, function() {
|
||||
console.log('Failed to send config data!');
|
||||
console.log(JSON.stringify(e));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {Clay~ConfigItem|Array} item
|
||||
* @return {void}
|
||||
*/
|
||||
function _registerStandardComponents(item) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function(item) {
|
||||
_registerStandardComponents(item);
|
||||
});
|
||||
} else if (item.type === 'section') {
|
||||
_registerStandardComponents(item.items);
|
||||
} else if (standardComponents[item.type]) {
|
||||
self.registerComponent(standardComponents[item.type]);
|
||||
}
|
||||
}
|
||||
|
||||
_registerStandardComponents(self.config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a component to Clay.
|
||||
* @param {Object} component - the clay component to register
|
||||
* @param {string} component.name - the name of the component
|
||||
* @param {string} component.template - HTML template to use for the component
|
||||
* @param {string|Object} component.manipulator - methods to attach to the component
|
||||
* @param {function} component.manipulator.set - set manipulator method
|
||||
* @param {function} component.manipulator.get - get manipulator method
|
||||
* @param {Object} [component.defaults] - template defaults
|
||||
* @param {function} [component.initialize] - method to scaffold the component
|
||||
* @return {boolean} - Returns true if component was registered correctly
|
||||
*/
|
||||
Clay.prototype.registerComponent = function(component) {
|
||||
this.components.push(component);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate the Data URI used by the config Page with settings injected
|
||||
* @return {string}
|
||||
*/
|
||||
Clay.prototype.generateUrl = function() {
|
||||
var settings;
|
||||
var emulator = !Pebble || Pebble.platform === 'pypkjs';
|
||||
var returnTo = emulator ? '$$$RETURN_TO$$$' : 'pebblejs://close#';
|
||||
|
||||
try {
|
||||
settings = JSON.parse(localStorage.getItem('clay-settings')) || {};
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
settings = {};
|
||||
}
|
||||
|
||||
var compiledHtml = configPageHtml
|
||||
.replace('$$RETURN_TO$$', returnTo)
|
||||
.replace('$$CUSTOM_FN$$', toSource(this.customFn))
|
||||
.replace('$$CONFIG$$', toSource(this.config))
|
||||
.replace('$$SETTINGS$$', toSource(settings))
|
||||
.replace('$$COMPONENTS$$', toSource(this.components));
|
||||
|
||||
// if we are in the emulator then we need to proxy the data via a webpage to
|
||||
// obtain the return_to.
|
||||
// @todo calculate this from the Pebble object or something
|
||||
if (emulator) {
|
||||
return encodeDataUri(
|
||||
compiledHtml,
|
||||
'http://clay.pebble.com.s3-website-us-west-2.amazonaws.com/#'
|
||||
);
|
||||
}
|
||||
|
||||
return encodeDataUri(compiledHtml);
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse the response from the webviewclosed event data
|
||||
* @param {string} response
|
||||
* @returns {Object}
|
||||
*/
|
||||
Clay.prototype.getSettings = function(response) {
|
||||
// Decode and parse config data as JSON
|
||||
var settings = JSON.parse(decodeURIComponent(response));
|
||||
|
||||
if (!settings) return {};
|
||||
|
||||
localStorage.setItem('clay-settings', JSON.stringify(settings));
|
||||
return settings;
|
||||
};
|
||||
|
||||
module.exports = Clay;
|
||||
@@ -0,0 +1,72 @@
|
||||
{
|
||||
"name": "pebble-clay",
|
||||
"version": "0.1.0",
|
||||
"description": "Pebble Config Framework",
|
||||
"main": "dist/clay.js",
|
||||
"directories": {
|
||||
"example": "example"
|
||||
},
|
||||
"scripts": {
|
||||
"test-travis": "./node_modules/.bin/karma start ./test/karma.conf.js --single-run --browsers chromeTravisCI && ./node_modules/.bin/eslint ./",
|
||||
"test-debug": "(export DEBUG=true && ./node_modules/.bin/karma start ./test/karma.conf.js --no-single-run)",
|
||||
"test": "./node_modules/.bin/karma start ./test/karma.conf.js --single-run",
|
||||
"lint": "./node_modules/.bin/eslint ./",
|
||||
"build": "gulp",
|
||||
"dev": "gulp dev"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/pebble/clay.git"
|
||||
},
|
||||
"keywords": [
|
||||
"Pebble",
|
||||
"Config"
|
||||
],
|
||||
"author": "Pebble Technology",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pebble/clay/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pebble/clay#readme",
|
||||
"devDependencies": {
|
||||
"autoprefixer": "^6.3.1",
|
||||
"bourbon": "^4.2.6",
|
||||
"browserify": "^13.0.0",
|
||||
"browserify-istanbul": "^0.2.1",
|
||||
"chai": "^3.4.1",
|
||||
"deamdify": "^0.2.0",
|
||||
"del": "^2.0.2",
|
||||
"eslint": "^1.5.1",
|
||||
"eslint-config-pebble": "^1.2.0",
|
||||
"eslint-plugin-standard": "^1.3.1",
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-autoprefixer": "^3.1.0",
|
||||
"gulp-htmlmin": "^1.3.0",
|
||||
"gulp-inline": "0.0.15",
|
||||
"gulp-insert": "^0.5.0",
|
||||
"gulp-sass": "^2.1.1",
|
||||
"gulp-sourcemaps": "^1.6.0",
|
||||
"gulp-uglify": "^1.5.2",
|
||||
"joi": "^6.10.1",
|
||||
"karma": "^0.13.19",
|
||||
"karma-browserify": "^5.0.1",
|
||||
"karma-chrome-launcher": "^0.2.2",
|
||||
"karma-coverage": "^0.5.3",
|
||||
"karma-mocha": "^0.2.1",
|
||||
"karma-mocha-reporter": "^1.1.5",
|
||||
"karma-source-map-support": "^1.1.0",
|
||||
"karma-threshold-reporter": "^0.1.15",
|
||||
"mocha": "^2.3.4",
|
||||
"postcss": "^5.0.14",
|
||||
"require-from-string": "^1.1.0",
|
||||
"sassify": "^0.9.1",
|
||||
"sinon": "^1.17.3",
|
||||
"stringify": "^3.2.0",
|
||||
"through": "^2.3.8",
|
||||
"tosource": "^1.0.0",
|
||||
"vinyl-buffer": "^1.0.0",
|
||||
"vinyl-source-stream": "^1.1.0",
|
||||
"watchify": "^3.7.0"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="stylesheet" href="../tmp/config-page.css">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||
<script>
|
||||
window.returnTo = '$$RETURN_TO$$';
|
||||
window.clayConfig = $$CONFIG$$;
|
||||
window.claySettings = $$SETTINGS$$;
|
||||
window.customFn = $$CUSTOM_FN$$;
|
||||
window.clayComponents = $$COMPONENTS$$;
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<form id="main-form" class="inputs"></form>
|
||||
<script src="../tmp/config-page.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'checkboxgroup',
|
||||
template: require('../../templates/components/checkboxgroup.tpl'),
|
||||
style: require('../../styles/clay/components/checkboxgroup.scss'),
|
||||
manipulator: 'checkboxgroup',
|
||||
defaults: {
|
||||
label: '',
|
||||
options: [],
|
||||
attributes: {}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,194 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'color',
|
||||
template: require('../../templates/components/color.tpl'),
|
||||
style: require('../../styles/clay/components/color.scss'),
|
||||
manipulator: 'color',
|
||||
defaults: {
|
||||
label: ''
|
||||
},
|
||||
initialize: function(minified) {
|
||||
var HTML = minified.HTML;
|
||||
var self = this;
|
||||
var useSunlight = self.config.sunlight !== false;
|
||||
var sunlightColorMap = {
|
||||
'000000': '000000',
|
||||
'000055': '001e41',
|
||||
'0000aa': '004387',
|
||||
'0000ff': '0068ca',
|
||||
'005500': '2b4a2c',
|
||||
'005555': '27514f',
|
||||
'0055aa': '16638d',
|
||||
'0055ff': '007dce',
|
||||
'00aa00': '5e9860',
|
||||
'00aa55': '5c9b72',
|
||||
'00aaaa': '57a5a2',
|
||||
'00aaff': '4cb4db',
|
||||
'00ff00': '8ee391',
|
||||
'00ff55': '8ee69e',
|
||||
'00ffaa': '8aebc0',
|
||||
'00ffff': '84f5f1',
|
||||
'550000': '4a161b',
|
||||
'550055': '482748',
|
||||
'5500aa': '40488a',
|
||||
'5500ff': '2f6bcc',
|
||||
'555500': '564e36',
|
||||
'555555': '545454',
|
||||
'5555aa': '4f6790',
|
||||
'5555ff': '4180d0',
|
||||
'55aa00': '759a64',
|
||||
'55aa55': '759d76',
|
||||
'55aaaa': '71a6a4',
|
||||
'55aaff': '69b5dd',
|
||||
'55ff00': '9ee594',
|
||||
'55ff55': '9de7a0',
|
||||
'55ffaa': '9becc2',
|
||||
'55ffff': '95f6f2',
|
||||
'aa0000': '99353f',
|
||||
'aa0055': '983e5a',
|
||||
'aa00aa': '955694',
|
||||
'aa00ff': '8f74d2',
|
||||
'aa5500': '9d5b4d',
|
||||
'aa5555': '9d6064',
|
||||
'aa55aa': '9a7099',
|
||||
'aa55ff': '9587d5',
|
||||
'aaaa00': 'afa072',
|
||||
'aaaa55': 'aea382',
|
||||
'aaaaaa': 'ababab',
|
||||
'ffffff': 'ffffff',
|
||||
'aaaaff': 'a7bae2',
|
||||
'aaff00': 'c9e89d',
|
||||
'aaff55': 'c9eaa7',
|
||||
'aaffaa': 'c7f0c8',
|
||||
'aaffff': 'c3f9f7',
|
||||
'ff0000': 'e35462',
|
||||
'ff0055': 'e25874',
|
||||
'ff00aa': 'e16aa3',
|
||||
'ff00ff': 'de83dc',
|
||||
'ff5500': 'e66e6b',
|
||||
'ff5555': 'e6727c',
|
||||
'ff55aa': 'e37fa7',
|
||||
'ff55ff': 'e194df',
|
||||
'ffaa00': 'f1aa86',
|
||||
'ffaa55': 'f1ad93',
|
||||
'ffaaaa': 'efb5b8',
|
||||
'ffaaff': 'ecc3eb',
|
||||
'ffff00': 'ffeeab',
|
||||
'ffff55': 'fff1b5',
|
||||
'ffffaa': 'fff6d3'
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string|boolean|number} color
|
||||
* @returns {string}
|
||||
*/
|
||||
function cssColor(color) {
|
||||
if (color === false) { return 'transparent'; }
|
||||
if (typeof color === 'number') {
|
||||
color = color.toString(16);
|
||||
}
|
||||
while (color.length < 6) {
|
||||
color = '0' + color;
|
||||
}
|
||||
|
||||
return '#' + (useSunlight ? sunlightColorMap[color] : color);
|
||||
}
|
||||
|
||||
/* eslint-disable comma-spacing, no-multi-spaces, max-len,
|
||||
standard/array-bracket-even-spacing */
|
||||
var layout = self.config.layout || [
|
||||
[false , false , '55ff00', 'aaff55', false , 'ffff55', 'ffffaa', false , false ],
|
||||
[false , 'aaffaa', '55ff55', '00ff00', 'aaff00', 'ffff00', 'ffaa55', 'ffaaaa', false ],
|
||||
['55ffaa', '00ff55', '00aa00', '55aa00', 'aaaa55', 'aaaa00', 'ffaa00', 'ff5500', 'ff5555'],
|
||||
['aaffff', '00ffaa', '00aa55', '55aa55', '005500', '555500', 'aa5500', 'ff0000', 'ff0055'],
|
||||
[false , '55aaaa', '00aaaa', '005555', 'ffffff', '000000', 'aa5555', 'aa0000', false ],
|
||||
['55ffff', '00ffff', '00aaff', '0055aa', 'aaaaaa', '555555', '550000', 'aa0055', 'ff55aa'],
|
||||
['55aaff', '0055ff', '0000ff', '0000aa', '000055', '550055', 'aa00aa', 'ff00aa', 'ffaaff'],
|
||||
[false , '5555aa', '5555ff', '5500ff', '5500aa', 'aa00ff', 'ff00ff', 'ff55ff', false ],
|
||||
[false , false , false , 'aaaaff', 'aa55ff', 'aa55aa', false , false , false ]
|
||||
];
|
||||
/* eslint-enable */
|
||||
|
||||
var grid = '';
|
||||
var itemWidth = 100 / layout[0].length;
|
||||
var itemHeight = 100 / layout.length;
|
||||
var $elem = self.$element;
|
||||
|
||||
for (var i = 0; i < layout.length; i++) {
|
||||
for (var j = 0; j < layout[i].length; j++) {
|
||||
|
||||
var color = layout[i][j];
|
||||
var selectable = (color ? ' selectable' : '');
|
||||
|
||||
var roundedTL = (i === 0 && j === 0) || i === 0 && !layout[i][j - 1] ||
|
||||
!layout[i][j - 1] && !layout[i - 1][j] ?
|
||||
' rounded-tl' :
|
||||
'';
|
||||
|
||||
var roundedTR = i === 0 && !layout[i][j + 1] ||
|
||||
!layout[i][j + 1] && !layout[i - 1][j] ?
|
||||
' rounded-tr ' :
|
||||
'';
|
||||
|
||||
var roundedBL = (i === layout.length - 1 && j === 0) ||
|
||||
i === layout.length - 1 && !layout[i][j - 1] ||
|
||||
!layout[i][j - 1] && !layout[i + 1][j] ?
|
||||
' rounded-bl' :
|
||||
'';
|
||||
|
||||
var roundedBR = i === layout.length - 1 && !layout[i][j + 1] ||
|
||||
!layout[i][j + 1] && !layout[i + 1][j] ?
|
||||
' rounded-br' :
|
||||
'';
|
||||
|
||||
grid += '<i ' +
|
||||
'class="color-box ' + selectable + roundedTL + roundedTR + roundedBL +
|
||||
roundedBR + '" ' +
|
||||
'data-value="' + parseInt(color, 16) + '" ' +
|
||||
'style="' +
|
||||
'width:' + itemWidth + '%; ' +
|
||||
'height:' + itemHeight + '%; ' +
|
||||
'background:' + cssColor(color) + ';">' +
|
||||
'</i>';
|
||||
}
|
||||
}
|
||||
|
||||
$elem.select('.color-box-container').add(HTML(grid));
|
||||
|
||||
var $valueDisplay = $elem.select('.value');
|
||||
var $picker = $elem.select('.picker-wrap');
|
||||
var disabled = self.$manipulatorTarget.get('disabled');
|
||||
|
||||
$elem.select('label').on('click', function(ev) {
|
||||
if (!disabled) {
|
||||
$picker.set('show'); // toggle visibility
|
||||
}
|
||||
});
|
||||
|
||||
self.on('change', function() {
|
||||
var value = self.get();
|
||||
$valueDisplay.set('$background-color', cssColor(value));
|
||||
$elem.select('.color-box').set('-selected');
|
||||
$elem.select('.color-box[data-value="' + value + '"]').set('+selected');
|
||||
});
|
||||
|
||||
$elem.select('.color-box.selectable').on('click', function(ev) {
|
||||
self.set(parseInt(ev.target.dataset.value, 10));
|
||||
$picker.set('-show');
|
||||
});
|
||||
|
||||
$picker.on('click', function() {
|
||||
$picker.set('-show');
|
||||
});
|
||||
|
||||
self.on('disabled', function() {
|
||||
disabled = true;
|
||||
});
|
||||
|
||||
self.on('enabled', function() {
|
||||
disabled = false;
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'footer',
|
||||
template: require('../../templates/components/footer.tpl'),
|
||||
manipulator: 'html'
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'heading',
|
||||
template: require('../../templates/components/heading.tpl'),
|
||||
manipulator: 'html',
|
||||
defaults: {
|
||||
size: 4
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
color: require('./color'),
|
||||
footer: require('./footer'),
|
||||
heading: require('./heading'),
|
||||
input: require('./input'),
|
||||
select: require('./select'),
|
||||
submit: require('./submit'),
|
||||
text: require('./text'),
|
||||
toggle: require('./toggle'),
|
||||
radiogroup: require('./radiogroup'),
|
||||
checkboxgroup: require('./checkboxgroup')
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'input',
|
||||
template: require('../../templates/components/input.tpl'),
|
||||
style: require('../../styles/clay/components/input.scss'),
|
||||
manipulator: 'val',
|
||||
defaults: {
|
||||
label: '',
|
||||
attributes: {}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'radiogroup',
|
||||
template: require('../../templates/components/radiogroup.tpl'),
|
||||
style: require('../../styles/clay/components/radiogroup.scss'),
|
||||
manipulator: 'radiogroup',
|
||||
defaults: {
|
||||
label: '',
|
||||
options: [],
|
||||
attributes: {}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'select',
|
||||
template: require('../../templates/components/select.tpl'),
|
||||
style: require('../../styles/clay/components/select.scss'),
|
||||
manipulator: 'val',
|
||||
defaults: {
|
||||
label: '',
|
||||
options: [],
|
||||
attributes: {}
|
||||
},
|
||||
initialize: function() {
|
||||
var self = this;
|
||||
|
||||
var $value = self.$element.select('.value');
|
||||
|
||||
self.on('change', function(ev) {
|
||||
var value = self.$manipulatorTarget.select('option:checked').get('innerHTML');
|
||||
$value.set('innerHTML', value);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'submit',
|
||||
template: require('../../templates/components/submit.tpl'),
|
||||
manipulator: 'html',
|
||||
defaults: {
|
||||
attributes: {}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'text',
|
||||
template: require('../../templates/components/text.tpl'),
|
||||
manipulator: 'html'
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
name: 'toggle',
|
||||
template: require('../../templates/components/toggle.tpl'),
|
||||
style: require('../../styles/clay/components/toggle.scss'),
|
||||
manipulator: 'checked',
|
||||
defaults: {
|
||||
label: '',
|
||||
attributes: {}
|
||||
}
|
||||
};
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
var minified = require('./vendor/minified');
|
||||
var ClayConfig = require('./lib/clay-config');
|
||||
|
||||
var $ = minified.$;
|
||||
var _ = minified._;
|
||||
|
||||
var config = _.extend([], window.clayConfig || []);
|
||||
var settings = _.extend({}, window.claySettings || {});
|
||||
var returnTo = window.returnTo || 'pebblejs://close#';
|
||||
var customFn = window.customFn || function() {};
|
||||
var clayComponents = window.clayComponents || {};
|
||||
|
||||
var platform = window.navigator.userAgent.match(/android/i) ? 'android' : 'ios';
|
||||
document.documentElement.classList.add('platform-' + platform);
|
||||
|
||||
// Register the passed components
|
||||
_.eachObj(clayComponents, function(key, component) {
|
||||
ClayConfig.registerComponent(component);
|
||||
});
|
||||
|
||||
var $mainForm = $('#main-form');
|
||||
var clayConfig = new ClayConfig(settings, config, $mainForm);
|
||||
|
||||
// add listeners here
|
||||
$mainForm.on('submit', function() {
|
||||
// Set the return URL depending on the runtime environment
|
||||
location.href = returnTo +
|
||||
encodeURIComponent(JSON.stringify(clayConfig.getSettings()));
|
||||
});
|
||||
|
||||
// Run the custom function in the context of the ClayConfig
|
||||
customFn.call(clayConfig, minified);
|
||||
|
||||
// Now that we have given the dev's custom code to run and attach listeners,
|
||||
// we build the config
|
||||
clayConfig.build();
|
||||
@@ -0,0 +1,240 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* A Clay config Item
|
||||
* @typedef {Object} Clay~ConfigItem
|
||||
* @property {string} type
|
||||
* @property {string|boolean|number} defaultValue
|
||||
* @property {string} [appKey]
|
||||
* @property {string} [id]
|
||||
* @property {string} [label]
|
||||
* @property {Object} [attributes]
|
||||
* @property {Array} [options]
|
||||
* @property {Array} [items]
|
||||
*/
|
||||
|
||||
var HTML = require('../vendor/minified').HTML;
|
||||
var _ = require('../vendor/minified')._;
|
||||
var ClayItem = require('./clay-item');
|
||||
var utils = require('../lib/utils');
|
||||
var ClayEvents = require('./clay-events');
|
||||
var componentStore = require('./component-registry');
|
||||
var manipulators = require('./manipulators');
|
||||
|
||||
/**
|
||||
* @extends ClayEvents
|
||||
* @param {Object} settings - setting that were set from a previous session
|
||||
* @param {Array|Object} config
|
||||
* @param {M} $rootContainer
|
||||
* @constructor
|
||||
*/
|
||||
function ClayConfig(settings, config, $rootContainer) {
|
||||
var self = this;
|
||||
|
||||
var _settings = _.copyObj(settings);
|
||||
var _items = [];
|
||||
var _itemsById = {};
|
||||
var _itemsByAppKey = {};
|
||||
var _isBuilt = false;
|
||||
|
||||
/**
|
||||
* Add item(s) to the config
|
||||
* @param {Clay~ConfigItem|Array} item
|
||||
* @param {M} $container
|
||||
* @return {void}
|
||||
*/
|
||||
function _addItems(item, $container) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function(item) {
|
||||
_addItems(item, $container);
|
||||
});
|
||||
} else if (item.type === 'section') {
|
||||
var $wrapper = HTML('<div class="section">');
|
||||
$container.add($wrapper);
|
||||
_addItems(item.items, $wrapper);
|
||||
} else {
|
||||
var _item = _.copyObj(item);
|
||||
_item.clayId = _items.length;
|
||||
|
||||
var clayItem = new ClayItem(_item).initialize(self);
|
||||
|
||||
if (_item.id) {
|
||||
_itemsById[_item.id] = clayItem;
|
||||
}
|
||||
|
||||
if (_item.appKey) {
|
||||
_itemsByAppKey[_item.appKey] = clayItem;
|
||||
}
|
||||
|
||||
_items.push(clayItem);
|
||||
|
||||
// set the value of the item via the manipulator to ensure consistency
|
||||
var value = typeof _settings[_item.appKey] !== 'undefined' ?
|
||||
_settings[_item.appKey] :
|
||||
(_item.defaultValue || '');
|
||||
|
||||
clayItem.set(value);
|
||||
|
||||
$container.add(clayItem.$element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws if the config has not been built yet.
|
||||
* @param {string} fnName
|
||||
* @returns {boolean}
|
||||
* @private
|
||||
*/
|
||||
function _checkBuilt(fnName) {
|
||||
if (!_isBuilt) {
|
||||
throw new Error(
|
||||
'ClayConfig not built. build() must be run before ' +
|
||||
'you can run ' + fnName + '()'
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
self.EVENTS = {
|
||||
/**
|
||||
* Called before framework has initialized. This is when you would attach your
|
||||
* custom components.
|
||||
* @const
|
||||
*/
|
||||
BEFORE_BUILD: 'BEFORE_BUILD',
|
||||
|
||||
/**
|
||||
* Called after the config has been parsed and all components have their initial
|
||||
* value set
|
||||
* @const
|
||||
*/
|
||||
AFTER_BUILD: 'AFTER_BUILD'
|
||||
};
|
||||
utils.updateProperties(self.EVENTS, {writable: false});
|
||||
|
||||
/**
|
||||
* @returns {Array.<ClayItem>}
|
||||
*/
|
||||
self.getAllItems = function() {
|
||||
_checkBuilt('getAllItems');
|
||||
return _items;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} appKey
|
||||
* @returns {ClayItem}
|
||||
*/
|
||||
self.getItemByAppKey = function(appKey) {
|
||||
_checkBuilt('getItemByAppKey');
|
||||
return _itemsByAppKey[appKey];
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} id
|
||||
* @returns {ClayItem}
|
||||
*/
|
||||
self.getItemById = function(id) {
|
||||
_checkBuilt('getItemById');
|
||||
return _itemsById[id];
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} type
|
||||
* @returns {Array.<ClayItem>}
|
||||
*/
|
||||
self.getItemsByType = function(type) {
|
||||
_checkBuilt('getItemsByType');
|
||||
return _items.filter(function(item) {
|
||||
return item.config.type === type;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {Object}
|
||||
*/
|
||||
self.getSettings = function() {
|
||||
_checkBuilt('getSettings');
|
||||
_.eachObj(_itemsByAppKey, function(appKey, item) {
|
||||
_settings[appKey] = item.get();
|
||||
});
|
||||
return _settings;
|
||||
};
|
||||
|
||||
// @todo maybe don't do this and force the static method
|
||||
self.registerComponent = ClayConfig.registerComponent;
|
||||
|
||||
/**
|
||||
* Build the config page. This must be run before any of the get methods can be run
|
||||
* @returns {ClayConfig}
|
||||
*/
|
||||
self.build = function() {
|
||||
self.trigger(self.EVENTS.BEFORE_BUILD);
|
||||
_addItems(config, $rootContainer);
|
||||
_isBuilt = true;
|
||||
self.trigger(self.EVENTS.AFTER_BUILD);
|
||||
return self;
|
||||
};
|
||||
|
||||
// attach event methods
|
||||
ClayEvents.call(self, $rootContainer);
|
||||
|
||||
// prevent external modifications of properties
|
||||
utils.updateProperties(self, { writable: false, configurable: false });
|
||||
|
||||
// expose the config to allow developers to update it before the build is run
|
||||
self.config = config;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a component to Clay. This must be called prior to .build();
|
||||
* @param {Object} component - the clay component to register
|
||||
* @param {string} component.name - the name of the component
|
||||
* @param {string} component.template - HTML template to use for the component
|
||||
* @param {string|Object} component.manipulator - methods to attach to the component
|
||||
* @param {function} component.manipulator.set - set manipulator method
|
||||
* @param {function} component.manipulator.get - get manipulator method
|
||||
* @param {Object} [component.defaults] - template defaults
|
||||
* @param {function} [component.initialize] - method to scaffold the component
|
||||
* @return {boolean} - Returns true if component was registered correctly
|
||||
*/
|
||||
ClayConfig.registerComponent = function(component) {
|
||||
var _component = _.copyObj(component);
|
||||
|
||||
if (componentStore[_component.name]) {
|
||||
console.warn('Component: ' + _component.name +
|
||||
' is already registered. If you wish to override the existing' +
|
||||
' functionality, you must provide a new name');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof _component.manipulator === 'string') {
|
||||
_component.manipulator = manipulators[component.manipulator];
|
||||
|
||||
if (!_component.manipulator) {
|
||||
throw new Error('The manipulator: ' + component.manipulator +
|
||||
' does not exist in the built-in manipulators.');
|
||||
}
|
||||
}
|
||||
|
||||
if (!_component.manipulator) {
|
||||
throw new Error('The manipulator must be defined');
|
||||
}
|
||||
|
||||
if (typeof _component.manipulator.set !== 'function' ||
|
||||
typeof _component.manipulator.get !== 'function') {
|
||||
throw new Error('The manipulator must have both a `get` and `set` method');
|
||||
}
|
||||
|
||||
if (_component.style) {
|
||||
var style = document.createElement('style');
|
||||
style.type = 'text/css';
|
||||
style.appendChild(document.createTextNode(_component.style));
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
|
||||
componentStore[_component.name] = _component;
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = ClayConfig;
|
||||
@@ -0,0 +1,101 @@
|
||||
'use strict';
|
||||
|
||||
var $ = require('../vendor/minified').$;
|
||||
var _ = require('../vendor/minified')._;
|
||||
|
||||
/**
|
||||
* Attaches event methods to the context.
|
||||
* Call with ClayEvents.call(yourObject, $eventTarget)
|
||||
* @param {EventEmitter|M} $eventTarget - An object that will be used as the event
|
||||
* target. Must implement EventEmitter
|
||||
* @constructor
|
||||
*/
|
||||
function ClayEvents($eventTarget) {
|
||||
var self = this;
|
||||
var _eventProxies = [];
|
||||
|
||||
/**
|
||||
* prefixes events with "|"
|
||||
* @param {string} events
|
||||
* @returns {string}
|
||||
* @private
|
||||
*/
|
||||
function _transformEventNames(events) {
|
||||
return events.split(' ').map(function(event) {
|
||||
return '|' + event.replace(/^\|/, '');
|
||||
}).join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function} handler
|
||||
* @param {function} proxy
|
||||
* @returns {function}
|
||||
* @private
|
||||
*/
|
||||
function _registerEventProxy(handler, proxy) {
|
||||
var eventProxy = _.find(_eventProxies, function(item) {
|
||||
return item.handler === handler ? item : null;
|
||||
});
|
||||
|
||||
if (!eventProxy) {
|
||||
eventProxy = { handler: handler, proxy: proxy };
|
||||
_eventProxies.push(eventProxy);
|
||||
}
|
||||
return eventProxy.proxy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function} handler
|
||||
* @returns {function}
|
||||
* @private
|
||||
*/
|
||||
function _getEventProxy(handler) {
|
||||
return _.find(_eventProxies, function(item) {
|
||||
return item.handler === handler ? item.proxy : null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach an event listener to the item.
|
||||
* @param {string} events - a space separated list of events
|
||||
* @param {function} handler
|
||||
* @returns {ClayEvents}
|
||||
*/
|
||||
self.on = function(events, handler) {
|
||||
var _events = _transformEventNames(events);
|
||||
var self = this;
|
||||
var _proxy = _registerEventProxy(handler, function() {
|
||||
handler.apply(self, arguments);
|
||||
});
|
||||
$eventTarget.on(_events, _proxy);
|
||||
return self;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove the given event handler. NOTE: This will remove the handler from all
|
||||
* registered events
|
||||
* @param {function} handler
|
||||
* @returns {ClayEvents}
|
||||
*/
|
||||
self.off = function(handler) {
|
||||
var _proxy = _getEventProxy(handler);
|
||||
if (_proxy) {
|
||||
$.off(_proxy);
|
||||
}
|
||||
return self;
|
||||
};
|
||||
|
||||
/**
|
||||
* Trigger an event.
|
||||
* @param {string} name - a single event name to trigger
|
||||
* @param {Object} [eventObj] - an object to pass to the event handler,
|
||||
* provided the handler does not have custom arguments.
|
||||
* @returns {ClayEvents}
|
||||
*/
|
||||
self.trigger = function(name, eventObj) {
|
||||
$eventTarget.trigger(name, eventObj);
|
||||
return self;
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = ClayEvents;
|
||||
@@ -0,0 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
var componentRegistry = require('./component-registry');
|
||||
var minified = require('../vendor/minified');
|
||||
var utils = require('../lib/utils');
|
||||
var ClayEvents = require('./clay-events');
|
||||
|
||||
var _ = minified._;
|
||||
var HTML = minified.HTML;
|
||||
|
||||
/**
|
||||
* @extends ClayEvents
|
||||
* @param {Clay~ConfigItem} config
|
||||
* @constructor
|
||||
*/
|
||||
function ClayItem(config) {
|
||||
var self = this;
|
||||
|
||||
var _component = componentRegistry[config.type];
|
||||
|
||||
if (!_component) {
|
||||
throw new Error('The component: ' + config.type + ' is not registered. ' +
|
||||
'Make sure to register it with ClayConfig.registerComponent()');
|
||||
}
|
||||
|
||||
var _templateData = _.extend({}, _component.defaults || {}, config);
|
||||
|
||||
/** @type {string|null} */
|
||||
self.id = config.id || null;
|
||||
|
||||
/** @type {string|null} */
|
||||
self.appKey = config.appKey || null;
|
||||
|
||||
/** @type {Object} */
|
||||
self.config = config;
|
||||
|
||||
/** @type {M} */
|
||||
self.$element = HTML(_component.template.trim(), _templateData);
|
||||
|
||||
/** @type {M} */
|
||||
self.$manipulatorTarget = self.$element.select('[data-manipulator-target]');
|
||||
|
||||
// this caters for situations where the manipulator target is the root element
|
||||
if (!self.$manipulatorTarget.length) {
|
||||
self.$manipulatorTarget = self.$element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the initializer if it exists and attaches the css to the head.
|
||||
* Passes minified as the first param
|
||||
* @param {ClayConfig} clay
|
||||
* @returns {ClayItem}
|
||||
*/
|
||||
self.initialize = function(clay) {
|
||||
if (typeof _component.initialize === 'function') {
|
||||
_component.initialize.call(self, minified, clay);
|
||||
}
|
||||
return self;
|
||||
};
|
||||
|
||||
// attach event methods
|
||||
ClayEvents.call(self, self.$element);
|
||||
|
||||
// attach the manipulator methods to the clayItem
|
||||
_.eachObj(_component.manipulator, function(methodName, method) {
|
||||
self[methodName] = method.bind(self);
|
||||
});
|
||||
|
||||
// prevent external modifications of properties
|
||||
utils.updateProperties(self, { writable: false, configurable: false });
|
||||
}
|
||||
|
||||
module.exports = ClayItem;
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
'use strict';
|
||||
|
||||
// module is blank because we dynamically add components
|
||||
module.exports = {};
|
||||
Executable
+104
@@ -0,0 +1,104 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @returns {ClayEvents}
|
||||
*/
|
||||
function disable() {
|
||||
this.$element.set('+disabled');
|
||||
this.$manipulatorTarget.set('disabled', true);
|
||||
return this.trigger('disabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {ClayEvents}
|
||||
*/
|
||||
function enable() {
|
||||
this.$element.set('-disabled');
|
||||
this.$manipulatorTarget.set('disabled', false);
|
||||
return this.trigger('enabled');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
html: {
|
||||
get: function() {
|
||||
return this.$manipulatorTarget.get('innerHTML');
|
||||
},
|
||||
set: function(value) {
|
||||
this.$manipulatorTarget.set('innerHTML', value);
|
||||
return this.trigger('change');
|
||||
}
|
||||
},
|
||||
val: {
|
||||
get: function() {
|
||||
return this.$manipulatorTarget.get('value');
|
||||
},
|
||||
set: function(value) {
|
||||
this.$manipulatorTarget.set('value', value);
|
||||
return this.trigger('change');
|
||||
},
|
||||
disable: disable,
|
||||
enable: enable
|
||||
},
|
||||
checked: {
|
||||
get: function() {
|
||||
return this.$manipulatorTarget.get('checked');
|
||||
},
|
||||
set: function(value) {
|
||||
this.$manipulatorTarget.set('checked', value);
|
||||
return this.trigger('change');
|
||||
},
|
||||
disable: disable,
|
||||
enable: enable
|
||||
},
|
||||
radiogroup: {
|
||||
get: function() {
|
||||
return this.$element.select('input:checked').get('value');
|
||||
},
|
||||
set: function(value) {
|
||||
this.$element
|
||||
.select('input[value="' + value.replace('"', '\\"') + '"]')
|
||||
.set('checked', true);
|
||||
return this.trigger('change');
|
||||
},
|
||||
disable: disable,
|
||||
enable: enable
|
||||
},
|
||||
checkboxgroup: {
|
||||
get: function() {
|
||||
var result = [];
|
||||
this.$element.select('input:checked').each(function(item) {
|
||||
result.push(item.value);
|
||||
});
|
||||
return result;
|
||||
},
|
||||
set: function(values) {
|
||||
var self = this;
|
||||
self.$element.select('input').set('checked', false);
|
||||
values = values || [];
|
||||
values.map(function(value) {
|
||||
self.$element
|
||||
.select('input[value="' + value.replace('"', '\\"') + '"]')
|
||||
.set('checked', true);
|
||||
});
|
||||
return self.trigger('change');
|
||||
},
|
||||
disable: disable,
|
||||
enable: enable
|
||||
},
|
||||
color: {
|
||||
get: function() {
|
||||
return parseInt(this.$manipulatorTarget.get('value'), 16);
|
||||
},
|
||||
set: function(value) {
|
||||
switch (typeof value) {
|
||||
case 'number': value = value.toString(16); break;
|
||||
case 'string': value = value.replace(/^#|^0x/, ''); break;
|
||||
}
|
||||
|
||||
this.$manipulatorTarget.set('value', value || '000000');
|
||||
return this.trigger('change');
|
||||
},
|
||||
disable: disable,
|
||||
enable: enable
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Batch update all the properties of an object.
|
||||
* @param {Object} obj
|
||||
* @param {Object} descriptor
|
||||
* @param {boolean} [descriptor.configurable]
|
||||
* @param {boolean} [descriptor.enumerable]
|
||||
* @param {*} [descriptor.value]
|
||||
* @param {boolean} [descriptor.writable]
|
||||
* @param {function} [descriptor.get]
|
||||
* @param {function} [descriptor.set]
|
||||
* @return {void}
|
||||
*/
|
||||
module.exports.updateProperties = function(obj, descriptor) {
|
||||
Object.getOwnPropertyNames(obj).forEach(function(prop) {
|
||||
Object.defineProperty(obj, prop, descriptor);
|
||||
});
|
||||
};
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
var path = require('path');
|
||||
var through = require('through');
|
||||
var postcss = require('postcss');
|
||||
var autoprefixer = require('autoprefixer');
|
||||
var requireFromString = require('require-from-string');
|
||||
|
||||
/**
|
||||
* Stringifies the content
|
||||
* @param {string} content
|
||||
* @returns {string}
|
||||
*/
|
||||
function stringify (content) {
|
||||
return 'module.exports = ' + JSON.stringify(content) + ';\n';
|
||||
}
|
||||
|
||||
module.exports = function (file, options) {
|
||||
|
||||
/**
|
||||
* The function Browserify will use to transform the input.
|
||||
* @param {string} file
|
||||
* @returns {stream}
|
||||
*/
|
||||
function browserifyTransform (file) {
|
||||
var extensions = ['.css', '.sass', '.scss', '.less'];
|
||||
var chunks = [];
|
||||
|
||||
if (extensions.indexOf(path.extname(file)) === -1) {
|
||||
return through();
|
||||
}
|
||||
|
||||
var write = function (buffer) {
|
||||
chunks.push(buffer);
|
||||
};
|
||||
|
||||
var end = function () {
|
||||
var contents = requireFromString(Buffer.concat(chunks).toString('utf8'));
|
||||
contents = postcss([autoprefixer(options)]).process(contents).css;
|
||||
this.queue(stringify(contents));
|
||||
this.queue(null);
|
||||
};
|
||||
|
||||
return through(write, end);
|
||||
}
|
||||
|
||||
if (typeof file !== 'string') {
|
||||
options = file;
|
||||
return browserifyTransform;
|
||||
} else {
|
||||
return browserifyTransform(file);
|
||||
}
|
||||
};
|
||||
Vendored
+3552
File diff suppressed because it is too large
Load Diff
Executable
+2
@@ -0,0 +1,2 @@
|
||||
@import "clay/vars";
|
||||
@import "clay/mixins";
|
||||
@@ -0,0 +1,198 @@
|
||||
@import "bourbon";
|
||||
@import "clay";
|
||||
|
||||
html, body {
|
||||
@include font-pfdin(regular);
|
||||
-webkit-font-smoothing: antialiased;
|
||||
font-size: $em-base;
|
||||
line-height: $base-line-height;
|
||||
height:100%;
|
||||
color: $color-white;
|
||||
|
||||
&.platform-ios {
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: $color-gray-2;
|
||||
padding: $item-spacing-v $item-spacing-h;
|
||||
}
|
||||
|
||||
h1, h2, h3, h4 {
|
||||
text-transform: uppercase;
|
||||
@include font-pfdin(medium);
|
||||
@include pfdin-uppercase();
|
||||
}
|
||||
|
||||
h1 {
|
||||
@include font-size(2);
|
||||
}
|
||||
|
||||
h2 {
|
||||
@include font-size(1.8);
|
||||
}
|
||||
|
||||
h3 {
|
||||
@include font-size(1.5);
|
||||
}
|
||||
|
||||
h4 {
|
||||
@include font-size(1.2);
|
||||
}
|
||||
|
||||
h5 {
|
||||
@include font-size(1);
|
||||
}
|
||||
|
||||
h6 {
|
||||
@include font-size(0.8);
|
||||
}
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
|
||||
.input {
|
||||
white-space: nowrap;
|
||||
display: flex;
|
||||
max-width: 50%;
|
||||
margin-left: $item-spacing-h;
|
||||
}
|
||||
|
||||
&.invalid .input:after {
|
||||
$size: 1.1rem;
|
||||
|
||||
content: "!";
|
||||
display: inline-block;
|
||||
color: $color-white;
|
||||
background: $color-orange;
|
||||
border-radius: $size / 2;
|
||||
width: $size;
|
||||
text-align: center;
|
||||
height: $size;
|
||||
font-size: $size * 0.75;
|
||||
vertical-align: middle;
|
||||
line-height: $size;
|
||||
box-shadow: $box-shadow-small-components;
|
||||
@include font-pfdin(medium);
|
||||
flex: 0 0 $size;
|
||||
margin-left: 0.3rem;
|
||||
}
|
||||
}
|
||||
|
||||
.component {
|
||||
padding-bottom: $item-spacing-v;
|
||||
@include tap-highlight();
|
||||
|
||||
&.disabled {
|
||||
opacity: 0.25;
|
||||
pointer-events: none;
|
||||
}
|
||||
}
|
||||
|
||||
.section {
|
||||
background: $color-gray-4;
|
||||
border-radius: 0.25rem;
|
||||
margin-bottom: 1rem;
|
||||
box-shadow: $color-gray-1 0 0.15rem 0.25rem;
|
||||
|
||||
.component {
|
||||
padding-top: $item-spacing-v;
|
||||
padding-right: $item-spacing-h;
|
||||
padding-left: $item-spacing-h;
|
||||
position: relative;
|
||||
|
||||
&:last-child,
|
||||
&:first-child {
|
||||
padding-bottom: $item-spacing-v;
|
||||
|
||||
&:after {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
&:after {
|
||||
content: "";
|
||||
background: $color-gray-6;
|
||||
display: block;
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: $item-spacing-h / 2;
|
||||
right: $item-spacing-h / 2;
|
||||
height: 1px;
|
||||
pointer-events: none;
|
||||
}
|
||||
}
|
||||
|
||||
.component-heading:first-child {
|
||||
background: $color-gray-3;
|
||||
border-radius: $border-radius $border-radius 0 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
strong {
|
||||
@include font-pfdin(medium);
|
||||
color: $color-orange;
|
||||
}
|
||||
|
||||
a {
|
||||
color: $color-gray-8;
|
||||
|
||||
&:hover {
|
||||
color: inherit;
|
||||
}
|
||||
}
|
||||
|
||||
.inputs {
|
||||
display: block;
|
||||
width:100%;
|
||||
border-collapse: collapse;
|
||||
|
||||
.section & .component {
|
||||
display: table;
|
||||
width:100%;
|
||||
|
||||
.label,
|
||||
.input {
|
||||
display: table-cell;
|
||||
}
|
||||
|
||||
.input {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
&.invalid {
|
||||
.input:after {
|
||||
content: "!";
|
||||
display: inline-block;
|
||||
color: $color-white;
|
||||
background: $color-orange;
|
||||
border-radius: $border-radius;
|
||||
width: 1.2em;
|
||||
text-align: center;
|
||||
height: 1.2em;
|
||||
font-size: 1em;
|
||||
vertical-align: middle;
|
||||
line-height: 1.3em;
|
||||
margin-left: -0.7em;
|
||||
position:absolute;
|
||||
@include font-pfdin(regular);
|
||||
}
|
||||
|
||||
.input input {
|
||||
color: $color-orange;
|
||||
padding-right: 1em;
|
||||
|
||||
@include placeholder {
|
||||
color: $color-orange;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,43 @@
|
||||
@import "vars";
|
||||
|
||||
@mixin font-pfdin($weight: normal) {
|
||||
|
||||
font-weight: normal;
|
||||
|
||||
@if $weight == normal or $weight == regular {
|
||||
font-family: 'PFDinDisplayPro-Regular', PFDinDisplayProRegularWebfont, sans-serif;
|
||||
}
|
||||
|
||||
@if $weight == medium {
|
||||
// iOS includes the PFDinDisplayPro-Medium in the webview but Android does not,
|
||||
// so we apply a faux bold on the regular weight.
|
||||
font-family: 'PFDinDisplayPro-Medium', PFDinDisplayProRegularWebfont, sans-serif;
|
||||
|
||||
.platform-android & {
|
||||
font-family: PFDinDisplayProRegularWebfont, sans-serif;
|
||||
font-weight: bold;
|
||||
letter-spacing: 0.025em;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@mixin pfdin-uppercase {
|
||||
text-transform: uppercase;
|
||||
position: relative;
|
||||
top: 0.05rem;
|
||||
line-height: 0.9;
|
||||
}
|
||||
|
||||
@mixin font-size($size: 1) {
|
||||
$font-size: $size * 1rem;
|
||||
font-size:$font-size;
|
||||
line-height: $base-line-height * ceil($font-size / $base-line-height);
|
||||
}
|
||||
|
||||
@mixin tap-highlight($color: rgba(255, 255, 255, 0.1)) {
|
||||
-webkit-tap-highlight-color: $color;
|
||||
|
||||
&:active {
|
||||
background-color: $color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/* http://meyerweb.com/eric/tools/css/reset/
|
||||
v2.0b1 | 201101
|
||||
NOTE: WORK IN PROGRESS
|
||||
USE WITH CAUTION AND TEST WITH ABANDON */
|
||||
|
||||
html, body, div, span, applet, object, iframe,
|
||||
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
|
||||
a, abbr, acronym, address, big, cite, code,
|
||||
del, dfn, em, img, ins, kbd, q, s, samp,
|
||||
small, strike, strong, sub, sup, tt, var,
|
||||
b, u, i, center,
|
||||
dl, dt, dd, ol, ul, li,
|
||||
fieldset, form, label, legend,
|
||||
table, caption, tbody, tfoot, thead, tr, th, td,
|
||||
article, aside, canvas, details, figcaption, figure,
|
||||
footer, header, hgroup, menu, nav, section, summary,
|
||||
time, mark, audio, video {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
outline: 0;
|
||||
font-size: 100%;
|
||||
font: inherit;
|
||||
vertical-align: baseline;
|
||||
}
|
||||
|
||||
input, textarea, button {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
/* HTML5 display-role reset for older browsers */
|
||||
article, aside, details, figcaption, figure,
|
||||
footer, header, hgroup, menu, nav, section {
|
||||
display: block;
|
||||
}
|
||||
|
||||
body {
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
ol, ul {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
blockquote:before, blockquote:after,
|
||||
q:before, q:after {
|
||||
content: '';
|
||||
content: none;
|
||||
}
|
||||
|
||||
/* apply a natural box layout model to all elements, but allowing components to change */
|
||||
html {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
*, *:before, *:after {
|
||||
box-sizing: inherit;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
@import "bourbon";
|
||||
|
||||
// Bourbon settings
|
||||
$em-base: 17px;
|
||||
|
||||
// custom vars
|
||||
$base-line-height: 1.4;
|
||||
$base-height: rem($base-line-height * $em-base);
|
||||
$border-radius: 0.25rem;
|
||||
$item-spacing-v: $base-height / 2;
|
||||
$item-spacing-h: 0.75rem;
|
||||
$small-component-width: $base-height * $golden;
|
||||
|
||||
|
||||
|
||||
// ---- Colors ------
|
||||
$color-orange: #ff4700;
|
||||
$color-orange-dark: #993d19;
|
||||
$color-red: #ff0000;
|
||||
$color-white: #ffffff;
|
||||
$color-black: #000000;
|
||||
|
||||
// background color
|
||||
$color-gray-0: #111111;
|
||||
$color-gray-1: #2f2f2f;
|
||||
$color-gray-2: #333333;
|
||||
$color-gray-3: #414141;
|
||||
$color-gray-4: #484848; // divider
|
||||
$color-gray-5: #5b5b5b;
|
||||
$color-gray-6: #666666;
|
||||
$color-gray-7: #767676;
|
||||
$color-gray-8: #858585;
|
||||
$color-gray-9: #a4a4a4 ;
|
||||
$color-gray-10: #ececec;
|
||||
$color-gray-11: #f2f2f2;
|
||||
|
||||
|
||||
|
||||
|
||||
$global-margin: 55px;
|
||||
$form-margin: 25px;
|
||||
$button-padding: 0.7rem;
|
||||
$button-padding-ios: 0.6rem;
|
||||
$button-line-height: 13px;
|
||||
$footer-height-large: 90px;
|
||||
|
||||
$box-shadow-small-components: $color-gray-1 0 0.1rem 0.1rem;
|
||||
@@ -0,0 +1,64 @@
|
||||
@import "bourbon";
|
||||
@import "clay";
|
||||
|
||||
.component-checkbox {
|
||||
|
||||
@include tap-highlight(rgba(0,0,0,0));
|
||||
display: block;
|
||||
|
||||
|
||||
> .label {
|
||||
display: block;
|
||||
padding-bottom: $item-spacing-v / 2;
|
||||
}
|
||||
|
||||
.checkbox-group {
|
||||
|
||||
label {
|
||||
padding: $item-spacing-v / 2 0;
|
||||
@include tap-highlight();
|
||||
}
|
||||
|
||||
.label {
|
||||
font-size: 0.9em;
|
||||
padding: 0 $item-spacing-h / 2;
|
||||
}
|
||||
|
||||
input {
|
||||
opacity: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
i {
|
||||
display: block;
|
||||
position: relative;
|
||||
border-radius: $border-radius;
|
||||
width: $base-height;
|
||||
height: $base-height;
|
||||
border: rem(2px) solid $color-gray-7;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
input:checked ~ i {
|
||||
border-color: $color-orange;
|
||||
background: $color-orange;
|
||||
|
||||
&:after {
|
||||
content: '';
|
||||
box-sizing: border-box;
|
||||
transform: rotate(45deg);
|
||||
position: absolute;
|
||||
left: 0.35rem;
|
||||
top: -0.05rem;
|
||||
display: block;
|
||||
width: 0.5rem;
|
||||
height: 1rem;
|
||||
border: 0 solid $color-white;
|
||||
border-right-width: rem(2px);
|
||||
border-bottom-width: rem(2px);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
@import "clay";
|
||||
|
||||
.component-color {
|
||||
|
||||
.value {
|
||||
width: $small-component-width;
|
||||
height: $base-height;
|
||||
border-radius: $base-height / 2;
|
||||
box-shadow: $box-shadow-small-components;
|
||||
}
|
||||
|
||||
.picker-wrap {
|
||||
left: 0;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
position: fixed;
|
||||
padding: 1rem;
|
||||
background: rgba(0,0,0,0.7);
|
||||
opacity: 0;
|
||||
transition: opacity 70ms ease-in 175ms;
|
||||
pointer-events: none;
|
||||
z-index: 100;
|
||||
|
||||
.picker {
|
||||
padding: 1rem;
|
||||
background: $color-gray-11;
|
||||
border-radius: $border-radius;
|
||||
}
|
||||
|
||||
&.show {
|
||||
transition-delay: 0ms;
|
||||
pointer-events: auto;
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.color-box-wrap {
|
||||
box-sizing: border-box;
|
||||
position: relative;
|
||||
height: 0;
|
||||
width: 100%;
|
||||
padding: 0 0 100% 0; // overridden with inline style
|
||||
margin: 0.6em 0 0;
|
||||
|
||||
.color-box-container {
|
||||
position: absolute;
|
||||
height: 99.97%;
|
||||
width: 100%;
|
||||
left: 0;
|
||||
top: 0;
|
||||
|
||||
.color-box {
|
||||
float:left;
|
||||
cursor: pointer;
|
||||
-webkit-tap-highlight-color: rgba(0,0,0,0);
|
||||
|
||||
&.rounded-tl {
|
||||
border-top-left-radius: $border-radius;
|
||||
}
|
||||
|
||||
&.rounded-tr {
|
||||
border-top-right-radius: $border-radius;
|
||||
}
|
||||
|
||||
&.rounded-bl {
|
||||
border-bottom-left-radius: $border-radius;
|
||||
}
|
||||
|
||||
&.rounded-br {
|
||||
border-bottom-right-radius: $border-radius;
|
||||
}
|
||||
|
||||
&.selected {
|
||||
transform: scale(1.15);
|
||||
border-radius: $border-radius;
|
||||
box-shadow: #111 0 0 0.24rem;
|
||||
position: relative;
|
||||
z-index: 100;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
@import "clay";
|
||||
|
||||
.component-input {
|
||||
|
||||
display: block;
|
||||
|
||||
.label {
|
||||
padding-bottom: $item-spacing-v;
|
||||
}
|
||||
|
||||
.input {
|
||||
position: relative;
|
||||
min-width: 100%;
|
||||
margin-top: $item-spacing-v;
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
input {
|
||||
display:block;
|
||||
width: 100%;
|
||||
background: $color-gray-2;
|
||||
border-radius: $border-radius;
|
||||
padding: $item-spacing-v / 2 $item-spacing-h / 2;
|
||||
border: none;
|
||||
vertical-align: baseline;
|
||||
color: $color-white;
|
||||
font-size: inherit;
|
||||
|
||||
@include placeholder {
|
||||
color: $color-gray-8;
|
||||
}
|
||||
|
||||
&:focus {
|
||||
@include placeholder {
|
||||
color: $color-gray-6;
|
||||
}
|
||||
border: none;
|
||||
box-shadow: none ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
@import "bourbon";
|
||||
@import "clay";
|
||||
|
||||
.component-radio {
|
||||
|
||||
@include tap-highlight(rgba(0,0,0,0));
|
||||
display: block;
|
||||
|
||||
|
||||
> .label {
|
||||
display: block;
|
||||
padding-bottom: $item-spacing-v / 2;
|
||||
}
|
||||
|
||||
.radio-group {
|
||||
|
||||
label {
|
||||
padding: $item-spacing-v / 2 0;
|
||||
@include tap-highlight();
|
||||
}
|
||||
|
||||
.label {
|
||||
font-size: 0.9em;
|
||||
padding: 0 $item-spacing-h / 2;
|
||||
}
|
||||
|
||||
input {
|
||||
opacity: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
i {
|
||||
display: block;
|
||||
position: relative;
|
||||
border-radius: $base-height;
|
||||
width: $base-height;
|
||||
height: $base-height;
|
||||
border: 2px solid $color-gray-7;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
input:checked ~ i {
|
||||
border-color: $color-orange;
|
||||
|
||||
&:after {
|
||||
$padding: 15%;
|
||||
content: '';
|
||||
display: block;
|
||||
position: absolute;
|
||||
left: $padding;
|
||||
right: $padding;
|
||||
top: $padding;
|
||||
bottom: $padding;
|
||||
border-radius: $base-height;
|
||||
background: $color-orange;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
@import "bourbon";
|
||||
@import "clay";
|
||||
|
||||
.component-select {
|
||||
position: relative;
|
||||
|
||||
.value {
|
||||
$triangle-size: 0.85rem;
|
||||
position: relative;
|
||||
padding-right: $triangle-size + 0.25rem;
|
||||
|
||||
&:after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 50%;
|
||||
margin-top: -0.1rem;
|
||||
@include triangle($triangle-size, $color-gray-10, down);
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
opacity: 0;
|
||||
position: absolute;
|
||||
display: block;
|
||||
left: 0;
|
||||
right: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
background: #000;
|
||||
width: 100%;
|
||||
border: none;
|
||||
margin:0;
|
||||
padding:0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
@import "clay";
|
||||
|
||||
.component-toggle {
|
||||
|
||||
$slide-height: $base-height * 0.75;
|
||||
$slide-width: $small-component-width;
|
||||
$marker-diameter: $base-height;
|
||||
|
||||
input {
|
||||
display: none; // @todo make sure this doesn't mess up interactivity on iOS
|
||||
}
|
||||
|
||||
.graphic {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
|
||||
.slide {
|
||||
display:block;
|
||||
border-radius: $slide-height;
|
||||
height: $slide-height;
|
||||
width: $slide-width;
|
||||
background: $color-gray-1;
|
||||
transition: background-color 150ms linear;
|
||||
}
|
||||
|
||||
.marker {
|
||||
background: $color-gray-10;
|
||||
width: $marker-diameter;
|
||||
height: $marker-diameter;
|
||||
border-radius: $marker-diameter;
|
||||
position: absolute;
|
||||
left: 0;
|
||||
display: block;
|
||||
top: ($marker-diameter - $slide-height) / 2 * -1;
|
||||
transition: transform 150ms linear;
|
||||
box-shadow: $box-shadow-small-components;
|
||||
}
|
||||
}
|
||||
|
||||
input:checked + .graphic {
|
||||
.slide {
|
||||
background: $color-orange-dark;
|
||||
}
|
||||
|
||||
.marker {
|
||||
background: $color-orange;
|
||||
transform: translateX($slide-width - $marker-diameter);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
@import "clay";
|
||||
|
||||
button,
|
||||
.button {
|
||||
@include font-pfdin(medium);
|
||||
text-transform: uppercase;
|
||||
background-color: $color-gray-4;
|
||||
border-radius: $border-radius;
|
||||
font-size: 1rem;
|
||||
line-height: 1;
|
||||
border: none;
|
||||
display: block;
|
||||
|
||||
color: $color-white;
|
||||
min-width: 12rem;
|
||||
text-align: center;
|
||||
margin: 0 auto;
|
||||
-webkit-tap-highlight-color: rgba(0,0,0,0);
|
||||
|
||||
padding: $button-padding;
|
||||
|
||||
.platform-ios & {
|
||||
padding: $button-padding-ios;
|
||||
}
|
||||
|
||||
&:disabled {
|
||||
background-color: $color-gray-3;
|
||||
color: $color-gray-8;
|
||||
}
|
||||
|
||||
&:not(:disabled):active {
|
||||
background-color: $color-gray-8;
|
||||
}
|
||||
|
||||
&.orange, &[type="submit"] {
|
||||
background-color: $color-orange;
|
||||
|
||||
&:disabled {
|
||||
background-color: $color-orange-dark;
|
||||
color: $color-gray-3;
|
||||
}
|
||||
|
||||
&:not(:disabled):active {
|
||||
background-color: $color-red;
|
||||
}
|
||||
}
|
||||
|
||||
&.light-grey {
|
||||
background-color: $color-gray-5;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
a.button {
|
||||
text-decoration: none;
|
||||
color: $color-white;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
@import "clay/fonts";
|
||||
@import "clay/reset";
|
||||
@import "clay/base";
|
||||
@import "clay/elements/button";
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
<div class="component component-checkbox">
|
||||
<span class="label">{{{label}}}</span>
|
||||
<div class="checkbox-group">
|
||||
{{each options}}
|
||||
<label>
|
||||
<span class="label">{{{this.label}}}</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
value="{{this.value}}"
|
||||
name="clay-{{clayId}}"
|
||||
data-manipulator-target
|
||||
{{each key: attributes}}{{key}}="{{this}}"{{/each}}
|
||||
/>
|
||||
<i></i>
|
||||
</label>
|
||||
{{/each}}
|
||||
</div>
|
||||
</div>
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
<div class="component component-color">
|
||||
<label>
|
||||
<input
|
||||
data-manipulator-target
|
||||
type="hidden"
|
||||
/>
|
||||
<span class="label">{{{label}}}</span>
|
||||
<span class="value"></span>
|
||||
</label>
|
||||
<div class="picker-wrap">
|
||||
<div class="picker">
|
||||
<div class="color-box-wrap">
|
||||
<div class="color-box-container"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
<footer data-manipulator-target class="component component-footer"></footer>
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
<div class="component component-heading">
|
||||
<h{{size}} data-manipulator-target></h{{size}}>
|
||||
</div>
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
<label class="component component-input">
|
||||
<span class="label">{{{label}}}</span>
|
||||
<span class="input">
|
||||
<input
|
||||
data-manipulator-target
|
||||
{{each key: attributes}}{{key}}="{{this}}"{{/each}}
|
||||
/>
|
||||
</span>
|
||||
</label>
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
<div class="component component-radio">
|
||||
<span class="label">{{{label}}}</span>
|
||||
<div class="radio-group">
|
||||
{{each options}}
|
||||
<label>
|
||||
<span class="label">{{{this.label}}}</span>
|
||||
<input
|
||||
type="radio"
|
||||
value="{{this.value}}"
|
||||
name="clay-{{clayId}}"
|
||||
data-manipulator-target
|
||||
{{each key: attributes}}{{key}}="{{this}}"{{/each}}
|
||||
/>
|
||||
<i></i>
|
||||
</label>
|
||||
{{/each}}
|
||||
</div>
|
||||
</div>
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
<label class="component component-select">
|
||||
<span class="label">{{{label}}}</span>
|
||||
<span class="value"></span>
|
||||
<select data-manipulator-target {{each key: attributes}}{{key}}="{{this}}"{{/each}}>
|
||||
{{each options}}
|
||||
<option value="{{this.value}}" class="item-select-option">{{this.label}}</option>
|
||||
{{/each}}
|
||||
</select>
|
||||
</label>
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
<div class="component component-button">
|
||||
<button
|
||||
data-manipulator-target
|
||||
type="submit"
|
||||
{{each key: attributes}}{{key}}="{{this}}"{{/each}}
|
||||
></button>
|
||||
</div>
|
||||
@@ -0,0 +1,3 @@
|
||||
<div class="component component-text">
|
||||
<p data-manipulator-target></p>
|
||||
</div>
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
<label class="component component-toggle">
|
||||
<span class="label">{{{label}}}</span>
|
||||
<span class="input">
|
||||
<input
|
||||
data-manipulator-target
|
||||
type="checkbox"
|
||||
{{each key: attributes}}{{key}}="{{this}}"{{/each}}
|
||||
/>
|
||||
<span class="graphic">
|
||||
<span class="slide"></span>
|
||||
<span class="marker"></span>
|
||||
</span>
|
||||
</span>
|
||||
</label>
|
||||
@@ -0,0 +1,76 @@
|
||||
'use strict';
|
||||
|
||||
var _ = require('../src/scripts/vendor/minified')._;
|
||||
var $ = require('../src/scripts/vendor/minified').$;
|
||||
var HTML = require('../src/scripts/vendor/minified').HTML;
|
||||
var ClayItem = require('../src/scripts/lib/clay-item');
|
||||
var ClayConfig = require('../src/scripts/lib/clay-config');
|
||||
var components = require('../src/scripts/components');
|
||||
var componentRegistry = require('../src/scripts/lib/component-registry');
|
||||
var idCounter = 0;
|
||||
|
||||
/**
|
||||
* @param {string|Object} config
|
||||
* @param {boolean} [autoRegister=true]
|
||||
* @returns {Clay~ConfigItem}
|
||||
*/
|
||||
module.exports.configItem = function(config, autoRegister) {
|
||||
if (typeof config === 'string') {
|
||||
config = { type: config };
|
||||
}
|
||||
|
||||
var result = _.extend({}, {
|
||||
label: config.type + '-label',
|
||||
appKey: 'appKey-' + idCounter,
|
||||
id: 'id-' + idCounter
|
||||
}, config);
|
||||
|
||||
idCounter++;
|
||||
|
||||
if (autoRegister !== false &&
|
||||
!componentRegistry[result.type] &&
|
||||
result.type !== 'section') {
|
||||
ClayConfig.registerComponent(components[result.type]);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string|Object} config
|
||||
* @param {boolean} [autoRegister=true]
|
||||
* @returns {ClayItem}
|
||||
*/
|
||||
module.exports.clayItem = function(config, autoRegister) {
|
||||
return new ClayItem(module.exports.configItem(config, autoRegister));
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Array} types
|
||||
* @param {boolean} [autoRegister=true]
|
||||
* @returns {*}
|
||||
*/
|
||||
module.exports.config = function(types, autoRegister) {
|
||||
return types.map(function(item) {
|
||||
return Array.isArray(item) ?
|
||||
{type: 'section', items: module.exports.config(item, autoRegister)} :
|
||||
module.exports.configItem(item, autoRegister);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Array} types
|
||||
* @param {boolean} [build=true] - run the build method on the result
|
||||
* @param {boolean} [autoRegister=true]
|
||||
* @param {Object} [settings] - settings to pass to constructor
|
||||
* @returns {ClayConfig}
|
||||
*/
|
||||
module.exports.clayConfig = function(types, build, autoRegister, settings) {
|
||||
var clayConfig = new ClayConfig(
|
||||
settings || {},
|
||||
module.exports.config(types, autoRegister),
|
||||
$(HTML('<div>'))
|
||||
);
|
||||
return build === false ? clayConfig : clayConfig.build();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
'use strict';
|
||||
|
||||
// Karma configuration
|
||||
|
||||
module.exports = function(config) {
|
||||
config.set({
|
||||
|
||||
// base path that will be used to resolve all patterns (eg. files, exclude)
|
||||
basePath: '..',
|
||||
|
||||
// frameworks to use
|
||||
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
|
||||
frameworks: ['browserify', 'source-map-support', 'mocha'],
|
||||
|
||||
browserify: {
|
||||
debug: true,
|
||||
transform: [
|
||||
[
|
||||
'stringify',
|
||||
{
|
||||
extensions: ['.html', '.tpl']
|
||||
}
|
||||
],
|
||||
'deamdify',
|
||||
[
|
||||
'sassify',
|
||||
{
|
||||
base64Encode: false,
|
||||
sourceMap: false,
|
||||
sourceMapEmbed: false,
|
||||
sourceMapContents: false,
|
||||
outputStyle: 'compact',
|
||||
includePaths: [].concat(
|
||||
require('bourbon').includePaths,
|
||||
'src/styles'
|
||||
)
|
||||
}
|
||||
],
|
||||
[
|
||||
'browserify-istanbul',
|
||||
{
|
||||
ignore: process.env.DEBUG ? ['**/**'] :
|
||||
[
|
||||
'**/test/**',
|
||||
'**/src/scripts/vendor/**',
|
||||
'**/src/scripts/config-page.js'
|
||||
]
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
|
||||
// list of files / patterns to load in the browser
|
||||
files: [
|
||||
'src/scripts/**/*.js',
|
||||
'test/spec/**/*.js'
|
||||
],
|
||||
|
||||
// list of files to exclude
|
||||
exclude: [],
|
||||
|
||||
// preprocess matching files before serving them to the browser
|
||||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
|
||||
preprocessors: {
|
||||
'src/scripts/**/*.js': ['browserify'],
|
||||
'test/spec/**/*.js': ['browserify']
|
||||
},
|
||||
|
||||
// test results reporter to use
|
||||
// possible values: 'dots', 'progress'
|
||||
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
|
||||
reporters: process.env.DEBUG ?
|
||||
['mocha'] :
|
||||
['mocha', 'coverage', 'threshold'],
|
||||
|
||||
// optionally, configure the reporter
|
||||
coverageReporter: {
|
||||
dir: 'coverage/',
|
||||
reporters: [
|
||||
{type: 'text-summary'},
|
||||
{type: 'html', subdir: '.'}
|
||||
]
|
||||
},
|
||||
|
||||
thresholdReporter: {
|
||||
statements: 100,
|
||||
branches: 100,
|
||||
functions: 100,
|
||||
lines: 100
|
||||
},
|
||||
|
||||
// web server port
|
||||
port: 9876,
|
||||
|
||||
// enable / disable colors in the output (reporters and logs)
|
||||
colors: true,
|
||||
|
||||
// level of logging
|
||||
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN ||
|
||||
// config.LOG_INFO || config.LOG_DEBUG
|
||||
logLevel: config.LOG_INFO,
|
||||
|
||||
// enable / disable watching file and executing tests whenever any file changes
|
||||
autoWatch: true,
|
||||
|
||||
// start these browsers
|
||||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
|
||||
browsers: ['Chrome'],
|
||||
|
||||
// set via the command line
|
||||
customLaunchers: {
|
||||
chromeTravisCI: {
|
||||
base: 'Chrome',
|
||||
flags: ['--no-sandbox']
|
||||
}
|
||||
},
|
||||
|
||||
// Continuous Integration mode
|
||||
// if true, Karma captures browsers, runs the tests and exits
|
||||
singleRun: true,
|
||||
|
||||
// Concurrency level
|
||||
// how many browser should be started simultaneous
|
||||
concurrency: Infinity
|
||||
});
|
||||
};
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"env": {
|
||||
"mocha": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
var fixture = require('../../fixture');
|
||||
|
||||
/**
|
||||
* @param {ClayItem} colorItem
|
||||
* @returns {void}
|
||||
*/
|
||||
function openPicker(colorItem) {
|
||||
colorItem.$element.select('label')[0].click();
|
||||
}
|
||||
|
||||
describe('component - color', function() {
|
||||
it('shows the sunlit colors in the value display', function() {
|
||||
var clayConfig = fixture.clayConfig([
|
||||
{
|
||||
type: 'color',
|
||||
sunlight: true,
|
||||
id: '1',
|
||||
defaultValue: 'ff0000'
|
||||
}
|
||||
]);
|
||||
var colorItem = clayConfig.getItemById('1');
|
||||
|
||||
assert.strictEqual(colorItem.get(), 0xff0000);
|
||||
assert.strictEqual(
|
||||
colorItem.$element.select('.value')[0].style.backgroundColor,
|
||||
'rgb(227, 84, 98)'
|
||||
);
|
||||
colorItem.set('0000FF');
|
||||
assert.strictEqual(
|
||||
colorItem.$element.select('.value')[0].style.backgroundColor,
|
||||
'rgb(0, 104, 202)'
|
||||
);
|
||||
});
|
||||
|
||||
it('shows the normal colors in the value display', function() {
|
||||
var clayConfig = fixture.clayConfig([
|
||||
{
|
||||
type: 'color',
|
||||
sunlight: false,
|
||||
id: '1',
|
||||
defaultValue: 'ff0000'
|
||||
}
|
||||
]);
|
||||
var colorItem = clayConfig.getItemById('1');
|
||||
|
||||
assert.strictEqual(colorItem.get(), 0xff0000);
|
||||
assert.strictEqual(
|
||||
colorItem.$element.select('.value')[0].style.backgroundColor,
|
||||
'rgb(255, 0, 0)'
|
||||
);
|
||||
colorItem.set('0000FF');
|
||||
assert.strictEqual(
|
||||
colorItem.$element.select('.value')[0].style.backgroundColor,
|
||||
'rgb(0, 0, 255)'
|
||||
);
|
||||
});
|
||||
|
||||
it('shows the picker when the label is clicked', function() {
|
||||
var clayConfig = fixture.clayConfig(['color']);
|
||||
var colorItem = clayConfig.getItemsByType('color')[0];
|
||||
var $picker = colorItem.$element.select('.picker-wrap');
|
||||
assert.strictEqual($picker.get('classList').contains('show'), false);
|
||||
openPicker(colorItem);
|
||||
assert.strictEqual($picker.get('classList').contains('show'), true);
|
||||
});
|
||||
|
||||
it('only shows the picker if the item is enabled', function() {
|
||||
var clayConfig = fixture.clayConfig(['color']);
|
||||
var colorItem = clayConfig.getItemsByType('color')[0];
|
||||
var $picker = colorItem.$element.select('.picker-wrap');
|
||||
colorItem.disable();
|
||||
assert.strictEqual($picker.get('classList').contains('show'), false);
|
||||
openPicker(colorItem);
|
||||
assert.strictEqual($picker.get('classList').contains('show'), false);
|
||||
colorItem.enable();
|
||||
openPicker(colorItem);
|
||||
assert.strictEqual($picker.get('classList').contains('show'), true);
|
||||
});
|
||||
|
||||
it('sets the value and closes picker when the user makes a selection', function() {
|
||||
var clayConfig = fixture.clayConfig([{type: 'color', sunlight: false}]);
|
||||
var colorItem = clayConfig.getItemsByType('color')[0];
|
||||
var $picker = colorItem.$element.select('.picker-wrap');
|
||||
assert.strictEqual(colorItem.get(), 0x000000);
|
||||
openPicker(colorItem);
|
||||
colorItem.$element.select('[data-value="16711680"]')[0].click();
|
||||
assert.strictEqual(colorItem.get(), 0xff0000);
|
||||
assert.strictEqual(
|
||||
colorItem.$element.select('.value')[0].style.backgroundColor,
|
||||
'rgb(255, 0, 0)'
|
||||
);
|
||||
assert.strictEqual($picker.get('classList').contains('show'), false);
|
||||
});
|
||||
|
||||
it('closes the picker if a user clicks the background', function() {
|
||||
var clayConfig = fixture.clayConfig([{type: 'color', sunlight: false}]);
|
||||
var colorItem = clayConfig.getItemsByType('color')[0];
|
||||
var $picker = colorItem.$element.select('.picker-wrap');
|
||||
assert.strictEqual(colorItem.get(), 0x000000);
|
||||
openPicker(colorItem);
|
||||
assert.strictEqual($picker.get('classList').contains('show'), true);
|
||||
$picker[0].click();
|
||||
assert.strictEqual($picker.get('classList').contains('show'), false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
var Joi = require('joi');
|
||||
var _ = require('../../../src/scripts/vendor/minified')._;
|
||||
var HTML = require('../../../src/scripts/vendor/minified').HTML;
|
||||
var components = require('../../../src/scripts/components');
|
||||
var manipulators = require('../../../src/scripts/lib/manipulators');
|
||||
var fixture = require('../../fixture');
|
||||
|
||||
var componentSchema = Joi.object().keys({
|
||||
name: Joi.string().required(),
|
||||
template: Joi.string().required(),
|
||||
style: Joi.string().optional(),
|
||||
manipulator: Joi.alternatives().try(
|
||||
Joi.string().valid(Object.keys(manipulators)),
|
||||
Joi.object().keys({
|
||||
get: Joi.func().required(),
|
||||
set: Joi.func().required()
|
||||
}).required().unknown(true)
|
||||
),
|
||||
defaults: Joi.object().optional(),
|
||||
initialize: Joi.func().optional()
|
||||
}).unknown(true);
|
||||
|
||||
describe('components', function() {
|
||||
_.eachObj(components, function(name, component) {
|
||||
describe(name, function() {
|
||||
it('has the correct structure', function() {
|
||||
Joi.assert(component, componentSchema);
|
||||
});
|
||||
|
||||
it('has all the necessary defaults', function() {
|
||||
assert.doesNotThrow(function() {
|
||||
HTML(component.template.trim(), component.defaults);
|
||||
});
|
||||
});
|
||||
|
||||
it('is able to be passed to ClayConfig', function() {
|
||||
fixture.clayConfig([component.name]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
var fixture = require('../../fixture');
|
||||
|
||||
describe('component - color', function() {
|
||||
it('sets the value display to the correct value on change', function() {
|
||||
var clayConfig = fixture.clayConfig([
|
||||
{
|
||||
type: 'select',
|
||||
defaultValue: 'value-1',
|
||||
options: [
|
||||
{ label: 'label 1', value: 'value-1' },
|
||||
{ label: 'label 2', value: 'value-2' }
|
||||
]
|
||||
}
|
||||
]);
|
||||
var selectItem = clayConfig.getItemsByType('select')[0];
|
||||
var $valueDisplay = selectItem.$element.select('.value');
|
||||
assert.strictEqual($valueDisplay.get('innerHTML'), 'label 1');
|
||||
selectItem.set('value-2');
|
||||
assert.strictEqual($valueDisplay.get('innerHTML'), 'label 2');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,245 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
var sinon = require('sinon');
|
||||
var _ = require('../../../src/scripts/vendor/minified')._;
|
||||
var selectComponent = require('../../../src/scripts/components/select');
|
||||
var componentRegistry = require('../../../src/scripts/lib/component-registry');
|
||||
var checkReadOnly = require('../../test-utils').checkReadOnly;
|
||||
var fixtures = require('../../fixture');
|
||||
|
||||
describe('ClayConfig', function() {
|
||||
it('defines read-only properties', function() {
|
||||
var properties = [
|
||||
'EVENTS',
|
||||
'getItemByAppKey',
|
||||
'getItemById',
|
||||
'getItemsByType',
|
||||
'getSettings',
|
||||
'registerComponent',
|
||||
'build',
|
||||
'on',
|
||||
'off',
|
||||
'trigger'
|
||||
];
|
||||
var clayConfig = fixtures.clayConfig(['input']);
|
||||
checkReadOnly(clayConfig, properties);
|
||||
});
|
||||
|
||||
describe('throws when trying to run methods before being built', function() {
|
||||
[
|
||||
'getItemByAppKey',
|
||||
'getItemById',
|
||||
'getItemsByType',
|
||||
'getSettings'
|
||||
].forEach(function(method) {
|
||||
it('.' + method + '()', function() {
|
||||
var clayConfig = fixtures.clayConfig(['input', 'text'], false);
|
||||
assert.throws(clayConfig[method], new RegExp(method));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getAllItems()', function() {
|
||||
it('returns an array of all the items', function() {
|
||||
var config = fixtures.config([
|
||||
{type: 'input', clayId: 0},
|
||||
{type: 'text', clayId: 1},
|
||||
[{type: 'input', clayId: 2}]
|
||||
]);
|
||||
var clayConfig = fixtures.clayConfig(config);
|
||||
var allItems = clayConfig.getAllItems();
|
||||
assert.strictEqual(allItems.length, 3);
|
||||
assert.deepEqual(allItems[0].config, config[0]);
|
||||
assert.deepEqual(allItems[1].config, config[1]);
|
||||
assert.deepEqual(allItems[2].config, config[2].items[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getItemByAppKey()', function() {
|
||||
it('it returns the correct item', function() {
|
||||
var config = fixtures.config([
|
||||
{type: 'input', appKey: 'test-app-key', clayId: 0},
|
||||
{type: 'input', appKey: undefined, clayId: 1}
|
||||
]);
|
||||
var clayConfig = fixtures.clayConfig(config);
|
||||
assert.deepEqual(clayConfig.getItemByAppKey('test-app-key').config, config[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getItemById()', function() {
|
||||
it('it returns the correct item', function() {
|
||||
var config = fixtures.config([
|
||||
{type: 'input', id: 'test-id', clayId: 0},
|
||||
{type: 'input', id: undefined, clayId: 1}
|
||||
]);
|
||||
var clayConfig = fixtures.clayConfig(config);
|
||||
assert.deepEqual(clayConfig.getItemById('test-id').config, config[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getItemsByType()', function() {
|
||||
it('it returns the correct items', function() {
|
||||
var config = fixtures.config([
|
||||
{type: 'input', clayId: 0},
|
||||
{type: 'text', clayId: 1},
|
||||
{type: 'input', clayId: 2}
|
||||
]);
|
||||
var clayConfig = fixtures.clayConfig(config);
|
||||
assert.deepEqual(clayConfig.getItemsByType('input')[0].config, config[0]);
|
||||
assert.deepEqual(clayConfig.getItemsByType('input')[1].config, config[2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getSettings()', function() {
|
||||
it('returns the correct settings', function() {
|
||||
var clayConfig = fixtures.clayConfig(
|
||||
[
|
||||
{type: 'input', appKey: 'test1', defaultValue: 'default val'},
|
||||
{type: 'select', appKey: 'test2', options: [
|
||||
{label: 'label-1', value: 'val-1'},
|
||||
{label: 'label-2', value: 'val-2'}
|
||||
]},
|
||||
{type: 'toggle', appKey: 'test3'}
|
||||
],
|
||||
true,
|
||||
true,
|
||||
{
|
||||
test2: 'val-2' // set one of the values via settings
|
||||
}
|
||||
);
|
||||
|
||||
assert.deepEqual(clayConfig.getSettings(), {
|
||||
test1: 'default val',
|
||||
test2: 'val-2',
|
||||
test3: false
|
||||
});
|
||||
|
||||
clayConfig.getItemByAppKey('test1').set('val-1');
|
||||
clayConfig.getItemByAppKey('test3').set(true);
|
||||
|
||||
assert.deepEqual(clayConfig.getSettings(), {
|
||||
test1: 'val-1',
|
||||
test2: 'val-2',
|
||||
test3: true
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.registerComponent()', function() {
|
||||
it('adds the component to the registry and adds the style to the HEAD',
|
||||
function(done) {
|
||||
delete componentRegistry.select;
|
||||
assert.typeOf(componentRegistry.select, 'undefined');
|
||||
var clayConfig = fixtures.clayConfig(['select'], false, false);
|
||||
|
||||
clayConfig.on(clayConfig.EVENTS.BEFORE_BUILD, function() {
|
||||
clayConfig.registerComponent(selectComponent);
|
||||
assert.strictEqual(componentRegistry.select.name, selectComponent.name);
|
||||
assert.include(document.head.innerHTML, selectComponent.style);
|
||||
});
|
||||
|
||||
clayConfig.on(clayConfig.EVENTS.AFTER_BUILD, function() {
|
||||
assert.strictEqual(this.getAllItems()[0].config.type, 'select');
|
||||
done();
|
||||
});
|
||||
|
||||
clayConfig.build();
|
||||
});
|
||||
|
||||
it('throws if manipulator is a string and does not match built-in manipulator',
|
||||
function(done) {
|
||||
delete componentRegistry.select;
|
||||
var clayConfig = fixtures.clayConfig(['select'], false, false);
|
||||
var _textComponent = _.copyObj(selectComponent);
|
||||
_textComponent.manipulator = 'not_real';
|
||||
|
||||
clayConfig.on(clayConfig.EVENTS.BEFORE_BUILD, function() {
|
||||
assert.throws(function() {
|
||||
clayConfig.registerComponent(_textComponent);
|
||||
}, new RegExp('not_real'));
|
||||
done();
|
||||
});
|
||||
|
||||
clayConfig.build();
|
||||
});
|
||||
|
||||
it('throws if manipulator does not have a `get` and `set` method',
|
||||
function(done) {
|
||||
delete componentRegistry.select;
|
||||
var clayConfig = fixtures.clayConfig(['select'], false, false);
|
||||
var _selectComponent = _.copyObj(selectComponent);
|
||||
_selectComponent.manipulator = {};
|
||||
|
||||
clayConfig.on(clayConfig.EVENTS.BEFORE_BUILD, function() {
|
||||
assert.throws(function() {
|
||||
clayConfig.registerComponent(_selectComponent);
|
||||
}, /(get.*set)|(set.*get)/);
|
||||
done();
|
||||
});
|
||||
|
||||
clayConfig.build();
|
||||
});
|
||||
|
||||
it('throws if there is no manipulator',
|
||||
function(done) {
|
||||
delete componentRegistry.select;
|
||||
var clayConfig = fixtures.clayConfig(['select'], false, false);
|
||||
var _selectComponent = _.copyObj(selectComponent);
|
||||
_selectComponent.manipulator = undefined;
|
||||
|
||||
clayConfig.on(clayConfig.EVENTS.BEFORE_BUILD, function() {
|
||||
assert.throws(function() {
|
||||
clayConfig.registerComponent(_selectComponent);
|
||||
}, /manipulator must be defined/);
|
||||
done();
|
||||
});
|
||||
|
||||
clayConfig.build();
|
||||
});
|
||||
|
||||
it('only registers the component once', function() {
|
||||
delete componentRegistry.select;
|
||||
var warnStub = sinon.stub(console, 'warn');
|
||||
var clayConfig = fixtures.clayConfig(['select'], false, false);
|
||||
var _selectComponent1 = _.copyObj(selectComponent);
|
||||
var _selectComponent2 = _.copyObj(selectComponent);
|
||||
_selectComponent2.template = 'fake';
|
||||
|
||||
assert.strictEqual(clayConfig.registerComponent(_selectComponent1), true);
|
||||
var styleCount = document.head.querySelectorAll('style').length;
|
||||
assert.strictEqual(clayConfig.registerComponent(_selectComponent2), false);
|
||||
|
||||
// make sure the styles were not added twice
|
||||
assert.strictEqual(document.head.querySelectorAll('style').length, styleCount);
|
||||
|
||||
// it should throw a warning
|
||||
assert.strictEqual(warnStub.callCount, 1, 'console.warn not called once');
|
||||
warnStub.restore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('.build()', function() {
|
||||
it('dispatches the BEFORE_BUILD event at the right time', function(done) {
|
||||
var clayConfig = fixtures.clayConfig(['input', 'text', 'input'], false);
|
||||
clayConfig.on(clayConfig.EVENTS.BEFORE_BUILD, function() {
|
||||
|
||||
// this should throw because the config has not been built yet
|
||||
assert.throws(clayConfig.getAllItems);
|
||||
done();
|
||||
});
|
||||
clayConfig.build();
|
||||
});
|
||||
|
||||
it('dispatches the AFTER_BUILD event at the right time', function(done) {
|
||||
var clayConfig = fixtures.clayConfig(['input', 'text', 'input'], false);
|
||||
clayConfig.on(clayConfig.EVENTS.AFTER_BUILD, function() {
|
||||
|
||||
// this should not throw because the config has been built
|
||||
assert.doesNotThrow(clayConfig.getAllItems);
|
||||
done();
|
||||
});
|
||||
clayConfig.build();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
var sinon = require('sinon');
|
||||
var assert = require('chai').assert;
|
||||
var ClayEvents = require('../../../src/scripts/lib/clay-events');
|
||||
var $ = require('../../../src/scripts/vendor/minified').$;
|
||||
var HTML = require('../../../src/scripts/vendor/minified').HTML;
|
||||
|
||||
/**
|
||||
* @extends ClayEvents
|
||||
*/
|
||||
var ctx;
|
||||
|
||||
var eventCounter = 0;
|
||||
|
||||
/**
|
||||
* Create a unique event name
|
||||
* @returns {string}
|
||||
*/
|
||||
function createEventName() {
|
||||
eventCounter++;
|
||||
return 'test-event-' + eventCounter;
|
||||
}
|
||||
|
||||
describe('ClayEvents', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
ctx = {};
|
||||
ClayEvents.call(ctx, $(HTML('<div>')));
|
||||
});
|
||||
|
||||
it('registers the methods on the context', function() {
|
||||
['on', 'off', 'trigger'].forEach(function(method) {
|
||||
assert.typeOf(ctx[method], 'function');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.on()', function() {
|
||||
it('registers one event', function() {
|
||||
var eventName = createEventName();
|
||||
var eventHandlerSpy = sinon.spy();
|
||||
|
||||
ctx.on(eventName, eventHandlerSpy);
|
||||
ctx.trigger(eventName);
|
||||
ctx.trigger(eventName);
|
||||
|
||||
assert(eventHandlerSpy.calledTwice, 'handler not called 2 times');
|
||||
assert(eventHandlerSpy.alwaysCalledOn(ctx), 'handler not called on ctx');
|
||||
});
|
||||
|
||||
it('registers multiple events', function() {
|
||||
var eventName1 = createEventName();
|
||||
var eventName2 = createEventName();
|
||||
var eventHandlerSpy = sinon.spy();
|
||||
|
||||
ctx.on(eventName1 + ' ' + eventName2, eventHandlerSpy);
|
||||
ctx.trigger(eventName1);
|
||||
ctx.trigger(eventName1);
|
||||
ctx.trigger(eventName2);
|
||||
ctx.trigger(eventName2);
|
||||
|
||||
assert.strictEqual(eventHandlerSpy.callCount, 4, 'handler not called 4 times');
|
||||
assert(eventHandlerSpy.alwaysCalledOn(ctx), 'handler not called on ctx');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.off()', function() {
|
||||
it('deregisters the handler for all events on the context', function() {
|
||||
var eventName1 = createEventName();
|
||||
var eventName2 = createEventName();
|
||||
var eventHandlerSpy = sinon.spy();
|
||||
|
||||
var ctx1 = ctx;
|
||||
var ctx2 = {};
|
||||
ClayEvents.call(ctx2, $(HTML('<div>')));
|
||||
|
||||
ctx.id = 1;
|
||||
ctx1.on(eventName1, eventHandlerSpy);
|
||||
ctx1.on(eventName2, eventHandlerSpy);
|
||||
ctx2.on(eventName2, eventHandlerSpy);
|
||||
|
||||
ctx1.trigger(eventName1);
|
||||
ctx1.trigger(eventName2);
|
||||
ctx2.trigger(eventName2);
|
||||
|
||||
ctx1.off(eventHandlerSpy);
|
||||
|
||||
ctx1.trigger(eventName1);
|
||||
ctx1.trigger(eventName2);
|
||||
ctx2.trigger(eventName2);
|
||||
|
||||
assert.strictEqual(eventHandlerSpy.callCount, 4, 'handler not called 4 times');
|
||||
});
|
||||
|
||||
it('does nothing if the handler does not exist', function() {
|
||||
// register a fake event so _getEventProxy() has something to look for
|
||||
ctx.on(createEventName(), sinon.spy());
|
||||
|
||||
assert.doesNotThrow(function() {
|
||||
ctx.off(sinon.spy());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.trigger()', function() {
|
||||
it('triggers the handler for the event with custom data', function() {
|
||||
var eventName = createEventName();
|
||||
var eventHandlerSpy = sinon.spy();
|
||||
var customData = {foo: 'bar'};
|
||||
|
||||
ctx.on(eventName, eventHandlerSpy);
|
||||
ctx.trigger(eventName, customData);
|
||||
|
||||
assert(eventHandlerSpy.calledOnce, 'handler not called 2 times');
|
||||
assert(eventHandlerSpy.alwaysCalledOn(ctx), 'handler not called on ctx');
|
||||
assert(eventHandlerSpy.calledWith(customData), 'handler not called on ctx');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
var sinon = require('sinon');
|
||||
var checkReadOnly = require('../../test-utils').checkReadOnly;
|
||||
var ClayItem = require('../../../src/scripts/lib/clay-item');
|
||||
var minified = require('../../../src/scripts/vendor/minified');
|
||||
var fixture = require('../../fixture');
|
||||
var componentRegistry = require('../../../src/scripts/lib/component-registry');
|
||||
|
||||
describe('ClayItem', function() {
|
||||
it('defines read-only properties', function() {
|
||||
var properties = [
|
||||
'id',
|
||||
'appKey',
|
||||
'config',
|
||||
'$element',
|
||||
'$manipulatorTarget',
|
||||
'on',
|
||||
'off',
|
||||
'trigger',
|
||||
'initialize'
|
||||
];
|
||||
var clayItem = fixture.clayItem('input');
|
||||
checkReadOnly(clayItem, properties);
|
||||
});
|
||||
|
||||
it('attaches the manipulator methods', function() {
|
||||
Object.keys(componentRegistry).forEach(function(itemName) {
|
||||
var clayItem = fixture.clayItem(itemName);
|
||||
var manipulator = componentRegistry[itemName].manipulator;
|
||||
checkReadOnly(clayItem, Object.keys(manipulator));
|
||||
});
|
||||
});
|
||||
|
||||
it('throws if a component is not in the registry', function() {
|
||||
var config = fixture.configItem('fake', false);
|
||||
/* eslint-disable no-new */
|
||||
assert.throws(function() { new ClayItem(config); }, /fake/);
|
||||
/* eslint-enable no-new */
|
||||
});
|
||||
|
||||
describe('.id', function() {
|
||||
it('sets id if config has id', function() {
|
||||
var config = fixture.configItem('input');
|
||||
var clayItem = new ClayItem(config);
|
||||
assert.strictEqual(clayItem.id, config.id);
|
||||
});
|
||||
|
||||
it('sets id to null if there is no id in the config', function() {
|
||||
var clayItem = fixture.clayItem({type: 'input', id: undefined});
|
||||
assert.strictEqual(clayItem.id, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.appKey', function() {
|
||||
it('sets appKey correctly', function() {
|
||||
var config = fixture.configItem('input');
|
||||
var clayItem = new ClayItem(config);
|
||||
assert.strictEqual(clayItem.appKey, config.appKey);
|
||||
});
|
||||
|
||||
it('sets appKey to null if there is no appKey in the config', function() {
|
||||
var clayItem = fixture.clayItem({type: 'input', appKey: undefined});
|
||||
assert.strictEqual(clayItem.appKey, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.config', function() {
|
||||
it('sets appKey correctly', function() {
|
||||
var config = fixture.configItem('input');
|
||||
var clayItem = new ClayItem(config);
|
||||
assert.strictEqual(clayItem.appKey, config.appKey);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.$element', function() {
|
||||
it('sets $element correctly', function() {
|
||||
var clayItem = fixture.clayItem('input');
|
||||
assert.strictEqual(clayItem.$element[0].tagName, 'LABEL');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.$manipulatorTarget', function() {
|
||||
it('sets the $manipulatorTarget to the root element if there are no children',
|
||||
function() {
|
||||
var clayItem = fixture.clayItem('footer');
|
||||
assert.strictEqual(clayItem.$manipulatorTarget, clayItem.$element);
|
||||
});
|
||||
it('sets the $manipulatorTarget to the correct child element', function() {
|
||||
var clayItem = fixture.clayItem('input');
|
||||
assert.strictEqual(clayItem.$manipulatorTarget[0].tagName, 'INPUT');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.initialize()', function() {
|
||||
it('calls component initializer with the ClayItem as context', function() {
|
||||
var initializeSpy = sinon.spy(componentRegistry.select, 'initialize');
|
||||
var clayConfig = fixture.clayConfig(['select']);
|
||||
assert(initializeSpy.alwaysCalledOn(clayConfig.getItemsByType('select')[0]));
|
||||
assert(initializeSpy.alwaysCalledWith(minified, clayConfig));
|
||||
initializeSpy.restore();
|
||||
});
|
||||
|
||||
it('returns itself for chaining', function() {
|
||||
var clayItem = fixture.clayItem('select');
|
||||
assert.strictEqual(clayItem.initialize(), clayItem);
|
||||
});
|
||||
|
||||
it('does nothing if there is no initialize function', function() {
|
||||
assert.doesNotThrow(fixture.clayItem('input').initialize);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,151 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
var sinon = require('sinon');
|
||||
var fixture = require('../../fixture');
|
||||
|
||||
describe('manipulators', function() {
|
||||
|
||||
/**
|
||||
* @param {string|Clay~ConfigItem} itemType
|
||||
* @param {*} value
|
||||
* @param {*} [expected]
|
||||
* @return {void}
|
||||
*/
|
||||
function testSetGet(itemType, value, expected) {
|
||||
expected = typeof expected === 'undefined' ? value : expected;
|
||||
|
||||
describe('.set() and .get()', function() {
|
||||
it('sets: "' + value + '" and gets: "' + expected + '" then triggers "change"',
|
||||
function() {
|
||||
var handlerSpy = sinon.spy();
|
||||
var clayItem = fixture.clayItem(itemType);
|
||||
clayItem.on('change', handlerSpy);
|
||||
|
||||
clayItem.set(value);
|
||||
assert.deepEqual(clayItem.get(), expected);
|
||||
assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once');
|
||||
assert(handlerSpy.calledOn(clayItem), 'handler not called on clayItem');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string|Clay~ConfigItem} itemType
|
||||
* @return {void}
|
||||
*/
|
||||
function testDisable(itemType) {
|
||||
describe('.disable()', function() {
|
||||
it('disables the field then triggers a "disabled" event', function() {
|
||||
var handlerSpy = sinon.spy();
|
||||
var clayItem = fixture.clayItem(itemType);
|
||||
clayItem.on('disabled', handlerSpy);
|
||||
assert.strictEqual(
|
||||
clayItem.$element[0].classList.contains('disabled'),
|
||||
false
|
||||
);
|
||||
clayItem.disable();
|
||||
assert.strictEqual(
|
||||
clayItem.$element[0].classList.contains('disabled'),
|
||||
true
|
||||
);
|
||||
assert.strictEqual(clayItem.$manipulatorTarget.get('disabled'), true);
|
||||
assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once');
|
||||
assert(handlerSpy.calledOn(clayItem), 'handler not called on clayItem');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} itemType
|
||||
* @return {void}
|
||||
*/
|
||||
function testEnable(itemType) {
|
||||
describe('.enable()', function() {
|
||||
it('enables the field then triggers an "enabled" event', function() {
|
||||
var handlerSpy = sinon.spy();
|
||||
var clayItem = fixture.clayItem(itemType);
|
||||
clayItem.on('enabled', handlerSpy);
|
||||
|
||||
clayItem.disable();
|
||||
assert.strictEqual(
|
||||
clayItem.$element[0].classList.contains('disabled'),
|
||||
true
|
||||
);
|
||||
clayItem.enable();
|
||||
assert.strictEqual(
|
||||
clayItem.$element[0].classList.contains('disabled'),
|
||||
false
|
||||
);
|
||||
assert.strictEqual(clayItem.$manipulatorTarget.get('disabled'), false);
|
||||
assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once');
|
||||
assert(handlerSpy.calledOn(clayItem), 'handler not called on clayItem');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('html', function() {
|
||||
testSetGet('footer', 'test123');
|
||||
});
|
||||
|
||||
describe('val', function() {
|
||||
testSetGet('input', 'test321');
|
||||
testDisable('input');
|
||||
testEnable('input');
|
||||
});
|
||||
|
||||
describe('checked', function() {
|
||||
testSetGet('toggle', true);
|
||||
testSetGet('toggle', false);
|
||||
testDisable('toggle');
|
||||
testEnable('toggle');
|
||||
});
|
||||
|
||||
describe('radiogroup', function() {
|
||||
var item = {
|
||||
type: 'radiogroup',
|
||||
clayId: 1,
|
||||
options: [
|
||||
{ label: '1', value: 'one' },
|
||||
{ label: '2', value: 'two' },
|
||||
{ label: '3', value: 'three "quote' }
|
||||
]
|
||||
};
|
||||
testSetGet(item, 'one');
|
||||
testSetGet(item, 'two');
|
||||
testSetGet(item, 'three "quote');
|
||||
testDisable(item);
|
||||
testEnable(item);
|
||||
});
|
||||
|
||||
describe('checkboxgroup', function() {
|
||||
var item = {
|
||||
type: 'checkboxgroup',
|
||||
clayId: 1,
|
||||
options: [
|
||||
{ label: '1', value: 'one' },
|
||||
{ label: '2', value: 'two' },
|
||||
{ label: '3', value: 'three "quote' }
|
||||
]
|
||||
};
|
||||
testSetGet(item, ['one', 'two']);
|
||||
testSetGet(item, ['three "quote']);
|
||||
testSetGet(item, []);
|
||||
testSetGet(item, false, []);
|
||||
testDisable(item);
|
||||
testEnable(item);
|
||||
});
|
||||
|
||||
describe('color', function() {
|
||||
testSetGet('color', 'FF0000', 0xff0000);
|
||||
testSetGet('color', '#FF0000', 0xff0000);
|
||||
testSetGet('color', '0xFF0000', 0xff0000);
|
||||
testSetGet('color', '#ff0000', 0xff0000);
|
||||
testSetGet('color', 0xff0000, 0xff0000);
|
||||
testSetGet('color', '', 0x000000);
|
||||
testSetGet('color', false, 0x000000);
|
||||
testSetGet('color', undefined, 0x000000);
|
||||
testDisable('color');
|
||||
testEnable('color');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
var utils = require('../../../src/scripts/lib/utils');
|
||||
var assert = require('chai').assert;
|
||||
|
||||
describe('Utils', function() {
|
||||
describe('.updateProperties', function() {
|
||||
var obj;
|
||||
|
||||
beforeEach(function() {
|
||||
obj = {
|
||||
one: 1,
|
||||
two: 2
|
||||
};
|
||||
});
|
||||
|
||||
it('sets the properties as non-writable', function() {
|
||||
utils.updateProperties(obj, { writable: false });
|
||||
assert.strictEqual(
|
||||
Object.getOwnPropertyDescriptor(obj, 'one').writable,
|
||||
false
|
||||
);
|
||||
assert.strictEqual(
|
||||
Object.getOwnPropertyDescriptor(obj, 'two').writable,
|
||||
false
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('chai').assert;
|
||||
|
||||
/**
|
||||
* @param {Object} object
|
||||
* @param {Array} properties
|
||||
* @return {void}
|
||||
*/
|
||||
module.exports.checkReadOnly = function(object, properties) {
|
||||
properties.forEach(function(property) {
|
||||
assert.strictEqual(
|
||||
Object.getOwnPropertyDescriptor(object, property).writable,
|
||||
false
|
||||
);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user