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"]],
132 style : 'fontFamily',
133 displayField: 'display',
134 optname : 'font-family',
183 // should we really allow this??
184 // should this just be
195 style : 'fontFamily',
196 displayField: 'display',
197 optname : 'font-family',
204 style : 'fontFamily',
205 displayField: 'display',
206 optname : 'font-family',
213 style : 'fontFamily',
214 displayField: 'display',
215 optname : 'font-family',
226 // this should be configurable..
227 Roo.form.HtmlEditor.ToolbarContext.options = {
229 [ 'Helvetica,Arial,sans-serif', 'Helvetica'],
230 [ 'Courier New', 'Courier New'],
231 [ 'Tahoma', 'Tahoma'],
232 [ 'Times New Roman,serif', 'Times'],
233 [ 'Verdana','Verdana' ]
237 // fixme - these need to be configurable..
240 Roo.form.HtmlEditor.ToolbarContext.types
243 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
251 * @cfg {Object} disable List of toolbar elements to disable
256 * @cfg {Object} styles List of styles
257 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
259 * These must be defined in the page, so they get rendered correctly..
270 init : function(editor)
272 this.editor = editor;
275 var fid = editor.frameId;
277 function btn(id, toggle, handler){
278 var xid = fid + '-'+ id ;
282 cls : 'x-btn-icon x-edit-'+id,
283 enableToggle:toggle !== false,
284 scope: editor, // was editor...
285 handler:handler||editor.relayBtnCmd,
286 clickEvent:'mousedown',
287 tooltip: etb.buttonTips[id] || undefined, ///tips ???
291 // create a new element.
292 var wdiv = editor.wrap.createChild({
294 }, editor.wrap.dom.firstChild.nextSibling, true);
296 // can we do this more than once??
301 // disable everything...
302 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
307 this.toolbars[i] = this.buildToolbar(ty[i],i);
309 this.tb = this.toolbars.BODY;
313 editor.on('hide', function( ) { this.footer.hide() }, this);
314 editor.on('show', function( ) { this.footer.show() }, this);
317 this.rendered = true;
320 editor.on('editorevent', this.updateToolbar, this);
321 // other toolbars need to implement this..
322 //editor.on('editmodechange', this.updateToolbar, this);
328 * Protected method that will not generally be called directly. It triggers
329 * a toolbar update by reading the markup state of the current selection in the editor.
331 updateToolbar: function(editor,ev,sel){
334 // capture mouse up - this is handy for selecting images..
335 // perhaps should go somewhere else...
336 if(!this.editor.activated){
337 this.editor.onFirstFocus();
341 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
342 // selectNode - might want to handle IE?
344 (ev.type == 'mouseup' || ev.type == 'click' ) &&
345 ev.target && ev.target.tagName == 'IMG') {
346 // they have click on an image...
347 // let's see if we can change the selection...
350 var nodeRange = sel.ownerDocument.createRange();
352 nodeRange.selectNode(sel);
354 nodeRange.selectNodeContents(sel);
356 //nodeRange.collapse(true);
357 var s = editor.win.getSelection();
359 s.addRange(nodeRange);
363 var updateFooter = sel ? false : true;
366 var ans = this.editor.getAllAncestors();
369 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
372 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
373 sel = sel ? sel : this.editor.doc.body;
374 sel = sel.tagName.length ? sel : this.editor.doc.body;
377 // pick a menu that exists..
378 var tn = sel.tagName.toUpperCase();
379 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
381 tn = sel.tagName.toUpperCase();
383 var lastSel = this.tb.selectedNode
385 this.tb.selectedNode = sel;
387 // if current menu does not match..
388 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
391 ///console.log("show: " + tn);
392 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
395 this.tb.items.first().el.innerHTML = tn + ': ';
399 if (this.tb.fields) {
400 this.tb.fields.each(function(e) {
402 e.setValue(sel.style[e.stylename]);
405 e.setValue(sel.getAttribute(e.attrname));
409 var hasStyles = false;
410 for(var i in this.styles) {
417 var st = this.tb.fields.item(0);
419 st.store.removeAll();
422 var cn = sel.className.split(/\s+/);
425 if (this.styles['*']) {
427 Roo.each(this.styles['*'], function(v) {
428 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
431 if (this.styles[tn]) {
432 Roo.each(this.styles[tn], function(v) {
433 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
437 st.store.loadData(avs);
441 // flag our selected Node.
442 this.tb.selectedNode = sel;
445 Roo.menu.MenuMgr.hideAll();
450 //this.footDisp.dom.innerHTML = '';
457 this.footerEls = ans.reverse();
458 Roo.each(this.footerEls, function(a,i) {
460 html += html.length ? ' > ' : '';
462 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
467 var sz = this.footDisp.up('td').getSize();
468 this.footDisp.dom.style.width = (sz.width -10) + 'px';
469 this.footDisp.dom.style.marginLeft = '5px';
471 this.footDisp.dom.style.overflow = 'hidden';
473 this.footDisp.dom.innerHTML = html;
475 //this.editorsyncValue();
482 onDestroy : function(){
485 this.tb.items.each(function(item){
487 item.menu.removeAll();
489 item.menu.el.destroy();
497 onFirstFocus: function() {
498 // need to do this for all the toolbars..
499 this.tb.items.each(function(item){
503 buildToolbar: function(tlist, nm)
505 var editor = this.editor;
506 // create a new element.
507 var wdiv = editor.wrap.createChild({
509 }, editor.wrap.dom.firstChild.nextSibling, true);
512 var tb = new Roo.Toolbar(wdiv);
515 tb.add(nm+ ": ");
518 for(var i in this.styles) {
523 if (styles && styles.length) {
525 // this needs a multi-select checkbox...
526 tb.addField( new Roo.form.ComboBox({
527 store: new Roo.data.SimpleStore({
529 fields: ['val', 'selected'],
532 name : '-roo-edit-className',
533 attrname : 'className',
538 triggerAction: 'all',
539 emptyText:'Select Style',
543 'select': function(c, r, i) {
544 // initial support only for on class per el..
545 tb.selectedNode.className = r ? r.get('val') : '';
553 var tbops = Roo.form.HtmlEditor.ToolbarContext.options;
555 for (var i in tlist) {
558 tb.add(item.title + ": ");
561 //optname == used so you can configure the options available..
562 var opts = item.opts ? item.opts : false;
564 opts = tbops[item.optname];
569 // opts == pulldown..
570 tb.addField( new Roo.form.ComboBox({
571 store: new Roo.data.SimpleStore({
573 fields: ['val', 'display'],
576 name : '-roo-edit-' + i,
578 stylename : item.style ? item.style : false,
579 displayField: item.displayField ? item.displayField : 'val',
584 triggerAction: 'all',
587 width: item.width ? item.width : 130,
589 'select': function(c, r, i) {
591 tb.selectedNode.style[c.stylename] = r.get('val');
594 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
603 tb.addField( new Roo.form.TextField({
611 tb.addField( new Roo.form.TextField({
612 name: '-roo-edit-' + i,
619 'change' : function(f, nv, ov) {
620 tb.selectedNode.setAttribute(f.attrname, nv);
635 // undo does not work.
637 var sn = tb.selectedNode;
639 var pn = sn.parentNode;
641 var stn = sn.childNodes[0];
642 var en = sn.childNodes[sn.childNodes.length - 1 ];
643 while (sn.childNodes.length) {
644 var node = sn.childNodes[0];
645 sn.removeChild(node);
647 pn.insertBefore(node, sn);
651 var range = editor.createRange();
653 range.setStart(stn,0);
654 range.setEnd(en,0); //????
655 //range.selectNode(sel);
658 var selection = editor.getSelection();
659 selection.removeAllRanges();
660 selection.addRange(range);
664 //_this.updateToolbar(null, null, pn);
665 _this.updateToolbar(null, null, null);
666 _this.footDisp.dom.innerHTML = '';
676 tb.el.on('click', function(e){
677 e.preventDefault(); // what does this do?
679 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
682 // dont need to disable them... as they will get hidden
687 buildFooter : function()
690 var fel = this.editor.wrap.createChild();
691 this.footer = new Roo.Toolbar(fel);
692 // toolbar has scrolly on left / right?
693 var footDisp= new Roo.Toolbar.Fill();
699 handler : function() {
700 _t.footDisp.scrollTo('left',0,true)
704 this.footer.add( footDisp );
709 handler : function() {
711 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
715 var fel = Roo.get(footDisp.el);
716 fel.addClass('x-editor-context');
717 this.footDispWrap = fel;
718 this.footDispWrap.overflow = 'hidden';
720 this.footDisp = fel.createChild();
721 this.footDispWrap.on('click', this.onContextClick, this)
725 onContextClick : function (ev,dom)
728 var cn = dom.className;
730 if (!cn.match(/x-ed-loc-/)) {
733 var n = cn.split('-').pop();
734 var ans = this.footerEls;
738 var range = this.editor.createRange();
740 range.selectNodeContents(sel);
741 //range.selectNode(sel);
744 var selection = this.editor.getSelection();
745 selection.removeAllRanges();
746 selection.addRange(range);
750 this.updateToolbar(null, null, sel);