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',
193 displayField: 'display',
196 [ 'helvetica,Arial,sans-serif', 'Helvetica'],
197 [ 'Courier New', 'Courier New'],
198 [ 'Tahoma', 'Tahoma'],
199 [ 'Times New Roman,serif', 'Times'],
200 [ 'Verdana','Verdana' ]
206 Roo.each([ 'SPAN', 'DIV' , 'TD' , 'P' ], function(tg) {
207 var cx= Roo.form.HtmlEditor.ToolbarContext;
208 if (typeof(cx.types[tg]) == 'undefined') {
212 cx.types[tg]['font-family'] = cx[ 'font-family'] ;
215 Roo.form.HtmlEditor.ToolbarContext.types
218 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
226 * @cfg {Object} disable List of toolbar elements to disable
231 * @cfg {Object} styles List of styles
232 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
234 * These must be defined in the page, so they get rendered correctly..
245 init : function(editor)
247 this.editor = editor;
250 var fid = editor.frameId;
252 function btn(id, toggle, handler){
253 var xid = fid + '-'+ id ;
257 cls : 'x-btn-icon x-edit-'+id,
258 enableToggle:toggle !== false,
259 scope: editor, // was editor...
260 handler:handler||editor.relayBtnCmd,
261 clickEvent:'mousedown',
262 tooltip: etb.buttonTips[id] || undefined, ///tips ???
266 // create a new element.
267 var wdiv = editor.wrap.createChild({
269 }, editor.wrap.dom.firstChild.nextSibling, true);
271 // can we do this more than once??
276 // disable everything...
277 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
282 this.toolbars[i] = this.buildToolbar(ty[i],i);
284 this.tb = this.toolbars.BODY;
288 editor.on('hide', function( ) { this.footer.hide() }, this);
289 editor.on('show', function( ) { this.footer.show() }, this);
292 this.rendered = true;
295 editor.on('editorevent', this.updateToolbar, this);
296 // other toolbars need to implement this..
297 //editor.on('editmodechange', this.updateToolbar, this);
303 * Protected method that will not generally be called directly. It triggers
304 * a toolbar update by reading the markup state of the current selection in the editor.
306 updateToolbar: function(editor,ev,sel){
309 // capture mouse up - this is handy for selecting images..
310 // perhaps should go somewhere else...
311 if(!this.editor.activated){
312 this.editor.onFirstFocus();
316 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
317 // selectNode - might want to handle IE?
319 (ev.type == 'mouseup' || ev.type == 'click' ) &&
320 ev.target && ev.target.tagName == 'IMG') {
321 // they have click on an image...
322 // let's see if we can change the selection...
325 var nodeRange = sel.ownerDocument.createRange();
327 nodeRange.selectNode(sel);
329 nodeRange.selectNodeContents(sel);
331 //nodeRange.collapse(true);
332 var s = editor.win.getSelection();
334 s.addRange(nodeRange);
338 var updateFooter = sel ? false : true;
341 var ans = this.editor.getAllAncestors();
344 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
347 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
348 sel = sel ? sel : this.editor.doc.body;
349 sel = sel.tagName.length ? sel : this.editor.doc.body;
352 // pick a menu that exists..
353 var tn = sel.tagName.toUpperCase();
354 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
356 tn = sel.tagName.toUpperCase();
358 var lastSel = this.tb.selectedNode
360 this.tb.selectedNode = sel;
362 // if current menu does not match..
363 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
366 ///console.log("show: " + tn);
367 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
370 this.tb.items.first().el.innerHTML = tn + ': ';
374 if (this.tb.fields) {
375 this.tb.fields.each(function(e) {
377 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({
543 fields: ['val', 'display'],
546 name : '-roo-edit-' + i,
548 stylename : item.style ? item.style : false,
549 displayField: item.displayField ? item.displayField : 'val',
554 triggerAction: 'all',
557 width: item.width ? item.width : 130,
559 'select': function(c, r, i) {
561 tb.selectedNode.style[c.stylename] = r.get('val');
564 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
573 tb.addField( new Roo.form.TextField({
581 tb.addField( new Roo.form.TextField({
582 name: '-roo-edit-' + i,
589 'change' : function(f, nv, ov) {
590 tb.selectedNode.setAttribute(f.attrname, nv);
605 // undo does not work.
607 var sn = tb.selectedNode;
609 var pn = sn.parentNode;
611 var stn = sn.childNodes[0];
612 var en = sn.childNodes[sn.childNodes.length - 1 ];
613 while (sn.childNodes.length) {
614 var node = sn.childNodes[0];
615 sn.removeChild(node);
617 pn.insertBefore(node, sn);
621 var range = editor.createRange();
623 range.setStart(stn,0);
624 range.setEnd(en,0); //????
625 //range.selectNode(sel);
628 var selection = editor.getSelection();
629 selection.removeAllRanges();
630 selection.addRange(range);
634 //_this.updateToolbar(null, null, pn);
635 _this.updateToolbar(null, null, null);
636 this.footDisp.dom.innerHTML = '';
646 tb.el.on('click', function(e){
647 e.preventDefault(); // what does this do?
649 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
652 // dont need to disable them... as they will get hidden
657 buildFooter : function()
660 var fel = this.editor.wrap.createChild();
661 this.footer = new Roo.Toolbar(fel);
662 // toolbar has scrolly on left / right?
663 var footDisp= new Roo.Toolbar.Fill();
669 handler : function() {
670 _t.footDisp.scrollTo('left',0,true)
674 this.footer.add( footDisp );
679 handler : function() {
681 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
685 var fel = Roo.get(footDisp.el);
686 fel.addClass('x-editor-context');
687 this.footDispWrap = fel;
688 this.footDispWrap.overflow = 'hidden';
690 this.footDisp = fel.createChild();
691 this.footDispWrap.on('click', this.onContextClick, this)
695 onContextClick : function (ev,dom)
698 var cn = dom.className;
700 if (!cn.match(/x-ed-loc-/)) {
703 var n = cn.split('-').pop();
704 var ans = this.footerEls;
708 var range = this.editor.createRange();
710 range.selectNodeContents(sel);
711 //range.selectNode(sel);
714 var selection = this.editor.getSelection();
715 selection.removeAllRanges();
716 selection.addRange(range);
720 this.updateToolbar(null, null, sel);