gd-sprest-js
Version:
SharePoint 2013/Online js components.
91 lines (90 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var _1 = require(".");
/**
* Link Field
*/
exports.LinkField = function (props) {
var _desc = null;
var _url = null;
// Method to get the link element
var get = function () {
// Returns the link element
return _url ? _url.get()._textField : null;
};
// Method to get the fabric component
var getFabricComponent = function () {
// Return the link
return _url;
};
// Method to get the value
var getValue = function () {
// Get the link value
return {
Description: _desc ? _desc.getValue() : "",
Url: _url ? _url.getValue() : ""
};
};
// Method to validate the url
var validate = function (url) {
// Clear the error message
_url.setErrorMessage("");
// See if the url exists
if (url) {
// Validate the url
if (/^https?\:\/\//.test(url) == false) {
// Set the error message
_url.setErrorMessage("The value must start with http:// or https://");
}
}
else {
// See if this field is required
if (props.required) {
// Set the error message
_url.setErrorMessage("This field requires a value.");
}
}
// Call the change event
props.onChange ? function (value) { props.onChange(getValue()); } : null;
};
// See if the field is disabled
if (props.disable) {
// Add the link html
props.el.innerHTML = _1.Templates.LinkField(props);
}
else {
// Add the link html
props.el.innerHTML = [
'<div class="url"></div>',
'<div class="description"></div>'
].join('\n');
// Create the url textfield
_url = _1.TextField({
disable: props.disable,
el: props.el.children[0],
label: props.label,
onChange: validate,
required: props.required,
type: _1.TextFieldTypes.Underline,
value: props.value && props.value.Url ? props.value.Url : ""
});
// Create the description textfield
_desc = _1.TextField({
disable: props.disable,
el: props.el.children[1],
label: props.label + " Description",
onChange: props.onChange ? function (value) { props.onChange(getValue()); } : null,
required: props.required,
type: _1.TextFieldTypes.Underline,
value: props.value && props.value.Description ? props.value.Description : ""
});
// Validate the url
validate(props.value ? props.value.Url : "");
}
// Return the link
return {
get: get,
getFabricComponent: getFabricComponent,
getValue: getValue
};
};