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"]],
121 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
126 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
136 style : 'fontFamily',
137 displayField: 'display',
138 optname : 'font-family',
187 // should we really allow this??
188 // should this just be
199 style : 'fontFamily',
200 displayField: 'display',
201 optname : 'font-family',
208 style : 'fontFamily',
209 displayField: 'display',
210 optname : 'font-family',
217 style : 'fontFamily',
218 displayField: 'display',
219 optname : 'font-family',
230 // this should be configurable.. - you can either set it up using stores, or modify options somehwere..
231 Roo.form.HtmlEditor.ToolbarContext.stores = false;
233 Roo.form.HtmlEditor.ToolbarContext.options = {
235 [ 'Helvetica,Arial,sans-serif', 'Helvetica'],
236 [ 'Courier New', 'Courier New'],
237 [ 'Tahoma', 'Tahoma'],
238 [ 'Times New Roman,serif', 'Times'],
239 [ 'Verdana','Verdana' ]
243 // fixme - these need to be configurable..
246 Roo.form.HtmlEditor.ToolbarContext.types
249 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
257 * @cfg {Object} disable List of toolbar elements to disable
262 * @cfg {Object} styles List of styles
263 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
265 * These must be defined in the page, so they get rendered correctly..
276 init : function(editor)
278 this.editor = editor;
279 this.editorcore = editor.editorcore ? editor.editorcore : editor;
280 var editorcore = this.editorcore;
282 var fid = editorcore.frameId;
284 function btn(id, toggle, handler){
285 var xid = fid + '-'+ id ;
289 cls : 'x-btn-icon x-edit-'+id,
290 enableToggle:toggle !== false,
291 scope: editor, // was editor...
292 handler:handler||editor.relayBtnCmd,
293 clickEvent:'mousedown',
294 tooltip: etb.buttonTips[id] || undefined, ///tips ???
298 // create a new element.
299 var wdiv = editor.wrap.createChild({
301 }, editor.wrap.dom.firstChild.nextSibling, true);
303 // can we do this more than once??
308 // disable everything...
309 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
314 this.toolbars[i] = this.buildToolbar(ty[i],i);
316 this.tb = this.toolbars.BODY;
320 editor.on('hide', function( ) { this.footer.hide() }, this);
321 editor.on('show', function( ) { this.footer.show() }, this);
324 this.rendered = true;
327 editor.on('editorevent', this.updateToolbar, this);
328 // other toolbars need to implement this..
329 //editor.on('editmodechange', this.updateToolbar, this);
335 * Protected method that will not generally be called directly. It triggers
336 * a toolbar update by reading the markup state of the current selection in the editor.
338 updateToolbar: function(editor,ev,sel){
341 // capture mouse up - this is handy for selecting images..
342 // perhaps should go somewhere else...
343 if(!this.editor.activated){
344 this.editor.onFirstFocus();
348 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
349 // selectNode - might want to handle IE?
351 (ev.type == 'mouseup' || ev.type == 'click' ) &&
352 ev.target && ev.target.tagName == 'IMG') {
353 // they have click on an image...
354 // let's see if we can change the selection...
357 var nodeRange = sel.ownerDocument.createRange();
359 nodeRange.selectNode(sel);
361 nodeRange.selectNodeContents(sel);
363 //nodeRange.collapse(true);
364 var s = editor.win.getSelection();
366 s.addRange(nodeRange);
370 var updateFooter = sel ? false : true;
373 var ans = this.editor.getAllAncestors();
376 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
379 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
380 sel = sel ? sel : this.editor.doc.body;
381 sel = sel.tagName.length ? sel : this.editor.doc.body;
384 // pick a menu that exists..
385 var tn = sel.tagName.toUpperCase();
386 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
388 tn = sel.tagName.toUpperCase();
390 var lastSel = this.tb.selectedNode
392 this.tb.selectedNode = sel;
394 // if current menu does not match..
395 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
398 ///console.log("show: " + tn);
399 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
402 this.tb.items.first().el.innerHTML = tn + ': ';
406 if (this.tb.fields) {
407 this.tb.fields.each(function(e) {
409 e.setValue(sel.style[e.stylename]);
412 e.setValue(sel.getAttribute(e.attrname));
416 var hasStyles = false;
417 for(var i in this.styles) {
424 var st = this.tb.fields.item(0);
426 st.store.removeAll();
429 var cn = sel.className.split(/\s+/);
432 if (this.styles['*']) {
434 Roo.each(this.styles['*'], function(v) {
435 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
438 if (this.styles[tn]) {
439 Roo.each(this.styles[tn], function(v) {
440 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
444 st.store.loadData(avs);
448 // flag our selected Node.
449 this.tb.selectedNode = sel;
452 Roo.menu.MenuMgr.hideAll();
457 //this.footDisp.dom.innerHTML = '';
464 this.footerEls = ans.reverse();
465 Roo.each(this.footerEls, function(a,i) {
467 html += html.length ? ' > ' : '';
469 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
474 var sz = this.footDisp.up('td').getSize();
475 this.footDisp.dom.style.width = (sz.width -10) + 'px';
476 this.footDisp.dom.style.marginLeft = '5px';
478 this.footDisp.dom.style.overflow = 'hidden';
480 this.footDisp.dom.innerHTML = html;
482 //this.editorsyncValue();
489 onDestroy : function(){
492 this.tb.items.each(function(item){
494 item.menu.removeAll();
496 item.menu.el.destroy();
504 onFirstFocus: function() {
505 // need to do this for all the toolbars..
506 this.tb.items.each(function(item){
510 buildToolbar: function(tlist, nm)
512 var editor = this.editor;
513 // create a new element.
514 var wdiv = editor.wrap.createChild({
516 }, editor.wrap.dom.firstChild.nextSibling, true);
519 var tb = new Roo.Toolbar(wdiv);
522 tb.add(nm+ ": ");
525 for(var i in this.styles) {
530 if (styles && styles.length) {
532 // this needs a multi-select checkbox...
533 tb.addField( new Roo.form.ComboBox({
534 store: new Roo.data.SimpleStore({
536 fields: ['val', 'selected'],
539 name : '-roo-edit-className',
540 attrname : 'className',
545 triggerAction: 'all',
546 emptyText:'Select Style',
550 'select': function(c, r, i) {
551 // initial support only for on class per el..
552 tb.selectedNode.className = r ? r.get('val') : '';
560 var tbc = Roo.form.HtmlEditor.ToolbarContext;
561 var tbops = tbc.options;
563 for (var i in tlist) {
566 tb.add(item.title + ": ");
569 //optname == used so you can configure the options available..
570 var opts = item.opts ? item.opts : false;
572 opts = tbops[item.optname];
577 // opts == pulldown..
578 tb.addField( new Roo.form.ComboBox({
579 store: typeof(tbc.stores[i]) != 'undefined' ? Roo.factory(tbc.stores[i],Roo.data) : new Roo.data.SimpleStore({
581 fields: ['val', 'display'],
584 name : '-roo-edit-' + i,
586 stylename : item.style ? item.style : false,
587 displayField: item.displayField ? item.displayField : 'val',
590 mode: typeof(tbc.stores[i]) != 'undefined' ? 'remote' : 'local',
592 triggerAction: 'all',
595 width: item.width ? item.width : 130,
597 'select': function(c, r, i) {
599 tb.selectedNode.style[c.stylename] = r.get('val');
602 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
611 tb.addField( new Roo.form.TextField({
619 tb.addField( new Roo.form.TextField({
620 name: '-roo-edit-' + i,
627 'change' : function(f, nv, ov) {
628 tb.selectedNode.setAttribute(f.attrname, nv);
643 // undo does not work.
645 var sn = tb.selectedNode;
647 var pn = sn.parentNode;
649 var stn = sn.childNodes[0];
650 var en = sn.childNodes[sn.childNodes.length - 1 ];
651 while (sn.childNodes.length) {
652 var node = sn.childNodes[0];
653 sn.removeChild(node);
655 pn.insertBefore(node, sn);
659 var range = editor.createRange();
661 range.setStart(stn,0);
662 range.setEnd(en,0); //????
663 //range.selectNode(sel);
666 var selection = editor.getSelection();
667 selection.removeAllRanges();
668 selection.addRange(range);
672 //_this.updateToolbar(null, null, pn);
673 _this.updateToolbar(null, null, null);
674 _this.footDisp.dom.innerHTML = '';
684 tb.el.on('click', function(e){
685 e.preventDefault(); // what does this do?
687 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
690 // dont need to disable them... as they will get hidden
695 buildFooter : function()
698 var fel = this.editor.wrap.createChild();
699 this.footer = new Roo.Toolbar(fel);
700 // toolbar has scrolly on left / right?
701 var footDisp= new Roo.Toolbar.Fill();
707 handler : function() {
708 _t.footDisp.scrollTo('left',0,true)
712 this.footer.add( footDisp );
717 handler : function() {
719 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
723 var fel = Roo.get(footDisp.el);
724 fel.addClass('x-editor-context');
725 this.footDispWrap = fel;
726 this.footDispWrap.overflow = 'hidden';
728 this.footDisp = fel.createChild();
729 this.footDispWrap.on('click', this.onContextClick, this)
733 onContextClick : function (ev,dom)
736 var cn = dom.className;
738 if (!cn.match(/x-ed-loc-/)) {
741 var n = cn.split('-').pop();
742 var ans = this.footerEls;
746 var range = this.editor.createRange();
748 range.selectNodeContents(sel);
749 //range.selectNode(sel);
752 var selection = this.editor.getSelection();
753 selection.removeAllRanges();
754 selection.addRange(range);
758 this.updateToolbar(null, null, sel);