` - an array of config items that match the provided `type`. |
-| `.getSettings()` | `Object` - an object 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. This method may do some conversions depending on the type of the setting. Arrays containing strings will have zeros inserted before each item. eg `['one', 'two']` becomes `['one', 0, 'two', 0]`. Booleans will be converted to numbers. eg `true` becomes `1` and `false` becomes `0` |
+| `.getSettings()` | `Object` - an object 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()`
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 )`
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 )`
Remove the given event handler. **NOTE:** This will remove the handler from all registered events. | `ClayConfig` |
diff --git a/index.js b/index.js
index 216391a..587b14a 100755
--- a/index.js
+++ b/index.js
@@ -3,7 +3,8 @@
var configPageHtml = require('./tmp/config-page.html');
var toSource = require('tosource');
var standardComponents = require('./src/scripts/components');
-
+var utils = require('./src/scripts/lib/utils');
+var _ = require('./src/scripts/vendor/minified')._;
/**
* @param {Array} config - the Clay config
* @param {function} [customFn] - Custom code to run from the config page. Will run
@@ -149,9 +150,10 @@ Clay.prototype.generateUrl = function() {
/**
* Parse the response from the webviewclosed event data
* @param {string} response
+ * @param {boolean} [convert=true]
* @returns {Object}
*/
-Clay.prototype.getSettings = function(response) {
+Clay.prototype.getSettings = function(response, convert) {
// Decode and parse config data as JSON
var settings = {};
@@ -162,7 +164,14 @@ Clay.prototype.getSettings = function(response) {
}
localStorage.setItem('clay-settings', JSON.stringify(settings));
- return settings;
+
+ if (convert === false) {
+ return settings;
+ } else {
+ return _.mapObj(settings, function(key, value) {
+ return utils.prepareForAppMessage(value);
+ });
+ }
};
/**
diff --git a/src/scripts/lib/clay-config.js b/src/scripts/lib/clay-config.js
index 7de1379..750e700 100644
--- a/src/scripts/lib/clay-config.js
+++ b/src/scripts/lib/clay-config.js
@@ -158,7 +158,7 @@ function ClayConfig(settings, config, $rootContainer, meta) {
self.getSettings = function() {
_checkBuilt('getSettings');
_.eachObj(_itemsByAppKey, function(appKey, item) {
- _settings[appKey] = utils.prepareForAppMessage(item.get());
+ _settings[appKey] = item.get();
});
return _settings;
};
diff --git a/src/scripts/vendor/minified.js b/src/scripts/vendor/minified.js
index a9816fb..bfb2cd8 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, equals, error, extend, find, format, formathtml, get, ht,
-// - html, isobject, off, on, ready, request, select, set, template, trigger,
-// - underscore, wait.
+// - html, isobject, mapobj, off, on, ready, request, select, set, template,
+// - trigger, underscore, wait.
// WARNING! This file is autogenerated from minified-master.js and others.
@@ -2774,6 +2774,46 @@ define('minified', function() {
*/
'eachObj': eachObj,
+ /*$
+ * @id mapobj
+ * @group OBJECT
+ * @requires
+ * @configurable default
+ * @name _.mapObj()
+ * @syntax _.mapObj(obj, callback)
+ * @syntax _.mapObj(obj, callback, ctx)
+ * @module UTIL
+ * Creates a new object with the same properties but different values using the given callback function. The function is called
+ * for each property of the input object to provice a new value for the property.
+ *
+ * @example Increases the values of all properties.
+ *
+ * var r = _.mapObj({a: 1, b: 5, c: 2}, function(key, value) {
+ * return value + 1;
+ * });
+ * // r is now {a: 2, b: 6, c: 2}
+ *
+ *
+ * @param obj the object to use
+ * @param callback The callback function(key, value) to invoke for each property.
+ * - key
- The name of the current property.
+ * - value
- The value of the current property.
+ * - this
- The given context. If not set, the object itself.
+ * - (callback return value)
- This value will replace the original value in the new object.
+ * @param ctx optional a context to pass to the callback as 'this'.
+ * @return the new object
+ *
+ * @see ##_.filterObj() filters an object.
+ * @see ##map() maps a list.
+ */
+ 'mapObj': function(obj, mapFunc, ctx) {
+ var result = {};
+ eachObj(obj, function(key, value) {
+ result[key] = mapFunc.call(ctx || obj, key, value);
+ });
+ return result;
+ },
+
/*$
* @id isobject
* @group TYPE
diff --git a/test/spec/index.js b/test/spec/index.js
index f124db4..099e378 100644
--- a/test/spec/index.js
+++ b/test/spec/index.js
@@ -254,6 +254,47 @@ describe('Clay', function() {
}, /Not Valid JSON/i);
assert.equal(localStorage.getItem('clay-settings'), '{"appKey":"value"}');
});
+
+ it('Prepares the settings for sendAppMessage', function() {
+ var clay = fixture.clay([]);
+ var response = encodeURIComponent(JSON.stringify({
+ test1: false,
+ test2: 'val-2',
+ test3: true,
+ test4: ['cb-1', 'cb-3'],
+ test5: 12345,
+ test6: [1, 2, 3, 4],
+ test7: [true, false, true]
+ }));
+ var expected = {
+ test1: 0,
+ test2: 'val-2',
+ test3: 1,
+ test4: ['cb-1', 0, 'cb-3', 0],
+ test5: 12345,
+ test6: [1, 2, 3, 4],
+ test7: [1, 0, 1]
+ };
+
+ assert.deepEqual(clay.getSettings(response), expected);
+ });
+
+ it('does not prepare the settings for sendAppMessage if convert is false',
+ function() {
+ var clay = fixture.clay([]);
+ var settings = {
+ test1: false,
+ test2: 'val-2',
+ test3: true,
+ test4: ['cb-1', 'cb-3'],
+ test5: 12345,
+ test6: [1, 2, 3, 4],
+ test7: [true, false, true]
+ };
+ var response = encodeURIComponent(JSON.stringify(settings));
+
+ assert.deepEqual(clay.getSettings(response, false), settings);
+ });
});
describe('.meta', function() {
diff --git a/test/spec/lib/clay-config.js b/test/spec/lib/clay-config.js
index 8275bab..887186a 100644
--- a/test/spec/lib/clay-config.js
+++ b/test/spec/lib/clay-config.js
@@ -101,31 +101,29 @@ describe('ClayConfig', function() {
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'},
- {type: 'checkboxgroup', appKey: 'test4', options: [
- {label: 'label-1', value: 'cb-1'},
- {label: 'label-2', value: 'cb-2'},
- {label: 'label-2', value: 'cb-3'}
- ]}
- ],
- true,
- true,
- {
- test2: 'val-2' // set one of the values via settings
- }
- );
+ var config = [
+ {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'},
+ {type: 'checkboxgroup', appKey: 'test4', options: [
+ {label: 'label-1', value: 'cb-1'},
+ {label: 'label-2', value: 'cb-2'},
+ {label: 'label-2', value: 'cb-3'}
+ ]}
+ ];
+ var settings = {
+ test2: 'val-2'
+ };
+
+ var clayConfig = fixtures.clayConfig(config, true, true, settings);
assert.deepEqual(clayConfig.getSettings(), {
test1: 'default val',
test2: 'val-2',
- test3: 0,
+ test3: false,
test4: []
});
@@ -136,8 +134,15 @@ describe('ClayConfig', function() {
assert.deepEqual(clayConfig.getSettings(), {
test1: 'val-1',
test2: 'val-2',
- test3: 1,
- test4: ['cb-1', 0, 'cb-3', 0]
+ test3: true,
+ test4: ['cb-1', 'cb-3']
+ });
+
+ // make sure the result of getSettings() can actually be fed back in to
+ // a new instance of ClayConfig
+ assert.doesNotThrow(function() {
+ settings = clayConfig.getSettings();
+ fixtures.clayConfig(config, true, true, settings);
});
});
});