2012-05-10 14:18:22 +02:00
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($) {
|
|
|
|
/**
|
|
|
|
* Class: .field.urlsegment
|
2013-02-04 00:44:50 +01:00
|
|
|
*
|
|
|
|
* Provides enhanced functionality (read-only/edit switch) and
|
|
|
|
* input validation on the URLSegment field
|
2012-05-10 14:18:22 +02:00
|
|
|
*/
|
2012-07-24 05:59:39 +02:00
|
|
|
$('.field.urlsegment:not(.readonly)').entwine({
|
2012-05-10 14:18:22 +02:00
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
// Roughly matches the field width including edit button
|
|
|
|
MaxPreviewLength: 55,
|
|
|
|
|
|
|
|
Ellipsis: '...',
|
|
|
|
|
2012-05-10 14:18:22 +02:00
|
|
|
onmatch : function() {
|
2012-08-16 09:48:54 +02:00
|
|
|
// Only initialize the field if it contains an editable field.
|
|
|
|
// This ensures we don't get bogus previews on readonly fields.
|
2013-02-04 00:44:50 +01:00
|
|
|
if(this.find(':text').length) this.toggleEdit(false);
|
|
|
|
this.redraw();
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
this._super();
|
|
|
|
},
|
2013-02-04 00:44:50 +01:00
|
|
|
|
|
|
|
redraw: function() {
|
|
|
|
var field = this.find(':text'),
|
|
|
|
url = field.data('prefix') + field.val(),
|
|
|
|
previewUrl = url;
|
|
|
|
|
|
|
|
// Truncate URL if required (ignoring the suffix, retaining the full value)
|
|
|
|
if(url.length > this.getMaxPreviewLength()) {
|
|
|
|
previewUrl = this.getEllipsis() + url.substr(url.length - this.getMaxPreviewLength(), url.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer current value to holder
|
|
|
|
this.find('.preview').attr('href', url + field.data('suffix')).text(previewUrl);
|
2012-05-14 01:43:55 +02:00
|
|
|
},
|
2013-02-04 00:44:50 +01:00
|
|
|
|
2012-05-10 14:18:22 +02:00
|
|
|
/**
|
2013-02-04 00:44:50 +01:00
|
|
|
* @param Boolean
|
2012-05-10 14:18:22 +02:00
|
|
|
*/
|
2013-02-04 00:44:50 +01:00
|
|
|
toggleEdit: function(toggle) {
|
|
|
|
var field = this.find(':text');
|
|
|
|
|
|
|
|
this.find('.preview-holder')[toggle ? 'hide' : 'show']();
|
|
|
|
this.find('.edit-holder')[toggle ? 'show' : 'hide']();
|
|
|
|
|
|
|
|
if(toggle) {
|
|
|
|
field.data("origval", field.val()); //retain current value for cancel
|
|
|
|
field.focus();
|
2012-05-10 14:18:22 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits the change of the URLSegment to the field
|
2013-02-04 00:44:50 +01:00
|
|
|
* Optional: pass in (String) to update the URLSegment
|
2012-05-10 14:18:22 +02:00
|
|
|
*/
|
|
|
|
update: function() {
|
|
|
|
var self = this,
|
|
|
|
field = this.find(':text'),
|
2013-02-04 00:44:50 +01:00
|
|
|
currentVal = field.data('origval'),
|
|
|
|
title = arguments[0],
|
|
|
|
updateVal = (title && title !== "") ? title : field.val();
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
if (currentVal != updateVal) {
|
2013-02-04 00:44:50 +01:00
|
|
|
this.addClass('loading');
|
|
|
|
this.suggest(updateVal, function(data) {
|
|
|
|
field.val(decodeURIComponent(data.value));
|
|
|
|
self.toggleEdit(false);
|
2012-08-17 10:05:39 +02:00
|
|
|
self.removeClass('loading');
|
2013-02-04 00:44:50 +01:00
|
|
|
self.redraw();
|
2012-05-10 14:18:22 +02:00
|
|
|
});
|
|
|
|
} else {
|
2013-02-04 00:44:50 +01:00
|
|
|
this.toggleEdit(false);
|
|
|
|
this.redraw();
|
2012-05-10 14:18:22 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels any changes to the field
|
|
|
|
*/
|
|
|
|
cancel: function() {
|
2013-02-04 00:44:50 +01:00
|
|
|
var field = this.find(':text');
|
|
|
|
field.val(field.data("origval"));
|
|
|
|
this.toggleEdit(false);
|
2012-05-10 14:18:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a value matching the criteria.
|
|
|
|
*
|
2013-02-04 00:44:50 +01:00
|
|
|
* @param (String)
|
|
|
|
* @param (Function)
|
2012-05-10 14:18:22 +02:00
|
|
|
*/
|
|
|
|
suggest: function(val, callback) {
|
2013-02-04 00:44:50 +01:00
|
|
|
var field = this.find(':text'),
|
|
|
|
urlParts = $.path.parseUrl(this.closest('form').attr('action')),
|
2012-06-29 14:21:44 +02:00
|
|
|
url = urlParts.hrefNoSearch + '/field/' + field.attr('name') + '/suggest/?value=' + encodeURIComponent(val);
|
|
|
|
if(urlParts.search) url += '&' + urlParts.search.replace(/^\?/, '');
|
|
|
|
|
2013-02-04 00:44:50 +01:00
|
|
|
$.get(url, function(data) {callback.apply(this, arguments);});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.field.urlsegment .edit').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.closest('.field').toggleEdit(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.field.urlsegment .update').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.closest('.field').update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.field.urlsegment .cancel').entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.closest('.field').cancel();
|
2012-05-10 14:18:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-02-04 00:44:50 +01:00
|
|
|
|
2012-05-14 01:43:55 +02:00
|
|
|
}(jQuery));
|