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 cls : 'roo-html-editor-btn-' + id,
94 cmd : cmd, // why id || cmd
95 enableToggle: toggle !== false,
97 pressed : toggle ? false : null,
100 a.listeners[toggle ? 'toggle' : 'click'] = function() {
101 handler ? handler.call(_this,this) :_this.onBtnClick.call(_this, cmd || id);
107 // var cb_box = function...
114 cls : 'roo-html-editor-font-chooser',
122 Roo.each(this.formats, function(f) {
123 style.menu.items.push({
126 html : '<'+ f+' style="margin:2px">'+f +'</'+ f+'>',
131 editorcore.insertTag(this.tagname);
138 children.push(style);
140 btn('bold', 'bold',true);
141 btn('italic', 'italic',true);
142 btn('underline', 'underline',true);
143 btn('align-left', 'justifyleft',true);
144 btn('align-center', 'justifycenter',true);
145 btn('align-right' , 'justifyright',true);
146 btn('link', false, true, this.onLinkClick);
149 btn('image', false, true, this.onImageClick);
150 btn('list','insertunorderedlist',true);
151 btn('list-ol','insertorderedlist',true);
153 btn('pencil', false,true, function(btn){
155 this.toggleSourceEdit(btn.pressed);
158 if (this.editor.btns.length > 0) {
159 for (var i = 0; i<this.editor.btns.length; i++) {
160 children.push(this.editor.btns[i]);
166 this.xtype = 'NavSimplebar'; // why?
168 for(var i=0;i< children.length;i++) {
170 this.buttons.add(this.addxtypeChild(children[i]));
173 this.buildToolbarDelete();
175 editor.on('editorevent', this.updateToolbar, this);
178 buildToolbarDelete : function()
181 /* this.addxtypeChild({
184 cls : 'roo-htmleditor-fill'
187 this.deleteBtn = this.addxtypeChild({
193 click : this.onDelete.createDelegate(this)
196 this.deleteBtn.hide();
200 onImageClick : function()
203 this.input.un('change', this.onFileSelected, this);
205 this.input = Roo.get(document.body).createChild({
208 style : 'display:none',
211 this.input.on('change', this.onFileSelected, this);
212 this.input.dom.click();
215 onFileSelected : function(e)
219 if(typeof(this.input.dom.files) == 'undefined' || !this.input.dom.files.length){
224 this.addFiles(Array.prototype.slice.call(this.input.dom.files));
227 addFiles : function(far) {
235 if (!f.type.match(/^image/)) {
240 var sn = this.selectedNode;
242 var bl = sn && this.editorcore.enableBlocks ? Roo.htmleditor.Block.factory(sn) : false;
244 var editor = this.editorcore;
246 var reader = new FileReader();
247 reader.addEventListener('load', (function() {
249 bl.image_src = reader.result;
250 //bl.caption = f.name;
251 bl.updateElement(sn);
252 editor.owner.fireEvent('editorevent', editor, false);
253 // we only do the first file!! and replace.
256 if (this.editorcore.enableBlocks) {
257 var fig = new Roo.htmleditor.BlockFigure({
258 image_src : reader.result,
260 caption_display : 'none' //default to hide captions..
262 editor.insertAtCursor(fig.toHTML());
263 editor.owner.fireEvent('editorevent', editor, false);
266 // just a standard img..
267 if (sn && sn.tagName.toUpperCase() == 'IMG') {
268 sn.src = reader.result;
269 editor.owner.fireEvent('editorevent', editor, false);
272 editor.insertAtCursor('<img src="' + reader.result +'">');
273 editor.owner.fireEvent('editorevent', editor.owner, false);
274 editor.owner.fireEvent('imageadd', editor.owner, false);
276 }).createDelegate(this));
277 reader.readAsDataURL(f);
283 onBtnClick : function(id)
285 this.editorcore.relayCmd(id);
286 this.editorcore.focus();
289 onLinkClick : function(btn) {
290 var url = this.selectedNode && this.selectedNode.tagName.toUpperCase() == 'A' ?
291 this.selectedNode.getAttribute('href') : '';
293 Roo.bootstrap.MessageBox.show({
294 title : "Add / Edit Link URL",
295 msg : "Enter the URL for the link",
296 buttons: Roo.bootstrap.MessageBox.OKCANCEL,
303 fn: function(pressed, newurl) {
304 if (pressed != 'ok') {
305 this.editorcore.focus();
309 this.selectedNode.setAttribute('href', newurl);
312 if(newurl && newurl .match(/http(s):\/\/.+/)) {
313 this.editorcore.relayCmd('createlink', newurl);
315 this.editorcore.focus();
320 * Protected method that will not generally be called directly. It triggers
321 * a toolbar update by reading the markup state of the current selection in the editor.
323 updateToolbar: function(editor ,ev, sel){
325 if(!this.editorcore.activated){
326 this.editor.onFirstFocus(); // is this neeed?
330 var btns = this.buttons;
331 var doc = this.editorcore.doc;
332 var hasToggle = false;
333 btns.each(function(e) {
334 if (e.enableToggle && e.cmd) {
335 hasToggle = hasToggle || (['align-left', 'align-right', 'align-center', 'image' , 'link', 'underline'].indexOf(e.btnid) < 0 && doc.queryCommandState(e.cmd));
336 e.setActive(doc.queryCommandState(e.cmd));
342 (ev.type == 'mouseup' || ev.type == 'click' ) &&
343 ev.target && ev.target.tagName != 'BODY' ) { // && ev.target.tagName == 'IMG') {
344 // they have click on an image...
345 // let's see if we can change the selection...
350 var ans = this.editorcore.getAllAncestors();
352 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
353 sel = sel ? sel : this.editorcore.doc.body;
354 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
358 var lastSel = this.selectedNode;
359 this.selectedNode = sel;
361 // ok see if we are editing a block?
364 // you are not actually selecting the block.
365 if (sel && sel.hasAttribute('data-block')) {
367 } else if (sel && sel.closest('[data-block]')) {
368 db = sel.closest('[data-block]');
371 Array.from(this.editorcore.doc.body.querySelectorAll('.roo-ed-selection')).forEach(function(e) {
372 e.classList.remove('roo-ed-selection');
376 if (db && this.editorcore.enableBlocks) {
377 block = Roo.htmleditor.Block.factory(db);
380 db.className = (db.classList.length > 0 ? db.className + ' ' : '') +
382 sel = this.selectedNode = db;
386 // highlight the 'a'..
387 var tn = sel && sel.tagName.toUpperCase() || '';
388 if (!block && sel && tn != 'A') {
389 var asel = sel.closest('A');
395 btns.get('link').setActive(tn == 'A' && this.selectedNode.hasAttribute('href'));
396 btns.get('image').setActive(tn == 'IMG' || this.editorcore.enableBlocks && tn == 'FIGURE');
397 btns.get('underline').setActive(tn == 'U' || sel.closest('u') ? true : false);
399 Roo.bootstrap.menu.Manager.hideAll();
405 // handle delete button..
406 if (hasToggle || (tn.length && tn == 'BODY')) {
407 this.deleteBtn.hide();
411 this.deleteBtn.show();
415 //this.editorsyncValue();
417 onFirstFocus: function() {
418 this.buttons.each(function(item){
423 onDelete : function()
425 var range = this.editorcore.createRange();
426 var selection = this.editorcore.getSelection();
427 var sn = this.selectedNode;
428 range.setStart(sn,0);
432 if (sn.hasAttribute('data-block')) {
433 var block = Roo.htmleditor.Block.factory(this.selectedNode);
435 sn = block.removeNode();
436 sn.parentNode.removeChild(sn);
437 selection.removeAllRanges();
438 selection.addRange(range);
439 this.updateToolbar(null, null, null);
440 this.editorcore.fireEditorEvent(false);
446 return; // should not really happen..
448 if (sn && sn.tagName == 'BODY') {
451 var stn = sn.childNodes[0] || sn.nextSibling || sn.previousSibling || sn.parentNode;
453 // remove and keep parents.
454 a = new Roo.htmleditor.FilterKeepChildren({tag : false});
457 selection.removeAllRanges();
458 selection.addRange(range);
459 this.editorcore.fireEditorEvent(false);
465 toggleSourceEdit : function(sourceEditMode){
469 Roo.log("disabling buttons");
470 this.buttons.each( function(item){
471 if(item.cmd != 'pencil'){
477 Roo.log("enabling buttons");
478 if(this.editorcore.initialized){
479 this.buttons.each( function(item){
485 Roo.log("calling toggole on editor");
486 // tell the editor that it's been pressed..
487 this.editor.toggleSourceEdit(sourceEditMode);