1 // <script type="text/javascript">
5 * Copyright(c) 2006-2007, Ext JS, LLC.
12 * @class Roo.form.HtmlEditor.ToolbarContext
17 new Roo.form.HtmlEditor({
20 { xtype: 'ToolbarStandard', styles : {} }
21 { xtype: 'ToolbarContext', disable : {} }
27 * @config : {Object} disable List of elements to disable.. (not done yet.)
28 * @config : {Object} styles Map of styles available.
32 Roo.form.HtmlEditor.ToolbarContext = function(config)
35 Roo.apply(this, config);
36 //Roo.form.HtmlEditorToolbar1.superclass.constructor.call(this, editor.wrap.dom.firstChild, [], config);
37 // dont call parent... till later.
38 this.styles = this.styles || {};
43 Roo.form.HtmlEditor.ToolbarContext.types = {
58 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
84 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
155 name : 'selectoptions',
161 // should we really allow this??
162 // should this just be
179 // this should be configurable.. - you can either set it up using stores, or modify options somehwere..
180 Roo.form.HtmlEditor.ToolbarContext.stores = false;
182 Roo.form.HtmlEditor.ToolbarContext.options = {
184 [ 'Helvetica,Arial,sans-serif', 'Helvetica'],
185 [ 'Courier New', 'Courier New'],
186 [ 'Tahoma', 'Tahoma'],
187 [ 'Times New Roman,serif', 'Times'],
188 [ 'Verdana','Verdana' ]
192 // fixme - these need to be configurable..
195 //Roo.form.HtmlEditor.ToolbarContext.types
198 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
207 * @cfg {Object} disable List of toolbar elements to disable
212 * @cfg {Object} styles List of styles
213 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
215 * These must be defined in the page, so they get rendered correctly..
226 init : function(editor)
228 this.editor = editor;
229 this.editorcore = editor.editorcore ? editor.editorcore : editor;
230 var editorcore = this.editorcore;
232 var fid = editorcore.frameId;
234 function btn(id, toggle, handler){
235 var xid = fid + '-'+ id ;
239 cls : 'x-btn-icon x-edit-'+id,
240 enableToggle:toggle !== false,
241 scope: editorcore, // was editor...
242 handler:handler||editorcore.relayBtnCmd,
243 clickEvent:'mousedown',
244 tooltip: etb.buttonTips[id] || undefined, ///tips ???
248 // create a new element.
249 var wdiv = editor.wrap.createChild({
251 }, editor.wrap.dom.firstChild.nextSibling, true);
253 // can we do this more than once??
258 // disable everything...
259 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
264 this.toolbars[i] = this.buildToolbar(ty[i],i);
266 this.tb = this.toolbars.BODY;
270 editor.on('hide', function( ) { this.footer.hide() }, this);
271 editor.on('show', function( ) { this.footer.show() }, this);
274 this.rendered = true;
277 editor.on('editorevent', this.updateToolbar, this);
278 // other toolbars need to implement this..
279 //editor.on('editmodechange', this.updateToolbar, this);
285 * Protected method that will not generally be called directly. It triggers
286 * a toolbar update by reading the markup state of the current selection in the editor.
288 * Note you can force an update by calling on('editorevent', scope, false)
290 updateToolbar: function(editor ,ev, sel)
294 ev.stopEvent(); // se if we can stop this looping with mutiple events.
298 // capture mouse up - this is handy for selecting images..
299 // perhaps should go somewhere else...
300 if(!this.editorcore.activated){
301 this.editor.onFirstFocus();
304 Roo.log(ev ? ev.target : 'NOTARGET');
307 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
308 // selectNode - might want to handle IE?
313 (ev.type == 'mouseup' || ev.type == 'click' ) &&
314 ev.target && ev.target != 'BODY' ) { // && ev.target.tagName == 'IMG') {
315 // they have click on an image...
316 // let's see if we can change the selection...
319 // this triggers looping?
320 //this.editorcore.selectNode(sel);
325 //var updateFooter = sel ? false : true;
328 var ans = this.editorcore.getAllAncestors();
331 var ty = Roo.form.HtmlEditor.ToolbarContext.types;
334 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
335 sel = sel ? sel : this.editorcore.doc.body;
336 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
340 var tn = sel.tagName.toUpperCase();
341 var lastSel = this.tb.selectedNode;
342 this.tb.selectedNode = sel;
345 // ok see if we are editing a block?
346 var sel_el = Roo.get(sel);
348 // you are not actually selecting the block.
349 if (sel && sel.hasAttribute('data-block')) {
351 } else if (sel && !sel.hasAttribute('contenteditable')) {
352 db = sel_el.findParent('[data-block]');
353 var cepar = sel_el.findParent('[contenteditable=true]');
354 if (db && cepar && cepar.tagName != 'BODY') {
355 db = false; // we are inside an editable block.. = not sure how we are going to handle nested blocks!?
361 //if (db && !sel.hasAttribute('contenteditable') && sel.getAttribute('contenteditable') != 'true' ) {
363 block = Roo.htmleditor.Block.factory(db);
365 tn = 'BLOCK.' + db.getAttribute('data-block');
367 //this.editorcore.selectNode(db);
368 if (typeof(this.toolbars[tn]) == 'undefined') {
369 this.toolbars[tn] = this.buildToolbar( false ,tn ,block.friendly_name, block);
371 this.toolbars[tn].selectedNode = db;
372 left_label = block.friendly_name;
373 ans = this.editorcore.getAllAncestors();
381 if (this.tb.name == tn && lastSel == this.tb.selectedNode && ev !== false) {
382 return; // no change?
388 ///console.log("show: " + tn);
389 this.tb = typeof(this.toolbars[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
393 this.tb.items.first().el.innerHTML = left_label + ': ';
399 this.tb.fields.each(function(e) {
400 e.setValue(block[e.name]);
404 } else if (this.tb.fields && this.tb.selectedNode) {
405 this.tb.fields.each( function(e) {
407 e.setValue(this.tb.selectedNode.style[e.stylename]);
410 e.setValue(this.tb.selectedNode.getAttribute(e.attrname));
412 this.updateToolbarStyles(this.tb.selectedNode);
417 Roo.menu.MenuMgr.hideAll();
424 this.updateFooter(ans);
428 updateToolbarStyles : function(sel)
430 var hasStyles = false;
431 for(var i in this.styles) {
437 if (hasStyles && this.tb.hasStyles) {
438 var st = this.tb.fields.item(0);
440 st.store.removeAll();
441 var cn = sel.className.split(/\s+/);
444 if (this.styles['*']) {
446 Roo.each(this.styles['*'], function(v) {
447 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
450 if (this.styles[tn]) {
451 Roo.each(this.styles[tn], function(v) {
452 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
456 st.store.loadData(avs);
463 updateFooter : function(ans)
467 this.footDisp.dom.innerHTML = '';
471 this.footerEls = ans.reverse();
472 Roo.each(this.footerEls, function(a,i) {
474 html += html.length ? ' > ' : '';
476 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
481 var sz = this.footDisp.up('td').getSize();
482 this.footDisp.dom.style.width = (sz.width -10) + 'px';
483 this.footDisp.dom.style.marginLeft = '5px';
485 this.footDisp.dom.style.overflow = 'hidden';
487 this.footDisp.dom.innerHTML = html;
494 onDestroy : function(){
497 this.tb.items.each(function(item){
499 item.menu.removeAll();
501 item.menu.el.destroy();
509 onFirstFocus: function() {
510 // need to do this for all the toolbars..
511 this.tb.items.each(function(item){
515 buildToolbar: function(tlist, nm, friendly_name, block)
517 var editor = this.editor;
518 var editorcore = this.editorcore;
519 // create a new element.
520 var wdiv = editor.wrap.createChild({
522 }, editor.wrap.dom.firstChild.nextSibling, true);
525 var tb = new Roo.Toolbar(wdiv);
527 if (tlist === false && block) {
528 tlist = block.contextMenu(this);
531 tb.hasStyles = false;
534 tb.add((typeof(friendly_name) == 'undefined' ? nm : friendly_name) + ": ");
536 var styles = Array.from(this.styles);
540 if (styles && styles.length) {
542 // this needs a multi-select checkbox...
543 tb.addField( new Roo.form.ComboBox({
544 store: new Roo.data.SimpleStore({
546 fields: ['val', 'selected'],
549 name : '-roo-edit-className',
550 attrname : 'className',
555 triggerAction: 'all',
556 emptyText:'Select Style',
560 'select': function(c, r, i) {
561 // initial support only for on class per el..
562 tb.selectedNode.className = r ? r.get('val') : '';
563 editorcore.syncValue();
570 var tbc = Roo.form.HtmlEditor.ToolbarContext;
573 for (var i = 0; i < tlist.length; i++) {
575 // newer versions will use xtype cfg to create menus.
576 if (typeof(tlist[i].xtype) != 'undefined') {
578 tb[typeof(tlist[i].name)== 'undefined' ? 'add' : 'addField'](Roo.factory(tlist[i]));
585 tb.add(item.title + ": ");
588 //optname == used so you can configure the options available..
589 var opts = item.opts ? item.opts : false;
590 if (item.optname) { // use the b
591 opts = Roo.form.HtmlEditor.ToolbarContext.options[item.optname];
596 // opts == pulldown..
597 tb.addField( new Roo.form.ComboBox({
598 store: typeof(tbc.stores[i]) != 'undefined' ? Roo.factory(tbc.stores[i],Roo.data) : new Roo.data.SimpleStore({
600 fields: ['val', 'display'],
603 name : '-roo-edit-' + tlist[i].name,
605 attrname : tlist[i].name,
606 stylename : item.style ? item.style : false,
608 displayField: item.displayField ? item.displayField : 'val',
611 mode: typeof(tbc.stores[tlist[i].name]) != 'undefined' ? 'remote' : 'local',
613 triggerAction: 'all',
616 width: item.width ? item.width : 130,
618 'select': function(c, r, i) {
619 if (tb.selectedNode.hasAttribute('data-block')) {
620 var b = Roo.htmleditor.Block.factory(tb.selectedNode);
621 b[c.attrname] = r.get('val');
622 b.updateElement(tb.selectedNode);
623 editorcore.syncValue();
628 tb.selectedNode.style[c.stylename] = r.get('val');
629 editorcore.syncValue();
633 tb.selectedNode.removeAttribute(c.attrname);
634 editorcore.syncValue();
637 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
638 editorcore.syncValue();
647 tb.addField( new Roo.form.TextField({
656 tb.addField( new Roo.form.TextField({
657 name: '-roo-edit-' + tlist[i].name,
658 attrname : tlist[i].name,
664 'change' : function(f, nv, ov) {
666 if (tb.selectedNode.hasAttribute('data-block')) {
667 var b = Roo.htmleditor.Block.factory(tb.selectedNode);
669 b.updateElement(tb.selectedNode);
670 editorcore.syncValue();
674 tb.selectedNode.setAttribute(f.attrname, nv);
675 editorcore.syncValue();
693 _this.editor.fireEvent('stylesheetsclick', _this.editor);
701 text: 'Remove Block or Formating', // remove the tag, and puts the children outside...
707 sn = Roo.htmleditor.Block.factory(tb.selectedNode).removalNode();
712 // undo does not work.
713 var sn = tb.selectedNode;
718 var stn = sn.childNodes[0] || sn.nextSibling || sn.previousSibling || sn.parentNode;
719 if (sn.hasAttribute('data-block')) {
720 stn = sn.nextSibling || sn.previousSibling || sn.parentNode;
721 sn.parentNode.removeChild(sn);
723 } else if (sn && sn.tagName != 'BODY') {
724 // remove and keep parents.
725 a = new Roo.htmleditor.FilterKeepChildren({tag : false});
730 var range = editorcore.createRange();
732 range.setStart(stn,0);
734 var selection = editorcore.getSelection();
735 selection.removeAllRanges();
736 selection.addRange(range);
739 //_this.updateToolbar(null, null, pn);
740 _this.updateToolbar(null, null, null);
741 _this.updateFooter(false);
752 tb.el.on('click', function(e){
753 e.preventDefault(); // what does this do?
755 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
758 // dont need to disable them... as they will get hidden
763 buildFooter : function()
766 var fel = this.editor.wrap.createChild();
767 this.footer = new Roo.Toolbar(fel);
768 // toolbar has scrolly on left / right?
769 var footDisp= new Roo.Toolbar.Fill();
775 handler : function() {
776 _t.footDisp.scrollTo('left',0,true)
780 this.footer.add( footDisp );
785 handler : function() {
787 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
791 var fel = Roo.get(footDisp.el);
792 fel.addClass('x-editor-context');
793 this.footDispWrap = fel;
794 this.footDispWrap.overflow = 'hidden';
796 this.footDisp = fel.createChild();
797 this.footDispWrap.on('click', this.onContextClick, this)
801 // when the footer contect changes
802 onContextClick : function (ev,dom)
805 var cn = dom.className;
807 if (!cn.match(/x-ed-loc-/)) {
810 var n = cn.split('-').pop();
811 var ans = this.footerEls;
815 var range = this.editorcore.createRange();
817 range.selectNodeContents(sel);
818 //range.selectNode(sel);
821 var selection = this.editorcore.getSelection();
822 selection.removeAllRanges();
823 selection.addRange(range);
827 this.updateToolbar(null, null, sel);