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 = {
55 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
77 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
129 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
134 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
144 style : 'fontFamily',
145 displayField: 'display',
146 optname : 'font-family',
195 // should we really allow this??
196 // should this just be
208 style : 'fontFamily',
209 displayField: 'display',
210 optname : 'font-family',
217 style : 'fontFamily',
218 displayField: 'display',
219 optname : 'font-family',
226 style : 'fontFamily',
227 displayField: 'display',
228 optname : 'font-family',
239 // this should be configurable.. - you can either set it up using stores, or modify options somehwere..
240 Roo.form.HtmlEditor.ToolbarContext.stores = false;
242 Roo.form.HtmlEditor.ToolbarContext.options = {
244 [ 'Helvetica,Arial,sans-serif', 'Helvetica'],
245 [ 'Courier New', 'Courier New'],
246 [ 'Tahoma', 'Tahoma'],
247 [ 'Times New Roman,serif', 'Times'],
248 [ 'Verdana','Verdana' ]
252 // fixme - these need to be configurable..
255 //Roo.form.HtmlEditor.ToolbarContext.types
258 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
267 * @cfg {Object} disable List of toolbar elements to disable
272 * @cfg {Object} styles List of styles
273 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
275 * These must be defined in the page, so they get rendered correctly..
286 init : function(editor)
288 this.editor = editor;
289 this.editorcore = editor.editorcore ? editor.editorcore : editor;
290 var editorcore = this.editorcore;
292 var fid = editorcore.frameId;
294 function btn(id, toggle, handler){
295 var xid = fid + '-'+ id ;
299 cls : 'x-btn-icon x-edit-'+id,
300 enableToggle:toggle !== false,
301 scope: editorcore, // was editor...
302 handler:handler||editorcore.relayBtnCmd,
303 clickEvent:'mousedown',
304 tooltip: etb.buttonTips[id] || undefined, ///tips ???
308 // create a new element.
309 var wdiv = editor.wrap.createChild({
311 }, editor.wrap.dom.firstChild.nextSibling, true);
313 // can we do this more than once??
318 // disable everything...
319 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
324 this.toolbars[i] = this.buildToolbar(ty[i],i);
326 this.tb = this.toolbars.BODY;
330 editor.on('hide', function( ) { this.footer.hide() }, this);
331 editor.on('show', function( ) { this.footer.show() }, this);
334 this.rendered = true;
337 editor.on('editorevent', this.updateToolbar, this);
338 // other toolbars need to implement this..
339 //editor.on('editmodechange', this.updateToolbar, this);
345 * Protected method that will not generally be called directly. It triggers
346 * a toolbar update by reading the markup state of the current selection in the editor.
348 * Note you can force an update by calling on('editorevent', scope, false)
350 updateToolbar: function(editor ,ev, sel){
353 // capture mouse up - this is handy for selecting images..
354 // perhaps should go somewhere else...
355 if(!this.editorcore.activated){
356 this.editor.onFirstFocus();
362 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
363 // selectNode - might want to handle IE?
368 (ev.type == 'mouseup' || ev.type == 'click' ) &&
369 ev.target && ev.target.tagName == 'IMG') {
370 // they have click on an image...
371 // let's see if we can change the selection...
374 var nodeRange = sel.ownerDocument.createRange();
376 nodeRange.selectNode(sel);
378 nodeRange.selectNodeContents(sel);
380 //nodeRange.collapse(true);
381 var s = this.editorcore.win.getSelection();
383 s.addRange(nodeRange);
387 //var updateFooter = sel ? false : true;
390 var ans = this.editorcore.getAllAncestors();
393 var ty = Roo.form.HtmlEditor.ToolbarContext.types;
396 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
397 sel = sel ? sel : this.editorcore.doc.body;
398 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
402 var tn = sel.tagName.toUpperCase();
403 var lastSel = this.tb.selectedNode;
404 this.tb.selectedNode = sel;
407 // ok see if we are editing a block?
409 var db = Roo.get(sel).findParent('[data-block]');
412 block = Roo.htmleditor.Block.factory(db);
414 tn = 'BLOCK.' + db.getAttribute('data-block');
415 this.tb.selectedNode = db;
416 if (typeof(this.toolbars[tn]) == 'undefined') {
417 this.toolbars[tn] = this.buildToolbar( block.context,tn ,block.friendly_name);
419 left_label = block.friendly_name;
427 if (this.tb.name == tn && lastSel == this.tb.selectedNode && ev !== false) {
428 return; // no change?
434 ///console.log("show: " + tn);
435 this.tb = typeof(this.toolbars[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
439 this.tb.items.first().el.innerHTML = left_label + ': ';
444 var dbo = new cls({node : sel});
445 this.tb.fields.each(function(e) {
446 e.setValue(dob[e.attrname]);
450 } else if (this.tb.fields) {
451 this.tb.fields.each(function(e) {
453 e.setValue(sel.style[e.stylename]);
456 e.setValue(sel.getAttribute(e.attrname));
459 this.updateToolbarStyles(sel);
461 // flag our selected Node.
462 this.tb.selectedNode = sel;
465 Roo.menu.MenuMgr.hideAll();
472 this.updateFooter(ans);
476 updateToolbarStyles : function(sel)
478 var hasStyles = false;
479 for(var i in this.styles) {
486 var st = this.tb.fields.item(0);
488 st.store.removeAll();
489 var cn = sel.className.split(/\s+/);
492 if (this.styles['*']) {
494 Roo.each(this.styles['*'], function(v) {
495 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
498 if (this.styles[tn]) {
499 Roo.each(this.styles[tn], function(v) {
500 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
504 st.store.loadData(avs);
511 updateFooter : function(ans)
515 this.footDisp.dom.innerHTML = '';
519 this.footerEls = ans.reverse();
520 Roo.each(this.footerEls, function(a,i) {
522 html += html.length ? ' > ' : '';
524 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
529 var sz = this.footDisp.up('td').getSize();
530 this.footDisp.dom.style.width = (sz.width -10) + 'px';
531 this.footDisp.dom.style.marginLeft = '5px';
533 this.footDisp.dom.style.overflow = 'hidden';
535 this.footDisp.dom.innerHTML = html;
542 onDestroy : function(){
545 this.tb.items.each(function(item){
547 item.menu.removeAll();
549 item.menu.el.destroy();
557 onFirstFocus: function() {
558 // need to do this for all the toolbars..
559 this.tb.items.each(function(item){
563 buildToolbar: function(tlist, nm, friendly_name)
565 var editor = this.editor;
566 var editorcore = this.editorcore;
567 // create a new element.
568 var wdiv = editor.wrap.createChild({
570 }, editor.wrap.dom.firstChild.nextSibling, true);
573 var tb = new Roo.Toolbar(wdiv);
576 tb.add((typeof(friendly_name) == 'undefined' ? nm : friendly_name) + ": ");
578 var styles = Array.from(this.styles);
582 if (styles && styles.length) {
584 // this needs a multi-select checkbox...
585 tb.addField( new Roo.form.ComboBox({
586 store: new Roo.data.SimpleStore({
588 fields: ['val', 'selected'],
591 name : '-roo-edit-className',
592 attrname : 'className',
597 triggerAction: 'all',
598 emptyText:'Select Style',
602 'select': function(c, r, i) {
603 // initial support only for on class per el..
604 tb.selectedNode.className = r ? r.get('val') : '';
605 editorcore.syncValue();
612 var tbc = Roo.form.HtmlEditor.ToolbarContext;
615 for (var i in tlist) {
618 tb.add(item.title + ": ");
621 //optname == used so you can configure the options available..
622 var opts = item.opts ? item.opts : false;
623 if (item.optname) { // use the b
624 opts = Roo.form.HtmlEditor.ToolbarContext.options[item.optname];
629 // opts == pulldown..
630 tb.addField( new Roo.form.ComboBox({
631 store: typeof(tbc.stores[i]) != 'undefined' ? Roo.factory(tbc.stores[i],Roo.data) : new Roo.data.SimpleStore({
633 fields: ['val', 'display'],
636 name : '-roo-edit-' + i,
639 stylename : item.style ? item.style : false,
641 displayField: item.displayField ? item.displayField : 'val',
644 mode: typeof(tbc.stores[i]) != 'undefined' ? 'remote' : 'local',
646 triggerAction: 'all',
649 width: item.width ? item.width : 130,
651 'select': function(c, r, i) {
652 if (tb.selectedNode.hasClass('data-block')) {
653 var b = Roo.htmleditor.Block.factory(tb.selectedNode);
654 b[c.attrname] = r.get('val');
655 b.updateElement(tb.selectedNode);
656 editorcore.syncValue();
661 tb.selectedNode.style[c.stylename] = r.get('val');
662 editorcore.syncValue();
666 tb.selectedNode.removeAttribute(c.attrname);
667 editorcore.syncValue();
670 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
671 editorcore.syncValue();
680 tb.addField( new Roo.form.TextField({
689 tb.addField( new Roo.form.TextField({
690 name: '-roo-edit-' + i,
697 'change' : function(f, nv, ov) {
699 if (tb.selectedNode.hasClass('data-block')) {
700 var b = Roo.htmleditor.Block.factory(tb.selectedNode);
702 b.updateElement(tb.selectedNode);
703 editorcore.syncValue();
707 tb.selectedNode.setAttribute(f.attrname, nv);
708 editorcore.syncValue();
726 _this.editor.fireEvent('stylesheetsclick', _this.editor);
734 text: 'Remove', // remove the tag, and puts the children outside...
740 // undo does not work.
741 var sn = tb.selectedNode;
742 var stn = sn.childNodes[0] || sn.nextSibling || sn.previousSibling || pn;
743 if (sn.hasAttribute('data-block')) {
744 sn.parentNode.removeChild(sn);
746 // remove and keep parents.
747 a = new Roo.htmleditor.FilterKeepChildren({tag : false});
752 var range = editorcore.createRange();
754 range.setStart(stn,0);
755 range.setEnd(en,0); //????
756 var selection = editorcore.getSelection();
757 selection.removeAllRanges();
758 selection.addRange(range);
761 //_this.updateToolbar(null, null, pn);
762 _this.updateToolbar(null, null, null);
763 _this.updateFooter(false);
774 tb.el.on('click', function(e){
775 e.preventDefault(); // what does this do?
777 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
780 // dont need to disable them... as they will get hidden
785 buildFooter : function()
788 var fel = this.editor.wrap.createChild();
789 this.footer = new Roo.Toolbar(fel);
790 // toolbar has scrolly on left / right?
791 var footDisp= new Roo.Toolbar.Fill();
797 handler : function() {
798 _t.footDisp.scrollTo('left',0,true)
802 this.footer.add( footDisp );
807 handler : function() {
809 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
813 var fel = Roo.get(footDisp.el);
814 fel.addClass('x-editor-context');
815 this.footDispWrap = fel;
816 this.footDispWrap.overflow = 'hidden';
818 this.footDisp = fel.createChild();
819 this.footDispWrap.on('click', this.onContextClick, this)
823 onContextClick : function (ev,dom)
826 var cn = dom.className;
828 if (!cn.match(/x-ed-loc-/)) {
831 var n = cn.split('-').pop();
832 var ans = this.footerEls;
836 var range = this.editorcore.createRange();
838 range.selectNodeContents(sel);
839 //range.selectNode(sel);
842 var selection = this.editorcore.getSelection();
843 selection.removeAllRanges();
844 selection.addRange(range);
848 this.updateToolbar(null, null, sel);