Skip to content

Instantly share code, notes, and snippets.

@rossmari
Last active August 29, 2015 14:06
Show Gist options
  • Save rossmari/c676ce830e1286fa8b1c to your computer and use it in GitHub Desktop.
Save rossmari/c676ce830e1286fa8b1c to your computer and use it in GitHub Desktop.
ckeditor plugin with crazy semaphore
CKEDITOR.dialog.add( 'dossiersDialog', function ( editor ) {
var selectedPersonId = null;
var semaphoreActive = false;
var start_inputs_value = 1;
var inputs_count = 1;
var searchValue = '';
var person_select = null;
function sendNameQuery()
{
$.ajax
({
type: "GET",
url: '/admin/dossiers',
data: { data: searchValue },
dataType: 'JSON'
}).success(
function(data, textStatus, jqXHR)
{
person_select.find('option').remove();
$.each(data.names, function(index, element){
var new_option = new Option( element[0].name, element[0].id );
person_select.append(new_option);
})
}
).error(
function(data, textStatus, jqXHR){
alert('errors!');
}
)
}
function startSemaphore()
{
//set semaphore flag
semaphoreActive = true;
var intervalID = setInterval(function(){
inputs_count = inputs_count - 1;
if(inputs_count == 0)
{
clearInterval(intervalID);
semaphoreActive = false;
sendNameQuery();
}
}, 1000);
}
return {
title: 'Ссылка на досье',
minWidth: 400,
minHeight: 200,
contents: [
{
id: 'tab-basic',
label: 'Basic Settings',
elements: [
{
type: 'text',
label: 'Фамилия/Имя человека',
onInput: function(){
//store input value
searchValue = this.getValue()
//if semaphore deactivated - start it
if(!semaphoreActive)
{
inputs_count = start_inputs_value;
startSemaphore();
return;
}
else
{
inputs_count = start_inputs_value;
return;
}
}
},
{
type: 'hbox',
align: 'right',
width: '200px',
children: [
{
type: 'html',
html: '<image src="' + CKEDITOR.basePath + 'plugins/dossiers/icons/incognito.png" id="person_image" style="width:135px; height: 135px;"></image>'
},
{
type: 'select',
id: 'person_select',
label: 'Выберите досье',
items: [ [ 'Basketball' ], [ 'Baseball' ], [ 'Hockey' ], [ 'Football' ] ],
'default': '',
controlStyle: 'width: 200px;',
size: 3,
style: 'width:150px; height: 120px;',
onChange: function( api ) {
selectedPersonId = this.getValue();
}
}
]
}
]
}
],
onShow: function(){
// BAD hook, don't know how to get elements by id from extended functions
var select_id = this.getContentElement('tab-basic', 'person_select').domId;
person_select = $('#' + select_id).find('select');
},
onOk: function() {
if(selectedPersonId != null) {
var selection = editor.getSelection();
var text = selection.getSelectedText();
var dossier_tag = editor.document.createElement('span');
dossier_tag.setAttribute('class', 'dossier_popup');
dossier_tag.setAttribute('style', 'color:blue;');
dossier_tag.setText(text);
dossier_tag.setAttribute('id', selectedPersonId);
editor.insertElement(dossier_tag);
}
else
{
return;
}
}
};
});
CKEDITOR.plugins.add('dossiers',
{
icons: 'dossiers',
init: function (editor) {
editor.addCommand('openDialog', new CKEDITOR.dialogCommand('dossiersDialog', {
allowedContent: 'span[id](dossier_popup){color}'}));
editor.ui.addButton('Dossiers',
{
label: 'Dossier Plugin',
command: 'openDialog',
toolbar: 'others'
});
CKEDITOR.dialog.add( 'dossiersDialog', this.path + 'dialogs/dossiers.js' );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment