2012-05-10 14:18:22 +02:00
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($) {
|
|
|
|
/**
|
|
|
|
* Class: .field.urlsegment
|
|
|
|
*
|
|
|
|
* Input validation on the URLSegment field
|
|
|
|
*/
|
2012-07-24 05:59:39 +02:00
|
|
|
$('.field.urlsegment:not(.readonly)').entwine({
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor: onmatch
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
if(this.find(':text').length) {
|
|
|
|
this._addActions(); // add elements and actions for editing
|
|
|
|
this.edit(); // toggle
|
|
|
|
this._autoInputWidth(); // set width of input field
|
|
|
|
}
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-14 01:43:55 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: edit
|
|
|
|
*
|
|
|
|
* Toggles the edit state of the field
|
|
|
|
*
|
|
|
|
* Return URLSegemnt val()
|
|
|
|
*
|
|
|
|
* Parameters:
|
2012-05-13 06:55:26 +02:00
|
|
|
* (Bool) auto (optional, triggers a second toggle)
|
2012-05-10 14:18:22 +02:00
|
|
|
*/
|
2012-05-13 06:55:26 +02:00
|
|
|
edit: function(auto) {
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
var field = this.find(':text'),
|
|
|
|
holder = this.find('.preview'),
|
|
|
|
edit = this.find('.edit'),
|
|
|
|
update = this.find('.update'),
|
|
|
|
cancel = this.find('.cancel'),
|
|
|
|
help = this.find('.help');
|
|
|
|
|
|
|
|
// transfer current value to holder
|
|
|
|
holder.text(field.val());
|
|
|
|
|
|
|
|
// toggle elements
|
|
|
|
if (field.is(':visible')) {
|
|
|
|
update.hide();
|
|
|
|
cancel.hide();
|
|
|
|
field.hide();
|
|
|
|
holder.show();
|
|
|
|
edit.show();
|
|
|
|
help.hide();
|
|
|
|
} else {
|
|
|
|
edit.hide();
|
|
|
|
holder.hide();
|
|
|
|
field.show();
|
|
|
|
update.show();
|
|
|
|
cancel.show();
|
|
|
|
help.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
// field updated from another fields value
|
|
|
|
// reset to original state
|
2012-05-13 06:55:26 +02:00
|
|
|
if (auto) this.edit();
|
2012-05-10 14:18:22 +02:00
|
|
|
|
|
|
|
return field.val();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: update
|
|
|
|
*
|
|
|
|
* Commits the change of the URLSegment to the field
|
2012-05-13 06:55:26 +02: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'),
|
|
|
|
holder = this.find('.preview'),
|
|
|
|
currentVal = holder.text(),
|
|
|
|
updateVal,
|
2012-05-13 06:55:26 +02:00
|
|
|
title = arguments[0];
|
2012-05-10 14:18:22 +02:00
|
|
|
|
2012-05-13 06:55:26 +02:00
|
|
|
if (title && title !== "") {
|
|
|
|
updateVal = title;
|
2012-05-10 14:18:22 +02:00
|
|
|
} else {
|
|
|
|
updateVal = field.val();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentVal != updateVal) {
|
2012-08-17 10:05:39 +02:00
|
|
|
self.addClass('loading');
|
2012-05-10 14:18:22 +02:00
|
|
|
self.suggest(updateVal, function(data) {
|
|
|
|
var newVal = decodeURIComponent(data.value);
|
|
|
|
field.val(newVal);
|
2012-05-13 06:55:26 +02:00
|
|
|
self.edit(title);
|
2012-08-17 10:05:39 +02:00
|
|
|
self.removeClass('loading');
|
2012-05-10 14:18:22 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.edit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: cancel
|
|
|
|
*
|
|
|
|
* Cancels any changes to the field
|
|
|
|
*
|
|
|
|
* Return URLSegemnt val()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
cancel: function() {
|
|
|
|
var field = this.find(':text'),
|
|
|
|
holder = this.find('.preview');
|
|
|
|
field.val(holder.text());
|
|
|
|
this.edit();
|
|
|
|
|
|
|
|
return field.val();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: suggest
|
|
|
|
*
|
|
|
|
* Return a value matching the criteria.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) val
|
|
|
|
* (Function) callback
|
|
|
|
*/
|
|
|
|
suggest: function(val, callback) {
|
2012-06-29 14:21:44 +02:00
|
|
|
var field = this.find(':text'), urlParts = $.path.parseUrl(this.closest('form').attr('action')),
|
|
|
|
url = urlParts.hrefNoSearch + '/field/' + field.attr('name') + '/suggest/?value=' + encodeURIComponent(val);
|
|
|
|
if(urlParts.search) url += '&' + urlParts.search.replace(/^\?/, '');
|
|
|
|
|
2012-05-10 14:18:22 +02:00
|
|
|
$.get(
|
2012-06-29 14:21:44 +02:00
|
|
|
url,
|
|
|
|
function(data) {callback.apply(this, arguments);}
|
2012-05-10 14:18:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: _addActions
|
|
|
|
*
|
|
|
|
* Utility to add edit buttons and actions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_addActions: function() {
|
|
|
|
var self = this,
|
|
|
|
field = this.find(':text'),
|
|
|
|
preview,
|
|
|
|
editAction,
|
|
|
|
updateAction,
|
|
|
|
cancelAction;
|
|
|
|
|
|
|
|
// element to display non-editable text
|
|
|
|
preview = $('<span />', {
|
|
|
|
'class': 'preview'
|
|
|
|
});
|
|
|
|
|
|
|
|
// edit button
|
|
|
|
editAction = $('<button />', {
|
|
|
|
'class': 'ss-ui-button ss-ui-button-small edit',
|
2012-08-06 10:49:25 +02:00
|
|
|
'text': ss.i18n._t('URLSEGMENT.Edit'),
|
2012-05-10 14:18:22 +02:00
|
|
|
'click': function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
self.edit();
|
|
|
|
self.find(':text').focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// update button
|
|
|
|
updateAction = $('<button />', {
|
|
|
|
'class': 'update ss-ui-button-small',
|
2012-08-06 10:49:25 +02:00
|
|
|
'text': ss.i18n._t('URLSEGMENT.OK'),
|
2012-05-10 14:18:22 +02:00
|
|
|
'click': function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
self.update();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// cancel button
|
|
|
|
cancelAction = $('<button />', {
|
|
|
|
'class': 'cancel ss-ui-action-minor ss-ui-button-small',
|
|
|
|
'href': '#',
|
2012-08-06 10:49:25 +02:00
|
|
|
'text': ss.i18n._t('URLSEGMENT.Cancel'),
|
2012-05-10 14:18:22 +02:00
|
|
|
'click': function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
self.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// insert elements
|
|
|
|
preview.insertAfter('.prefix');
|
|
|
|
editAction.insertAfter(field);
|
|
|
|
cancelAction.insertAfter(field);
|
|
|
|
updateAction.insertAfter(field);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: _autoInputWidth
|
|
|
|
*
|
|
|
|
* Sets the width of input so it lines up with the other fields
|
|
|
|
*/
|
|
|
|
_autoInputWidth: function() {
|
|
|
|
var field = this.find(':text');
|
2012-05-10 17:21:30 +02:00
|
|
|
field.width((field.width() + 15) - this.find('.prefix').width());
|
2012-05-10 14:18:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2012-05-14 01:43:55 +02:00
|
|
|
}(jQuery));
|