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
190 Roo.form.HtmlEditor.ToolbarContext[ 'font-family'] = {
192 style : 'fontFamily',
198 [ 'Times New Roman'],
205 Roo.each([ 'SPAN', 'DIV' , 'TD' , 'P' ], function(tg) {
206 var cx= Roo.form.HtmlEditor.ToolbarContext;
207 if (typeof(cx.types[tg]) == 'undefined') {
211 cx.types[tg]['font-family'] = cx[ 'font-family'] ;
214 Roo.form.HtmlEditor.ToolbarContext.types
217 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
225 * @cfg {Object} disable List of toolbar elements to disable
230 * @cfg {Object} styles List of styles
231 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
233 * These must be defined in the page, so they get rendered correctly..
244 init : function(editor)
246 this.editor = editor;
249 var fid = editor.frameId;
251 function btn(id, toggle, handler){
252 var xid = fid + '-'+ id ;
256 cls : 'x-btn-icon x-edit-'+id,
257 enableToggle:toggle !== false,
258 scope: editor, // was editor...
259 handler:handler||editor.relayBtnCmd,
260 clickEvent:'mousedown',
261 tooltip: etb.buttonTips[id] || undefined, ///tips ???
265 // create a new element.
266 var wdiv = editor.wrap.createChild({
268 }, editor.wrap.dom.firstChild.nextSibling, true);
270 // can we do this more than once??
275 // disable everything...
276 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
281 this.toolbars[i] = this.buildToolbar(ty[i],i);
283 this.tb = this.toolbars.BODY;
287 editor.on('hide', function( ) { this.footer.hide() }, this);
288 editor.on('show', function( ) { this.footer.show() }, this);
291 this.rendered = true;
294 editor.on('editorevent', this.updateToolbar, this);
295 // other toolbars need to implement this..
296 //editor.on('editmodechange', this.updateToolbar, this);
302 * Protected method that will not generally be called directly. It triggers
303 * a toolbar update by reading the markup state of the current selection in the editor.
305 updateToolbar: function(editor,ev,sel){
308 // capture mouse up - this is handy for selecting images..
309 // perhaps should go somewhere else...
310 if(!this.editor.activated){
311 this.editor.onFirstFocus();
315 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
316 // selectNode - might want to handle IE?
318 (ev.type == 'mouseup' || ev.type == 'click' ) &&
319 ev.target && ev.target.tagName == 'IMG') {
320 // they have click on an image...
321 // let's see if we can change the selection...
324 var nodeRange = sel.ownerDocument.createRange();
326 nodeRange.selectNode(sel);
328 nodeRange.selectNodeContents(sel);
330 //nodeRange.collapse(true);
331 var s = editor.win.getSelection();
333 s.addRange(nodeRange);
337 var updateFooter = sel ? false : true;
340 var ans = this.editor.getAllAncestors();
343 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
346 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
347 sel = sel ? sel : this.editor.doc.body;
348 sel = sel.tagName.length ? sel : this.editor.doc.body;
351 // pick a menu that exists..
352 var tn = sel.tagName.toUpperCase();
353 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
355 tn = sel.tagName.toUpperCase();
357 var lastSel = this.tb.selectedNode
359 this.tb.selectedNode = sel;
361 // if current menu does not match..
362 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
365 ///console.log("show: " + tn);
366 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
369 this.tb.items.first().el.innerHTML = tn + ': ';
373 if (this.tb.fields) {
374 this.tb.fields.each(function(e) {
376 e.setValue(sel.style[e.stylename]);
380 e.setValue(sel.getAttribute(e.attrname));
384 var hasStyles = false;
385 for(var i in this.styles) {
392 var st = this.tb.fields.item(0);
394 st.store.removeAll();
397 var cn = sel.className.split(/\s+/);
400 if (this.styles['*']) {
402 Roo.each(this.styles['*'], function(v) {
403 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
406 if (this.styles[tn]) {
407 Roo.each(this.styles[tn], function(v) {
408 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
412 st.store.loadData(avs);
416 // flag our selected Node.
417 this.tb.selectedNode = sel;
420 Roo.menu.MenuMgr.hideAll();
425 //this.footDisp.dom.innerHTML = '';
432 this.footerEls = ans.reverse();
433 Roo.each(this.footerEls, function(a,i) {
435 html += html.length ? ' > ' : '';
437 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
442 var sz = this.footDisp.up('td').getSize();
443 this.footDisp.dom.style.width = (sz.width -10) + 'px';
444 this.footDisp.dom.style.marginLeft = '5px';
446 this.footDisp.dom.style.overflow = 'hidden';
448 this.footDisp.dom.innerHTML = html;
450 //this.editorsyncValue();
457 onDestroy : function(){
460 this.tb.items.each(function(item){
462 item.menu.removeAll();
464 item.menu.el.destroy();
472 onFirstFocus: function() {
473 // need to do this for all the toolbars..
474 this.tb.items.each(function(item){
478 buildToolbar: function(tlist, nm)
480 var editor = this.editor;
481 // create a new element.
482 var wdiv = editor.wrap.createChild({
484 }, editor.wrap.dom.firstChild.nextSibling, true);
487 var tb = new Roo.Toolbar(wdiv);
490 tb.add(nm+ ": ");
493 for(var i in this.styles) {
498 if (styles && styles.length) {
500 // this needs a multi-select checkbox...
501 tb.addField( new Roo.form.ComboBox({
502 store: new Roo.data.SimpleStore({
504 fields: ['val', 'selected'],
507 name : '-roo-edit-className',
508 attrname : 'className',
513 triggerAction: 'all',
514 emptyText:'Select Style',
518 'select': function(c, r, i) {
519 // initial support only for on class per el..
520 tb.selectedNode.className = r ? r.get('val') : '';
530 for (var i in tlist) {
533 tb.add(item.title + ": ");
539 // opts == pulldown..
540 tb.addField( new Roo.form.ComboBox({
541 store: new Roo.data.SimpleStore({
546 name : '-roo-edit-' + i,
548 stylename : item.style ? item.style : false,
553 triggerAction: 'all',
556 width: item.width ? item.width : 130,
558 'select': function(c, r, i) {
559 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
568 tb.addField( new Roo.form.TextField({
576 tb.addField( new Roo.form.TextField({
577 name: '-roo-edit-' + i,
584 'change' : function(f, nv, ov) {
585 tb.selectedNode.setAttribute(f.attrname, nv);
600 // undo does not work.
602 var sn = tb.selectedNode;
604 var pn = sn.parentNode;
606 var stn = sn.childNodes[0];
607 var en = sn.childNodes[sn.childNodes.length - 1 ];
608 while (sn.childNodes.length) {
609 var node = sn.childNodes[0];
610 sn.removeChild(node);
612 pn.insertBefore(node, sn);
616 var range = editor.createRange();
618 range.setStart(stn,0);
619 range.setEnd(en,0); //????
620 //range.selectNode(sel);
623 var selection = editor.getSelection();
624 selection.removeAllRanges();
625 selection.addRange(range);
629 //_this.updateToolbar(null, null, pn);
630 _this.updateToolbar(null, null, null);
631 this.footDisp.dom.innerHTML = '';
641 tb.el.on('click', function(e){
642 e.preventDefault(); // what does this do?
644 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
647 // dont need to disable them... as they will get hidden
652 buildFooter : function()
655 var fel = this.editor.wrap.createChild();
656 this.footer = new Roo.Toolbar(fel);
657 // toolbar has scrolly on left / right?
658 var footDisp= new Roo.Toolbar.Fill();
664 handler : function() {
665 _t.footDisp.scrollTo('left',0,true)
669 this.footer.add( footDisp );
674 handler : function() {
676 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
680 var fel = Roo.get(footDisp.el);
681 fel.addClass('x-editor-context');
682 this.footDispWrap = fel;
683 this.footDispWrap.overflow = 'hidden';
685 this.footDisp = fel.createChild();
686 this.footDispWrap.on('click', this.onContextClick, this)
690 onContextClick : function (ev,dom)
693 var cn = dom.className;
695 if (!cn.match(/x-ed-loc-/)) {
698 var n = cn.split('-').pop();
699 var ans = this.footerEls;
703 var range = this.editor.createRange();
705 range.selectNodeContents(sel);
706 //range.selectNode(sel);
709 var selection = this.editor.getSelection();
710 selection.removeAllRanges();
711 selection.addRange(range);
715 this.updateToolbar(null, null, sel);