edit: right-align ckeditor toolbar
[minimedit.git] / edit.js
1 CKEDITOR.plugins.add('inlinesave', {
2         init: function(editor) {
3                 editor.addCommand( 'inlinesave', {
4                         exec: function (editor) {
5                                 var pagename = window.location.pathname.replace(/\/$/, '/index');
6                                 var data = 'body='+encodeURIComponent(editor.getData());
7                                 ajaxpost = new XMLHttpRequest();
8                                 ajaxpost.open('POST', '/edit'+pagename, true);
9                                 ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
10                                 ajaxpost.onreadystatechange = function () {
11                                         if (ajaxpost.readyState != 4)
12                                                 return; // not done yet
13                                         if (ajaxpost.status != 200)
14                                                 alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
15                                         else
16                                                 alert('Pagina is goed opgeslagen');
17                                 };
18                                 ajaxpost.send(data);
19                         },
20                 });
21                 editor.ui.addButton( 'Inlinesave', {
22                         command: 'inlinesave',
23                         label: editor.lang.save.toolbar,
24                         icon: 'save',
25                 });
26         }
27 });
28
29 CKEDITOR.on('dialogDefinition', function (event) {
30         if (event.data.name === 'table') {
31                 // override initial attribute values
32                 var infoTab = event.data.definition.getContents('info');
33                 infoTab.get('txtWidth').default = '';
34                 infoTab.get('txtBorder').default = '0';
35                 infoTab.get('txtCellSpace').default = '';
36                 infoTab.get('txtCellPad').default = '';
37         }
38 });
39
40 CKEDITOR.on('instanceCreated', function (event) {
41         var editor = event.editor;
42         editor.on('configLoaded', function () {
43                 var config = editor.config;
44                 config.language = 'nl';
45                 config.extraPlugins = 'sourcedialog,inlinesave';
46                 config.format_tags = 'h2;h3;h4;p';
47                 config.allowedContent = true;
48                 config.entities = false; // keep unicode
49                 config.filebrowserImageUploadUrl = '/edit?type=img';
50                 config.forcePasteAsPlainText = true;
51                 config.contentsCss = '/excelsior.css';
52                 config.toolbar = [
53                         ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
54                         ['Format'],
55                         ['BulletedList', 'NumberedList', '-', 'Blockquote'],
56                         ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Anchor', 'Link'],
57                         ['HorizontalRule', 'Table', 'Image'],
58                 ];
59                 config.toolbarCanCollapse = true;
60                 config.floatSpacePreferRight = true;
61                 config.floatSpaceDockedOffsetY = 0;
62                 config.startupFocus = true;
63
64                 config.disableObjectResizing = true;
65                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
66         });
67 });
68
69         CKEDITOR.on('instanceReady', function (event) {
70                 var editor = event.editor;
71                 var writer = editor.dataProcessor.writer;
72                 writer.selfClosingEnd = '>';
73                 writer.setRules( 'p', {
74                         breakAfterOpen: true,
75                         breakBeforeClose: true,
76                 });
77         });
78
79         CKEDITOR.disableAutoInline = true;
80
81 // add edit link to menu
82 var pagebody = document.getElementsByClassName('static')[0];
83 if (pagebody) {
84         var editlink = document.createElement('a');
85         editlink.style.cursor = 'pointer';
86         editlink.appendChild(document.createTextNode('Wijzig'));
87         editlink.onclick = function (e) {
88                 var toggled = editlink.style.fontWeight;
89                 editlink.style.fontWeight = toggled ? '' : 'bold';
90                 pagebody.setAttribute('contenteditable', !toggled);
91                 if (toggled) {
92                         for (name in CKEDITOR.instances) {
93                                 CKEDITOR.instances[name].destroy()
94                         }
95                 }
96                 else {
97                         CKEDITOR.inline(pagebody);
98                 }
99                 document.body.className = toggled ? '' : 'edit';
100                 return false;
101         };
102         document.querySelector('header ul').appendChild(editlink);
103 }
104