3 * @class Roo.bootstrap.form.HtmlEditorToolbar.Standard
4 * @parent Roo.bootstrap.form.HtmlEditor
5 * @extends Roo.bootstrap.nav.Simplebar
11 new Roo.bootstrap.form.HtmlEditor({
14 new Roo.bootstrap.form.HtmlEditorToolbar.Standard({
15 disable : { fonts: 1 , format: 1, ..., ... , ...],
21 * @cfg {Object} disable List of elements to disable..
22 * @cfg {Array} btns List of additional buttons.
26 * .x-html-editor-tb .x-edit-none .x-btn-text { background: none; }
29 Roo.bootstrap.form.HtmlEditorToolbar.Standard = function(config)
32 Roo.apply(this, config);
34 // default disabled, based on 'good practice'..
35 this.disable = this.disable || {};
36 Roo.applyIf(this.disable, {
39 specialElements : true
41 Roo.bootstrap.form.HtmlEditorToolbar.Standard.superclass.constructor.call(this, config);
43 this.editor = config.editor;
44 this.editorcore = config.editor.editorcore;
46 this.buttons = new Roo.util.MixedCollection(false, function(o) { return o.btnid; });
48 //Roo.form.HtmlEditorToolbar1.superclass.constructor.call(this, editor.wrap.dom.firstChild, [], config);
49 // dont call parent... till later.
51 Roo.extend(Roo.bootstrap.form.HtmlEditorToolbar.Standard, Roo.bootstrap.nav.Simplebar, {
61 "h1","h2","h3","h4","h5","h6",
63 "abbr", "acronym", "address", "cite", "samp", "var",
70 onRender : function(ct, position)
72 // Roo.log("Call onRender: " + this.xtype);
74 Roo.bootstrap.form.HtmlEditorToolbar.Standard.superclass.onRender.call(this, ct, position);
76 this.el.dom.style.marginBottom = '0';
78 var editorcore = this.editorcore;
79 var editor= this.editor;
82 var btn = function(id, cmd , toggle, handler, html){
84 var event = toggle ? 'toggle' : 'click';
93 cmd : cmd, // why id || cmd
94 enableToggle: toggle !== false,
96 pressed : toggle ? false : null,
99 a.listeners[toggle ? 'toggle' : 'click'] = function() {
100 handler ? handler.call(_this,this) :_this.onBtnClick.call(_this, cmd || id);
106 // var cb_box = function...
120 Roo.each(this.formats, function(f) {
121 style.menu.items.push({
124 html : '<'+ f+' style="margin:2px">'+f +'</'+ f+'>',
129 editorcore.insertTag(this.tagname);
136 children.push(style);
138 btn('bold', false,true);
139 btn('italic', false,true);
140 btn('align-left', 'justifyleft',true);
141 btn('align-center', 'justifycenter',true);
142 btn('align-right' , 'justifyright',true);
143 btn('link', false, true, this.onLinkClick);
146 btn('image', false, true, this.onImageClick);
147 btn('list','insertunorderedlist',true);
148 btn('list-ol','insertorderedlist',true);
150 btn('pencil', false,true, function(btn){
152 this.toggleSourceEdit(btn.pressed);
155 if (this.editor.btns.length > 0) {
156 for (var i = 0; i<this.editor.btns.length; i++) {
157 children.push(this.editor.btns[i]);
163 this.xtype = 'NavSimplebar'; // why?
165 for(var i=0;i< children.length;i++) {
167 this.buttons.add(this.addxtypeChild(children[i]));
170 this.buildToolbarDelete();
172 editor.on('editorevent', this.updateToolbar, this);
175 buildToolbarDelete : function()
178 /* this.addxtypeChild({
181 cls : 'roo-htmleditor-fill'
184 this.deleteBtn = this.addxtypeChild({
189 tooltip : "Clear Formating / Delete",
191 click : this.onDelete.createDelegate(this)
194 this.deleteBtn.hide();
198 onImageClick : function()
201 this.input.un('change', this.onFileSelected, this);
203 this.input = Roo.get(document.body).createChild({
206 style : 'display:none',
209 this.input.on('change', this.onFileSelected, this);
210 this.input.dom.click();
213 onFileSelected : function(e)
217 if(typeof(this.input.dom.files) == 'undefined' || !this.input.dom.files.length){
222 this.addFiles(Array.prototype.slice.call(this.input.dom.files));
225 addFiles : function(far) {
233 if (!f.type.match(/^image/)) {
238 var sn = this.selectedNode;
240 var bl = sn && this.editorcore.enableBlocks ? Roo.htmleditor.Block.factory(sn) : false;
242 var editor = this.editorcore;
244 var reader = new FileReader();
245 reader.addEventListener('load', (function() {
247 bl.image_src = reader.result;
248 //bl.caption = f.name;
249 bl.updateElement(sn);
250 editor.owner.fireEvent('editorevent', editor, false);
251 // we only do the first file!! and replace.
254 if (this.editorcore.enableBlocks) {
255 var fig = new Roo.htmleditor.BlockFigure({
256 image_src : reader.result,
258 caption_display : 'none' //default to hide captions..
260 editor.insertAtCursor(fig.toHTML());
261 editor.owner.fireEvent('editorevent', editor, false);
264 // just a standard img..
265 if (sn && sn.tagName.toUpperCase() == 'IMG') {
266 sn.src = reader.result;
267 editor.owner.fireEvent('editorevent', editor, false);
270 editor.insertAtCursor('<img src="' + reader.result +'">');
271 editor.owner.fireEvent('editorevent', editor, false);
273 }).createDelegate(this));
274 reader.readAsDataURL(f);
280 onBtnClick : function(id)
282 this.editorcore.relayCmd(id);
283 this.editorcore.focus();
286 onLinkClick : function(btn) {
287 var url = this.selectedNode && this.selectedNode.tagName.toUpperCase() == 'A' ?
288 this.selectedNode.getAttribute('href') : '';
290 Roo.bootstrap.MessageBox.show({
291 title : "Add / Edit Link URL",
292 msg : "Enter the URL for the link",
293 buttons: Roo.bootstrap.MessageBox.OKCANCEL,
300 fn: function(pressed, newurl) {
301 if (pressed != 'ok') {
302 this.editorcore.focus();
306 this.selectedNode.setAttribute('href', newurl);
309 if(newurl && newurl .match(/http(s):\/\/.+/)) {
310 this.editorcore.relayCmd('createlink', newurl);
312 this.editorcore.focus();
317 * Protected method that will not generally be called directly. It triggers
318 * a toolbar update by reading the markup state of the current selection in the editor.
320 updateToolbar: function(editor ,ev, sel){
322 if(!this.editorcore.activated){
323 this.editor.onFirstFocus(); // is this neeed?
327 var btns = this.buttons;
328 var doc = this.editorcore.doc;
329 var hasToggle = false;
330 btns.each(function(e) {
331 if (e.enableToggle && e.cmd) {
332 hasToggle = hasToggle || (['align-left', 'align-right', 'align-center'].indexOf(e.btnid) < 0 && doc.queryCommandState(e.cmd));
333 e.setActive(doc.queryCommandState(e.cmd));
339 (ev.type == 'mouseup' || ev.type == 'click' ) &&
340 ev.target && ev.target.tagName != 'BODY' ) { // && ev.target.tagName == 'IMG') {
341 // they have click on an image...
342 // let's see if we can change the selection...
347 var ans = this.editorcore.getAllAncestors();
349 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
350 sel = sel ? sel : this.editorcore.doc.body;
351 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
355 var lastSel = this.selectedNode;
356 this.selectedNode = sel;
358 // ok see if we are editing a block?
361 // you are not actually selecting the block.
362 if (sel && sel.hasAttribute('data-block')) {
364 } else if (sel && sel.closest('[data-block]')) {
365 db = sel.closest('[data-block]');
368 Array.from(this.editorcore.doc.body.querySelectorAll('.roo-ed-selection')).forEach(function(e) {
369 e.classList.remove('roo-ed-selection');
373 if (db && this.editorcore.enableBlocks) {
374 block = Roo.htmleditor.Block.factory(db);
377 db.className = (db.classList.length > 0 ? db.className + ' ' : '') +
379 sel = this.selectedNode = db;
383 // highlight the 'a'..
384 var tn = sel && sel.tagName.toUpperCase() || '';
385 if (!block && sel && tn != 'A') {
386 var asel = sel.closest('A');
392 btns.get('link').setActive(tn == 'A' && this.selectedNode.hasAttribute('href'));
394 Roo.bootstrap.menu.Manager.hideAll();
400 // handle delete button..
401 if (hasToggle || (tn.length && tn == 'BODY')) {
402 this.deleteBtn.hide();
406 this.deleteBtn.show();
410 //this.editorsyncValue();
412 onFirstFocus: function() {
413 this.buttons.each(function(item){
418 onDelete : function()
420 var range = this.editorcore.createRange();
421 var selection = this.editorcore.getSelection();
422 var sn = this.selectedNode;
423 range.setStart(sn,0);
427 if (sn.hasAttribute('data-block')) {
428 var block = Roo.htmleditor.Block.factory(this.selectedNode);
430 sn = block.removeNode();
431 sn.parentNode.removeChild(sn);
432 selection.removeAllRanges();
433 selection.addRange(range);
434 this.updateToolbar(null, null, null);
435 this.editorcore.fireEditorEvent(false);
441 return; // should not really happen..
443 if (sn && sn.tagName == 'BODY') {
446 var stn = sn.childNodes[0] || sn.nextSibling || sn.previousSibling || sn.parentNode;
448 // remove and keep parents.
449 a = new Roo.htmleditor.FilterKeepChildren({tag : false});
452 selection.removeAllRanges();
453 selection.addRange(range);
454 this.editorcore.fireEditorEvent(false);
460 toggleSourceEdit : function(sourceEditMode){
464 Roo.log("disabling buttons");
465 this.buttons.each( function(item){
466 if(item.cmd != 'pencil'){
472 Roo.log("enabling buttons");
473 if(this.editorcore.initialized){
474 this.buttons.each( function(item){
480 Roo.log("calling toggole on editor");
481 // tell the editor that it's been pressed..
482 this.editor.toggleSourceEdit(sourceEditMode);