# all files / keystone/lib/content/ page.js

27.27% Statements 15/55
0% Branches 0/28
0% Functions 0/10
27.27% Lines 15/55

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    2181×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×
     var _ = require('lodash');
    var assign = require('object-assign');
    var keystone = require('../../');
    var utils = keystone.utils;
    var Type = require('./type');

    /**
     * Page Class
     *
     * @param {String} key
     * @param {Object} options
     * @api public
     */

    function Page (key, options) {

    	if (!(this instanceof Page)) {
    		return new Page(key, options);
    	}

    	this.options = assign({}, options);
    	this.key = key;
    	this.fields = {};

    }

    Object.defineProperty(Page.prototype, 'name', {
    	get: function () {
    		return this.get('name') || this.set('name', utils.keyToLabel(this.key));
    	},
    });

    /**
     * Sets page option
     *
     * ####Example:
     *
     *     page.set('test', value) // sets the 'test' option to `value`
     *
     * @param {String} key
     * @param {String} value
     * @return value
     * @api public
     */

    Page.prototype.set = function (key, value) {

    	if (!key) {
    		throw new Error('keystone.content.Page.set() Error: must be provided with a key to set a value.');
    	}

    	value = value || null;
    	this.options[key] = value;
    	return value;

    };

    /**
     * Gets page option
     *
     * ####Example:
     *
     *     page.get('test') // returns the value of 'test' key
     *
     * @param {String} key
     * @return any
     * @method get
     * @api public
     */

    Page.prototype.get = function (key) {
    	if (!key) {
    		throw new Error('keystone.content.Paget.get() Error: must be provided with a key to get a value.');
    	}

    	if (!this.options.hasOwnProperty(key)) {
    		return null;
    	}

    	return this.options[key];
    };

    /**
     * Adds one or more fields to the page
     *
     * @api public
     */

    Page.prototype.add = function (fields) {

    	// TODO: nested paths
    	if (!utils.isObject(fields)) {
    		throw new Error('keystone.content.Page.add() Error: fields must be an object.');
    	}

    	var self = this;

    	_.forEach(fields, function (options, path) {

    		if (typeof options === 'function') {
    			options = { type: options };
    		}

    		if (typeof options.type !== 'function') {
    			throw new Error('keystone.content.page.add() Error: Page fields must be specified with a type function');
    		}

    		if (options.type.prototype.__proto__ !== Type.prototype) { // eslint-disable-line no-proto

    			// Convert native field types to their default Keystone counterpart

    			if (options.type === String) {
    				options.type = keystone.content.Types.Text;
    			}

    			// TODO: More types
    			// else if (options.type == Number)
    			// 	options.type = Field.Types.Number;
    			// else if (options.type == Boolean)
    			// 	options.type = Field.Types.Boolean;
    			// else if (options.type == Date)
    			// 	options.type = Field.Types.Datetime;

    			else {
    				throw new Error('keystone.content.page.add() Error: Unrecognised field constructor: ' + options.type);
    			}

    		}

    		self.fields[path] = new options.type(path, options);

    	});

    	return this;

    };

    /**
     * Registers the page with Keystone.
     *
     * ####Example:
     *
     * 		var homePage = new keystone.content.Page('home');
     * 		// ...
     * 		homePage.register();
     *
     * 		// later...
     * 		var homePage = keystone.content.page('home');
     *
     * @api public
     */

    Page.prototype.register = function () {
    	return keystone.content.page(this.key, this);
    };

    /**
     * Populates a data structure based on defined fields
     *
     * @api public
     */

    Page.prototype.populate = function (data) {

    	if (typeof data !== 'object') {
    		data = {};
    	}

    	// TODO: implement schema

    	return data;

    };

    /**
     * Validates a data structure based on defined fields
     *
     * @api public
     */

    Page.prototype.validate = function (data) {

    	if (typeof data !== 'object') {
    		data = {};
    	}

    	// TODO: implement schema

    	return data;

    };

    /**
     * Cleans a data structure so only the defined fields are present
     *
     * @api public
     */

    Page.prototype.clean = function (data) {

    	if (typeof data !== 'object') {
    		data = {};
    	}

    	// TODO: implement schema

    	return data;

    };

    /*!
     * Export class
     */

    module.exports = Page;