all files / keystone/fields/types/url/ UrlType.js

95.24% Statements 20/21
66.67% Branches 4/6
100% Functions 3/3
95.24% Lines 20/21
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                                                                
var FieldType = require('../Type');
var TextType = require('../text/TextType');
var util = require('util');
 
 
/**
 * URL FieldType Constructor
 * @extends Field
 * @api public
 */
function url (list, path, options) {
	this._nativeType = String;
	this._underscoreMethods = ['format'];
	url.super_.call(this, list, path, options);
}
util.inherits(url, FieldType);
 
 
// TODO: is it worth adding URL specific validation logic? it would have to be
// robust so as to not trigger invalid cases on valid input, might be so
// flexible that it's not worth adding.
url.prototype.validateInput = TextType.prototype.validateInput;
url.prototype.validateRequiredInput = TextType.prototype.validateRequiredInput;
 
/* Inherit from TextType prototype */
url.prototype.addFilterToQuery = TextType.prototype.addFilterToQuery;
 
/**
 * Formats the field value using either a supplied format function or default
 * which strips the leading protocol from the value for simpler display
 */
url.prototype.format = function (item) {
	var url = item.get(this.path) || '';
	Iif (this.options.format === false) {
		return url;
	} else if (typeof this.options.format === 'function') {
		return this.options.format(url);
	} else {
		return removeProtocolPrefix(url);
	}
};
 
/**
 * Remove the protocol prefix from url
 */
function removeProtocolPrefix (url) {
	return url.replace(/^[a-zA-Z]+\:\/\//, '');
}
 
/* Export Field Type */
module.exports = url;