silverstripe-dms/javascript/DocumentHtmlEditorFieldToolbar.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

(function ($) {
2012-08-21 04:21:23 +02:00
"use strict";
$.entwine('ss', function ($) {
2012-08-21 04:21:23 +02:00
$('form.htmleditorfield-linkform input[name=LinkType]').entwine({
onchange: function (e) {
2012-08-21 04:21:23 +02:00
this._super(e);
var form = $('form.htmleditorfield-linkform');
var show = false;
if (this.attr('value') === 'document') {
2012-08-21 04:21:23 +02:00
if (this.is(':checked')) {
show = true;
}
}
//hide or show the additional document link addition tool
if (show) {
2012-08-31 05:45:13 +02:00
form.find('.ss-add').show();
2012-08-21 04:21:23 +02:00
} else {
2012-08-31 05:45:13 +02:00
form.find('.ss-add').hide();
2012-08-21 04:21:23 +02:00
}
},
onadd: function (e) {
2012-08-21 04:21:23 +02:00
this.change();
}
});
2012-08-21 04:21:23 +02:00
$('form.htmleditorfield-linkform').entwine({
insertLink: function () {
2012-08-21 04:21:23 +02:00
var href, target = null;
var checkedValue = this.find(':input[name=LinkType]:checked').val();
if (checkedValue === 'document') {
href = '[dms_document_link,id=' + this.find('.selected-document').data('document-id') + ']';
// Determine target
if (this.find(':input[name=TargetBlank]').is(':checked')) {
target = '_blank';
}
2012-08-21 04:21:23 +02:00
var attributes = {
href : href,
target : target,
class : 'documentLink',
2012-08-21 04:21:23 +02:00
title : this.find('.selected-document').text() //title is the text of the selected document
};
this.modifySelection(function (ed) {
2012-08-21 04:21:23 +02:00
ed.insertLink(attributes);
});
this.updateFromEditor();
return false;
} else {
this._super();
}
},
getCurrentLink: function () {
2012-08-21 04:21:23 +02:00
var selectedEl = this.getSelection(), href = "", target = "", title = "", action = "insert", style_class = "";
var linkDataSource = null;
if (selectedEl.length) {
if (selectedEl.is('a')) {
2012-08-21 04:21:23 +02:00
linkDataSource = selectedEl;
} else {
linkDataSource = selectedEl = selectedEl.parents('a:first');
}
}
if (linkDataSource && linkDataSource.length) {
this.modifySelection(function (ed) {
ed.selectNode(linkDataSource[0]);
});
}
2012-08-21 04:21:23 +02:00
// Is anchor not a link
if (!linkDataSource.attr('href')) {
linkDataSource = null;
}
2012-08-21 04:21:23 +02:00
if (linkDataSource) {
href = linkDataSource.attr('href');
target = linkDataSource.attr('target');
title = linkDataSource.attr('title');
style_class = linkDataSource.attr('class');
href = this.getEditor().cleanLink(href, linkDataSource);
action = "update";
}
//match a document or call the regular link handling
if (href.match(/^\[dms_document_link(\s*|%20|,)?id=([0-9]+)\]?$/i)) {
2012-08-21 04:21:23 +02:00
var returnArray = {
LinkType: 'document',
DocumentID: RegExp.$2,
Description: title
};
//show the selected document
$('.document-add-existing').selectdocument(returnArray.DocumentID,returnArray.Description);
//select the correct radio button
$('form.htmleditorfield-linkform input[name=LinkType][value=document]').click();
return returnArray;
} else {
$('.document-add-existing').selectdocument(); //clear the selected document
$('form.htmleditorfield-linkform .ss-add.ss-upload').hide();
return this._super();
}
}
});
});
}(jQuery));