From 663f91e9bd5b46523919c67bd4cc1d152d118667 Mon Sep 17 00:00:00 2001 From: keegan-lillo Date: Sat, 5 Mar 2016 23:03:39 +1100 Subject: [PATCH 1/4] Made documentation less ambiguous about classes + added example for loading a different config based on platform --- README.md | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2a3e468..836d135 100755 --- a/README.md +++ b/README.md @@ -532,15 +532,23 @@ Example: ```javascript var Clay = require('./clay'); var clayConfig = require('./config'); +var clayConfigAplite = require('./config-aplite'); var clay = new Clay(clayConfig, null, { autoHandleEvents: false }); Pebble.addEventListener('showConfiguration', function(e) { + + // This is an example of how you might load a different config based on platform. + var platform = clay.meta.activeWatchInfo.platform || 'aplite'; + if (platform === 'aplite') { + clay.config = clayConfigAplite; + } + Pebble.openURL(clay.generateUrl()); }); Pebble.addEventListener('webviewclosed', function(e) { - if (e && !e.response) { - return; + if (e && !e.response) { + return; } // Get the keys and values from each config item @@ -558,6 +566,17 @@ Pebble.addEventListener('webviewclosed', function(e) { ### `Clay([Array] config, [function] customFn, [object] options)` +#### Properties + +| Property | Type | Description | +|----------|------|-------------| +| `.config` | Array | Reference to the config passed to the constructor and used for generating the page. **WARNING** this is a direct reference, not a copy of the config so any modification you make to it, will be reflected on the original as well | +| `.customFn` | Function | Reference to the custom function passed to the constructor. **WARNING** this is a direct reference, not a copy of the custom function so any modification you make to it, will be reflected on the original as well | +| `.meta` | Object | Contains information about the current user and watch | +| `.meta.activeWatchInfo` | watchinfo\|null | An object containing information on the currently connected Pebble smartwatch or null if unavailable. Read more [here](https://developer.pebble.com/docs/js/Pebble/#getActiveWatchInfo). | +| `.meta.accountToken` | String | A unique account token that is associated with the Pebble account of the current user. Read more [here](https://developer.pebble.com/docs/js/Pebble/#getAccountToken). | +| `.meta.watchToken` | String | A unique token that can be used to identify a Pebble device. Read more [here](https://developer.pebble.com/docs/js/Pebble/#getWatchToken). | + #### Methods | Method | Returns | @@ -595,27 +614,27 @@ var clay = new Clay(clayConfig, customClay); ```javascript module.exports = function(minified) { - var Clay = this; + var clayConfig = this; var _ = minified._; var $ = minified.$; var HTML = minified.HTML; function toggleBackground() { if (this.get()) { - Clay.getItemByAppKey('background').enable(); + clayConfig.getItemByAppKey('background').enable(); } else { - Clay.getItemByAppKey('background').disable(); + clayConfig.getItemByAppKey('background').disable(); } } - Clay.on(Clay.EVENTS.AFTER_BUILD, function() { - var coolStuffToggle = Clay.getItemByAppKey('cool_stuff'); + clayConfig.on(clayConfig.EVENTS.AFTER_BUILD, function() { + var coolStuffToggle = clayConfig.getItemByAppKey('cool_stuff'); toggleBackground.call(coolStuffToggle); coolStuffToggle.on('change', toggleBackground); // Hide the color picker for aplite - if (Clay.meta.activeWatchInfo.platform === 'aplite') { - Clay.getItemByAppKey('background').hide(); + if (!clayConfig.meta.activeWatchInfo || clayConfig.meta.activeWatchInfo.platform === 'aplite') { + clayConfig.getItemByAppKey('background').hide(); } }); @@ -626,7 +645,7 @@ module.exports = function(minified) { ### `ClayConfig([Object] settings, [Array] config, [$Minified] $rootContainer)` -This is the main way of talking to your generated config page. +This is the main way of talking to your generated config page. An instance of this class will be passed as the context of your custom function when it runs on the generated config page. #### Properties From 08e41036dafbbf00ab6396c01055dc6cf40e6369 Mon Sep 17 00:00:00 2001 From: keegan-lillo Date: Sat, 5 Mar 2016 23:08:52 +1100 Subject: [PATCH 2/4] Clarify when Clay.meta is actually available. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 836d135..f9b5a43 100755 --- a/README.md +++ b/README.md @@ -572,7 +572,7 @@ Pebble.addEventListener('webviewclosed', function(e) { |----------|------|-------------| | `.config` | Array | Reference to the config passed to the constructor and used for generating the page. **WARNING** this is a direct reference, not a copy of the config so any modification you make to it, will be reflected on the original as well | | `.customFn` | Function | Reference to the custom function passed to the constructor. **WARNING** this is a direct reference, not a copy of the custom function so any modification you make to it, will be reflected on the original as well | -| `.meta` | Object | Contains information about the current user and watch | +| `.meta` | Object | Contains information about the current user and watch. **WARNING** This will only be populated in the `showConfiguration` event handler. (See example above) | | `.meta.activeWatchInfo` | watchinfo\|null | An object containing information on the currently connected Pebble smartwatch or null if unavailable. Read more [here](https://developer.pebble.com/docs/js/Pebble/#getActiveWatchInfo). | | `.meta.accountToken` | String | A unique account token that is associated with the Pebble account of the current user. Read more [here](https://developer.pebble.com/docs/js/Pebble/#getAccountToken). | | `.meta.watchToken` | String | A unique token that can be used to identify a Pebble device. Read more [here](https://developer.pebble.com/docs/js/Pebble/#getWatchToken). | From 069d4a80da7f9936e0d212ffc14fdcc28a5e925b Mon Sep 17 00:00:00 2001 From: Keegan Date: Sun, 6 Mar 2016 14:28:38 +1100 Subject: [PATCH 3/4] only dispatch manipulator events if the state is actually changed. --- src/scripts/components/select.js | 11 +++++-- src/scripts/lib/manipulators.js | 32 ++++++++++++++---- src/scripts/vendor/minified.js | 56 ++++++++++++++++++++++++++++++-- test/spec/components/select.js | 2 +- test/spec/lib/manipulators.js | 23 ++++++++----- 5 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/scripts/components/select.js b/src/scripts/components/select.js index ebd3229..09be191 100644 --- a/src/scripts/components/select.js +++ b/src/scripts/components/select.js @@ -16,11 +16,18 @@ module.exports = { var $value = self.$element.select('.value'); - self.on('change', function() { + /** + * Updates the HTML value of the component to match the slected option's label + * @return {void} + */ + function setValueDisplay() { var selectedIndex = self.$manipulatorTarget.get('selectedIndex'); var $options = self.$manipulatorTarget.select('option'); var value = $options[selectedIndex] && $options[selectedIndex].innerHTML; $value.set('innerHTML', value); - }); + } + + setValueDisplay(); + self.on('change', setValueDisplay); } }; diff --git a/src/scripts/lib/manipulators.js b/src/scripts/lib/manipulators.js index 9f0000b..693e457 100755 --- a/src/scripts/lib/manipulators.js +++ b/src/scripts/lib/manipulators.js @@ -1,35 +1,45 @@ 'use strict'; +var _ = require('../vendor/minified')._; + /** - * @returns {ClayEvents} + * @returns {ClayItem|ClayEvents} + * @extends {ClayItem} */ function disable() { + if (this.$manipulatorTarget.get('disabled')) { return this; } this.$element.set('+disabled'); this.$manipulatorTarget.set('disabled', true); return this.trigger('disabled'); } /** - * @returns {ClayEvents} + * @returns {ClayItem|ClayEvents} + * @extends {ClayItem} */ function enable() { + if (!this.$manipulatorTarget.get('disabled')) { return this; } this.$element.set('-disabled'); this.$manipulatorTarget.set('disabled', false); return this.trigger('enabled'); } /** - * @returns {ClayEvents} + * @returns {ClayItem|ClayEvents} + * @extends {ClayItem} */ function hide() { + if (this.$element[0].classList.contains('hide')) { return this; } this.$element.set('+hide'); return this.trigger('hide'); } /** - * @returns {ClayEvents} + * @returns {ClayItem|ClayEvents} + * @extends {ClayItem} */ function show() { + if (!this.$element[0].classList.contains('hide')) { return this; } this.$element.set('-hide'); return this.trigger('show'); } @@ -40,6 +50,7 @@ module.exports = { return this.$manipulatorTarget.get('innerHTML'); }, set: function(value) { + if (this.get() === value.toString(10)) { return this; } this.$manipulatorTarget.set('innerHTML', value); return this.trigger('change'); }, @@ -51,6 +62,7 @@ module.exports = { return this.$manipulatorTarget.get('value'); }, set: function(value) { + if (this.get() === value.toString(10)) { return this; } this.$manipulatorTarget.set('value', value); return this.trigger('change'); }, @@ -64,6 +76,7 @@ module.exports = { return this.$manipulatorTarget.get('checked') ? 1 : 0; }, set: function(value) { + if (!this.get() === !value) { return this; } this.$manipulatorTarget.set('checked', !!value); return this.trigger('change'); }, @@ -77,6 +90,7 @@ module.exports = { return this.$element.select('input:checked').get('value'); }, set: function(value) { + if (this.get() === value.toString(10)) { return this; } this.$element .select('input[value="' + value.replace('"', '\\"') + '"]') .set('checked', true); @@ -97,8 +111,12 @@ module.exports = { }, set: function(values) { var self = this; - self.$element.select('input').set('checked', false); values = values || []; + + if (_.equals(this.get(), values)) { return this; } + + self.$element.select('input').set('checked', false); + values.map(function(value) { self.$element .select('input[value="' + value.replace('"', '\\"') + '"]') @@ -120,8 +138,10 @@ module.exports = { case 'number': value = value.toString(16); break; case 'string': value = value.replace(/^#|^0x/, ''); break; } + value = value || '000000'; - this.$manipulatorTarget.set('value', value || '000000'); + if (this.get() === parseInt(value, 16)) { return this; } + this.$manipulatorTarget.set('value', value); return this.trigger('change'); }, disable: disable, diff --git a/src/scripts/vendor/minified.js b/src/scripts/vendor/minified.js index 05b4061..a9816fb 100644 --- a/src/scripts/vendor/minified.js +++ b/src/scripts/vendor/minified.js @@ -1,8 +1,8 @@ // minified.js config start -- use this comment to re-create a configuration in the Builder // - Only sections add, always, amdsupport, copyobj, dollardollar, -// - each, eachobj, error, extend, find, format, formathtml, get, ht, html, -// - isobject, off, on, ready, request, select, set, template, trigger, underscore, -// - wait. +// - each, eachobj, equals, error, extend, find, format, formathtml, get, ht, +// - html, isobject, off, on, ready, request, select, set, template, trigger, +// - underscore, wait. // WARNING! This file is autogenerated from minified-master.js and others. @@ -1550,6 +1550,53 @@ define('minified', function() { */ 'each': listBind(each), + /*$ + * @id equals + * @group LIST + * @requires + * @configurable default + * @name .equals() + * @altname _.equals() + * @syntax list.equals(otherObject) + * @syntax _.equals(thisObject, otherObject) + * @module UTIL + * Checks whether two values, lists or objects are equal in a deep comparison. + * + * First equals() checks whether it got a function as parameter. + * If yes, it will be invoked without arguments and equals() calls itself recursively with the function's result. + * + * Once both values are no functions anymore, the values will be evaluated, If the first value is... + *
  • ...null or undefined, they are only equal if the other one is also either null or undefined.
  • + *
  • ...a value as defined by ##_.isValue(), but not a Date, they are equal if the other value is the same type and is equal according to the '==' operator.
  • + *
  • ...a Date, they are equal if the other value is a Date representing the same time.
  • + *
  • ...a list or array, they are equal if the other value is also either a list or an array, has the same number of items and all items equal the items of the other + * list at the same position. The equality of list items is determined recursively using the same rules, so you can also nest lists.
  • + *
  • ...a function, it will be invoked without arguments and its return value is evaluated using these rules as if the value has been passed.
  • + *
  • ...any other object, they are equal if they contain exactly the same keys (as defined by ##_.eachObj()) and all values are equal as determined using these rules + * recursively.
  • + *
+ * + * Please note that, according to the rules, a ##list#Minified list## is equal to an array, as long as their content is equal. equals does not + * differentiate between null and undefined. + * + * equals is commutative. If you swap the parameters, the result is the same as long as no functions are involved. + * + * @example Compare a list and an array: + *
+     *  _.equals([1, 2, 3], _(1, 2, 3));  // returns true
+     * 
+ * + * @example Same result, but with a list method: + *
+     *  _(1, 2, 3).equals([1, 2, 3]);  // returns true
+     * 
+ * + * @param thisObject The first reference to evaluate. + * @param otherObject The second reference to evaluate. + * @return true if both references are equal. False otherwise. + */ + 'equals': listBind(equals), + /*$ * @id find * @group LIST @@ -2626,6 +2673,9 @@ define('minified', function() { // @condblock find 'find': find, // @condend + // @condblock equals + 'equals': equals, + // @condend /*$ * @id copyobj diff --git a/test/spec/components/select.js b/test/spec/components/select.js index 86c7521..8ea8b2e 100644 --- a/test/spec/components/select.js +++ b/test/spec/components/select.js @@ -3,7 +3,7 @@ var assert = require('chai').assert; var fixture = require('../../fixture'); -describe('component - color', function() { +describe('component - select', function() { it('sets the value display to the correct value on change', function() { var clayConfig = fixture.clayConfig([ { diff --git a/test/spec/lib/manipulators.js b/test/spec/lib/manipulators.js index 5dff85a..b38b2dd 100644 --- a/test/spec/lib/manipulators.js +++ b/test/spec/lib/manipulators.js @@ -19,9 +19,10 @@ describe('manipulators', function() { it('sets: "' + value + '" and gets: "' + expected + '" then triggers "change"', function() { var handlerSpy = sinon.spy(); - var clayItem = fixture.clayItem(itemType); + var clayItem = fixture.clayConfig([itemType]).getAllItems()[0]; clayItem.on('change', handlerSpy); + clayItem.set(value); clayItem.set(value); assert.deepEqual(clayItem.get(), expected); assert.strictEqual(handlerSpy.callCount, 1, 'handler not called once'); @@ -45,6 +46,7 @@ describe('manipulators', function() { false ); clayItem.disable(); + clayItem.disable(); assert.strictEqual( clayItem.$element[0].classList.contains('disabled'), true @@ -73,6 +75,7 @@ describe('manipulators', function() { true ); clayItem.enable(); + clayItem.enable(); assert.strictEqual( clayItem.$element[0].classList.contains('disabled'), false @@ -100,6 +103,7 @@ describe('manipulators', function() { false ); clayItem.hide(); + clayItem.hide(); assert.strictEqual( clayItem.$element[0].classList.contains('hide'), true @@ -127,6 +131,7 @@ describe('manipulators', function() { true ); clayItem.show(); + clayItem.show(); assert.strictEqual( clayItem.$element[0].classList.contains('hide'), false @@ -145,6 +150,7 @@ describe('manipulators', function() { describe('val', function() { testSetGet('input', 'test321'); + testSetGet('input', 1234, '1234'); testDisable('input'); testEnable('input'); testShow('text'); @@ -152,10 +158,10 @@ describe('manipulators', function() { }); describe('checked', function() { - testSetGet('toggle', true, 1); - testSetGet('toggle', 1); - testSetGet('toggle', false, 0); - testSetGet('toggle', 0); + testSetGet({type: 'toggle', defaultValue: 0}, true, 1); + testSetGet({type: 'toggle', defaultValue: 0}, 1); + testSetGet({type: 'toggle', defaultValue: 1}, false, 0); + testSetGet({type: 'toggle', defaultValue: 1}, 0); testDisable('toggle'); testEnable('toggle'); testShow('toggle'); @@ -185,6 +191,7 @@ describe('manipulators', function() { var item = { type: 'checkboxgroup', clayId: 1, + defaultValue: ['two'], options: [ { label: '1', value: 'one' }, { label: '2', value: 'two' }, @@ -207,9 +214,9 @@ describe('manipulators', function() { testSetGet('color', '0xFF0000', 0xff0000); testSetGet('color', '#ff0000', 0xff0000); testSetGet('color', 0xff0000, 0xff0000); - testSetGet('color', '', 0x000000); - testSetGet('color', false, 0x000000); - testSetGet('color', undefined, 0x000000); + testSetGet({type: 'color', defaultValue: 0x00ff00}, '', 0x000000); + testSetGet({type: 'color', defaultValue: 0x00ff00}, false, 0x000000); + testSetGet({type: 'color', defaultValue: 0x00ff00}, undefined, 0x000000); testDisable('color'); testEnable('color'); testShow('color'); From 264866ce267b96cbdff10c57d31598aa550b6561 Mon Sep 17 00:00:00 2001 From: Keegan Date: Sun, 6 Mar 2016 14:32:53 +1100 Subject: [PATCH 4/4] update readme to explain new events behavior. --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a3e468..fbc650a 100755 --- a/README.md +++ b/README.md @@ -452,8 +452,12 @@ The submit button for the page. You **MUST** include this component somewhere in 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 however there are also methods to assist in interactivity such as `.hide()` and `.disable()`. **NOTE:** There is currently no way to disable or hide an entire section. You must disable/hide each item in the section to achieve this effect. + When the config page is closed, the `.get()` method is run on all components registered with an `appKey` to construct the object sent to the C app. -Many of these methods fire an event when the method is called. You can listen for these events with `ClayItem.on()`. + +Many of these methods fire an event when the method is called. You can listen for these events with `ClayItem.on()`. +**NOTE** These events will only be fired if the state actually changes. +Eg: If you run the `.show()` manipulator on an item that is already visible, the `show` event will not be triggered. #### html