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"]],
117 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
122 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
176 // should we really allow this??
177 // should this just be
191 // fixme - these need to be configurable..
193 Roo.form.HtmlEditor.ToolbarContext[ 'font-family'] = {
195 style : 'fontFamily',
196 displayField: 'display',
199 [ 'helvetica,Arial,sans-serif', 'Helvetica'],
200 [ 'Courier New', 'Courier New'],
201 [ 'Tahoma', 'Tahoma'],
202 [ 'Times New Roman,serif', 'Times'],
203 [ 'Verdana','Verdana' ]
209 Roo.each([ 'SPAN', 'DIV' , 'TD' , 'P' ], function(tg) {
210 var cx= Roo.form.HtmlEditor.ToolbarContext;
211 if (typeof(cx.types[tg]) == 'undefined') {
215 cx.types[tg]['font-family'] = cx[ 'font-family'] ;
218 Roo.form.HtmlEditor.ToolbarContext.types
221 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
229 * @cfg {Object} disable List of toolbar elements to disable
234 * @cfg {Object} styles List of styles
235 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
237 * These must be defined in the page, so they get rendered correctly..
248 init : function(editor)
250 this.editor = editor;
253 var fid = editor.frameId;
255 function btn(id, toggle, handler){
256 var xid = fid + '-'+ id ;
260 cls : 'x-btn-icon x-edit-'+id,
261 enableToggle:toggle !== false,
262 scope: editor, // was editor...
263 handler:handler||editor.relayBtnCmd,
264 clickEvent:'mousedown',
265 tooltip: etb.buttonTips[id] || undefined, ///tips ???
269 // create a new element.
270 var wdiv = editor.wrap.createChild({
272 }, editor.wrap.dom.firstChild.nextSibling, true);
274 // can we do this more than once??
279 // disable everything...
280 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
285 this.toolbars[i] = this.buildToolbar(ty[i],i);
287 this.tb = this.toolbars.BODY;
291 editor.on('hide', function( ) { this.footer.hide() }, this);
292 editor.on('show', function( ) { this.footer.show() }, this);
295 this.rendered = true;
298 editor.on('editorevent', this.updateToolbar, this);
299 // other toolbars need to implement this..
300 //editor.on('editmodechange', this.updateToolbar, this);
306 * Protected method that will not generally be called directly. It triggers
307 * a toolbar update by reading the markup state of the current selection in the editor.
309 updateToolbar: function(editor,ev,sel){
312 // capture mouse up - this is handy for selecting images..
313 // perhaps should go somewhere else...
314 if(!this.editor.activated){
315 this.editor.onFirstFocus();
319 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
320 // selectNode - might want to handle IE?
322 (ev.type == 'mouseup' || ev.type == 'click' ) &&
323 ev.target && ev.target.tagName == 'IMG') {
324 // they have click on an image...
325 // let's see if we can change the selection...
328 var nodeRange = sel.ownerDocument.createRange();
330 nodeRange.selectNode(sel);
332 nodeRange.selectNodeContents(sel);
334 //nodeRange.collapse(true);
335 var s = editor.win.getSelection();
337 s.addRange(nodeRange);
341 var updateFooter = sel ? false : true;
344 var ans = this.editor.getAllAncestors();
347 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
350 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
351 sel = sel ? sel : this.editor.doc.body;
352 sel = sel.tagName.length ? sel : this.editor.doc.body;
355 // pick a menu that exists..
356 var tn = sel.tagName.toUpperCase();
357 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
359 tn = sel.tagName.toUpperCase();
361 var lastSel = this.tb.selectedNode
363 this.tb.selectedNode = sel;
365 // if current menu does not match..
366 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
369 ///console.log("show: " + tn);
370 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
373 this.tb.items.first().el.innerHTML = tn + ': ';
377 if (this.tb.fields) {
378 this.tb.fields.each(function(e) {
380 e.setValue(sel.style[e.stylename]);
383 e.setValue(sel.getAttribute(e.attrname));
387 var hasStyles = false;
388 for(var i in this.styles) {
395 var st = this.tb.fields.item(0);
397 st.store.removeAll();
400 var cn = sel.className.split(/\s+/);
403 if (this.styles['*']) {
405 Roo.each(this.styles['*'], function(v) {
406 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
409 if (this.styles[tn]) {
410 Roo.each(this.styles[tn], function(v) {
411 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
415 st.store.loadData(avs);
419 // flag our selected Node.
420 this.tb.selectedNode = sel;
423 Roo.menu.MenuMgr.hideAll();
428 //this.footDisp.dom.innerHTML = '';
435 this.footerEls = ans.reverse();
436 Roo.each(this.footerEls, function(a,i) {
438 html += html.length ? ' > ' : '';
440 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
445 var sz = this.footDisp.up('td').getSize();
446 this.footDisp.dom.style.width = (sz.width -10) + 'px';
447 this.footDisp.dom.style.marginLeft = '5px';
449 this.footDisp.dom.style.overflow = 'hidden';
451 this.footDisp.dom.innerHTML = html;
453 //this.editorsyncValue();
460 onDestroy : function(){
463 this.tb.items.each(function(item){
465 item.menu.removeAll();
467 item.menu.el.destroy();
475 onFirstFocus: function() {
476 // need to do this for all the toolbars..
477 this.tb.items.each(function(item){
481 buildToolbar: function(tlist, nm)
483 var editor = this.editor;
484 // create a new element.
485 var wdiv = editor.wrap.createChild({
487 }, editor.wrap.dom.firstChild.nextSibling, true);
490 var tb = new Roo.Toolbar(wdiv);
493 tb.add(nm+ ": ");
496 for(var i in this.styles) {
501 if (styles && styles.length) {
503 // this needs a multi-select checkbox...
504 tb.addField( new Roo.form.ComboBox({
505 store: new Roo.data.SimpleStore({
507 fields: ['val', 'selected'],
510 name : '-roo-edit-className',
511 attrname : 'className',
516 triggerAction: 'all',
517 emptyText:'Select Style',
521 'select': function(c, r, i) {
522 // initial support only for on class per el..
523 tb.selectedNode.className = r ? r.get('val') : '';
533 for (var i in tlist) {
536 tb.add(item.title + ": ");
542 // opts == pulldown..
543 tb.addField( new Roo.form.ComboBox({
544 store: new Roo.data.SimpleStore({
546 fields: ['val', 'display'],
549 name : '-roo-edit-' + i,
551 stylename : item.style ? item.style : false,
552 displayField: item.displayField ? item.displayField : 'val',
557 triggerAction: 'all',
560 width: item.width ? item.width : 130,
562 'select': function(c, r, i) {
564 tb.selectedNode.style[c.stylename] = r.get('val');
567 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
576 tb.addField( new Roo.form.TextField({
584 tb.addField( new Roo.form.TextField({
585 name: '-roo-edit-' + i,
592 'change' : function(f, nv, ov) {
593 tb.selectedNode.setAttribute(f.attrname, nv);
608 // undo does not work.
610 var sn = tb.selectedNode;
612 var pn = sn.parentNode;
614 var stn = sn.childNodes[0];
615 var en = sn.childNodes[sn.childNodes.length - 1 ];
616 while (sn.childNodes.length) {
617 var node = sn.childNodes[0];
618 sn.removeChild(node);
620 pn.insertBefore(node, sn);
624 var range = editor.createRange();
626 range.setStart(stn,0);
627 range.setEnd(en,0); //????
628 //range.selectNode(sel);
631 var selection = editor.getSelection();
632 selection.removeAllRanges();
633 selection.addRange(range);
637 //_this.updateToolbar(null, null, pn);
638 _this.updateToolbar(null, null, null);
639 this.footDisp.dom.innerHTML = '';
649 tb.el.on('click', function(e){
650 e.preventDefault(); // what does this do?
652 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
655 // dont need to disable them... as they will get hidden
660 buildFooter : function()
663 var fel = this.editor.wrap.createChild();
664 this.footer = new Roo.Toolbar(fel);
665 // toolbar has scrolly on left / right?
666 var footDisp= new Roo.Toolbar.Fill();
672 handler : function() {
673 _t.footDisp.scrollTo('left',0,true)
677 this.footer.add( footDisp );
682 handler : function() {
684 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
688 var fel = Roo.get(footDisp.el);
689 fel.addClass('x-editor-context');
690 this.footDispWrap = fel;
691 this.footDispWrap.overflow = 'hidden';
693 this.footDisp = fel.createChild();
694 this.footDispWrap.on('click', this.onContextClick, this)
698 onContextClick : function (ev,dom)
701 var cn = dom.className;
703 if (!cn.match(/x-ed-loc-/)) {
706 var n = cn.split('-').pop();
707 var ans = this.footerEls;
711 var range = this.editor.createRange();
713 range.selectNodeContents(sel);
714 //range.selectNode(sel);
717 var selection = this.editor.getSelection();
718 selection.removeAllRanges();
719 selection.addRange(range);
723 this.updateToolbar(null, null, sel);