2016-01-26 05:28:36 +01:00
|
|
|
(function (global, factory) {
|
|
|
|
if (typeof define === "function" && define.amd) {
|
2016-01-31 21:39:10 +01:00
|
|
|
define('ss.SiteTreeURLSegmentField', ['jQuery'], factory);
|
2016-01-26 05:28:36 +01:00
|
|
|
} else if (typeof exports !== "undefined") {
|
2016-01-31 21:39:10 +01:00
|
|
|
factory(require('jQuery'));
|
2016-01-26 05:28:36 +01:00
|
|
|
} else {
|
|
|
|
var mod = {
|
|
|
|
exports: {}
|
|
|
|
};
|
|
|
|
factory(global.jQuery);
|
|
|
|
global.ssSiteTreeURLSegmentField = mod.exports;
|
|
|
|
}
|
|
|
|
})(this, function (_jQuery) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _jQuery2 = _interopRequireDefault(_jQuery);
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) {
|
|
|
|
return obj && obj.__esModule ? obj : {
|
|
|
|
default: obj
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_jQuery2.default.entwine('ss', function ($) {
|
|
|
|
$('.field.urlsegment:not(.readonly)').entwine({
|
|
|
|
MaxPreviewLength: 55,
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
Ellipsis: '...',
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
onmatch: function onmatch() {
|
|
|
|
if (this.find(':text').length) this.toggleEdit(false);
|
|
|
|
this.redraw();
|
|
|
|
|
|
|
|
this._super();
|
|
|
|
},
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
redraw: function redraw() {
|
|
|
|
var field = this.find(':text'),
|
|
|
|
url = decodeURI(field.data('prefix') + field.val()),
|
|
|
|
previewUrl = url;
|
|
|
|
|
|
|
|
if (url.length > this.getMaxPreviewLength()) {
|
|
|
|
previewUrl = this.getEllipsis() + url.substr(url.length - this.getMaxPreviewLength(), url.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.find('.preview').attr('href', encodeURI(url + field.data('suffix'))).text(previewUrl);
|
|
|
|
},
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
toggleEdit: function toggleEdit(toggle) {
|
|
|
|
var field = this.find(':text');
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
this.find('.preview-holder')[toggle ? 'hide' : 'show']();
|
|
|
|
this.find('.edit-holder')[toggle ? 'show' : 'hide']();
|
|
|
|
|
|
|
|
if (toggle) {
|
2016-02-11 20:33:04 +01:00
|
|
|
field.data("origval", field.val());
|
2016-01-26 05:28:36 +01:00
|
|
|
field.focus();
|
|
|
|
}
|
|
|
|
},
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
update: function update() {
|
|
|
|
var self = this,
|
|
|
|
field = this.find(':text'),
|
|
|
|
currentVal = field.data('origval'),
|
|
|
|
title = arguments[0],
|
|
|
|
updateVal = title && title !== "" ? title : field.val();
|
|
|
|
|
|
|
|
if (currentVal != updateVal) {
|
|
|
|
this.addClass('loading');
|
|
|
|
this.suggest(updateVal, function (data) {
|
|
|
|
field.val(decodeURIComponent(data.value));
|
|
|
|
self.toggleEdit(false);
|
|
|
|
self.removeClass('loading');
|
|
|
|
self.redraw();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.toggleEdit(false);
|
|
|
|
this.redraw();
|
|
|
|
}
|
|
|
|
},
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
cancel: function cancel() {
|
|
|
|
var field = this.find(':text');
|
|
|
|
field.val(field.data("origval"));
|
|
|
|
this.toggleEdit(false);
|
|
|
|
},
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
suggest: function suggest(val, callback) {
|
|
|
|
var self = this,
|
|
|
|
field = self.find(':text'),
|
|
|
|
urlParts = $.path.parseUrl(self.closest('form').attr('action')),
|
|
|
|
url = urlParts.hrefNoSearch + '/field/' + field.attr('name') + '/suggest/?value=' + encodeURIComponent(val);
|
|
|
|
if (urlParts.search) url += '&' + urlParts.search.replace(/^\?/, '');
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
success: function success(data) {
|
|
|
|
callback.apply(this, arguments);
|
|
|
|
},
|
|
|
|
error: function error(xhr, status) {
|
|
|
|
xhr.statusText = xhr.responseText;
|
|
|
|
},
|
|
|
|
complete: function complete() {
|
|
|
|
self.removeClass('loading');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$('.field.urlsegment .edit').entwine({
|
|
|
|
onclick: function onclick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.closest('.field').toggleEdit(true);
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$('.field.urlsegment .update').entwine({
|
|
|
|
onclick: function onclick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.closest('.field').update();
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$('.field.urlsegment .cancel').entwine({
|
|
|
|
onclick: function onclick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.closest('.field').cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|