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, {
258 * @cfg {Object} disable List of toolbar elements to disable
263 * @cfg {Object} styles List of styles
264 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
266 * These must be defined in the page, so they get rendered correctly..
277 init : function(editor)
279 this.editor = editor;
280 this.editorcore = editor.editorcore ? editor.editorcore : editor;
281 var editorcore = this.editorcore;
283 var fid = editorcore.frameId;
285 function btn(id, toggle, handler){
286 var xid = fid + '-'+ id ;
290 cls : 'x-btn-icon x-edit-'+id,
291 enableToggle:toggle !== false,
292 scope: editorcore, // was editor...
293 handler:handler||editorcore.relayBtnCmd,
294 clickEvent:'mousedown',
295 tooltip: etb.buttonTips[id] || undefined, ///tips ???
299 // create a new element.
300 var wdiv = editor.wrap.createChild({
302 }, editor.wrap.dom.firstChild.nextSibling, true);
304 // can we do this more than once??
309 // disable everything...
310 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
315 this.toolbars[i] = this.buildToolbar(ty[i],i);
317 this.tb = this.toolbars.BODY;
321 editor.on('hide', function( ) { this.footer.hide() }, this);
322 editor.on('show', function( ) { this.footer.show() }, this);
325 this.rendered = true;
328 editor.on('editorevent', this.updateToolbar, this);
329 // other toolbars need to implement this..
330 //editor.on('editmodechange', this.updateToolbar, this);
336 * Protected method that will not generally be called directly. It triggers
337 * a toolbar update by reading the markup state of the current selection in the editor.
339 updateToolbar: function(editor,ev,sel){
342 // capture mouse up - this is handy for selecting images..
343 // perhaps should go somewhere else...
344 if(!this.editor.activated){
345 this.editor.onFirstFocus();
349 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
350 // selectNode - might want to handle IE?
352 (ev.type == 'mouseup' || ev.type == 'click' ) &&
353 ev.target && ev.target.tagName == 'IMG') {
354 // they have click on an image...
355 // let's see if we can change the selection...
358 var nodeRange = sel.ownerDocument.createRange();
360 nodeRange.selectNode(sel);
362 nodeRange.selectNodeContents(sel);
364 //nodeRange.collapse(true);
365 var s = this.editorcore.win.getSelection();
367 s.addRange(nodeRange);
371 var updateFooter = sel ? false : true;
374 var ans = this.editorcore.getAllAncestors();
377 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
380 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
381 sel = sel ? sel : this.editorcore.doc.body;
382 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
385 // pick a menu that exists..
386 var tn = sel.tagName.toUpperCase();
387 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
389 tn = sel.tagName.toUpperCase();
391 var lastSel = this.tb.selectedNode
393 this.tb.selectedNode = sel;
395 // if current menu does not match..
396 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
399 ///console.log("show: " + tn);
400 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
403 this.tb.items.first().el.innerHTML = tn + ': ';
407 if (this.tb.fields) {
408 this.tb.fields.each(function(e) {
410 e.setValue(sel.style[e.stylename]);
413 e.setValue(sel.getAttribute(e.attrname));
417 var hasStyles = false;
418 for(var i in this.styles) {
425 var st = this.tb.fields.item(0);
427 st.store.removeAll();
430 var cn = sel.className.split(/\s+/);
433 if (this.styles['*']) {
435 Roo.each(this.styles['*'], function(v) {
436 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
439 if (this.styles[tn]) {
440 Roo.each(this.styles[tn], function(v) {
441 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
445 st.store.loadData(avs);
449 // flag our selected Node.
450 this.tb.selectedNode = sel;
453 Roo.menu.MenuMgr.hideAll();
458 //this.footDisp.dom.innerHTML = '';
465 this.footerEls = ans.reverse();
466 Roo.each(this.footerEls, function(a,i) {
468 html += html.length ? ' > ' : '';
470 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
475 var sz = this.footDisp.up('td').getSize();
476 this.footDisp.dom.style.width = (sz.width -10) + 'px';
477 this.footDisp.dom.style.marginLeft = '5px';
479 this.footDisp.dom.style.overflow = 'hidden';
481 this.footDisp.dom.innerHTML = html;
483 //this.editorsyncValue();
490 onDestroy : function(){
493 this.tb.items.each(function(item){
495 item.menu.removeAll();
497 item.menu.el.destroy();
505 onFirstFocus: function() {
506 // need to do this for all the toolbars..
507 this.tb.items.each(function(item){
511 buildToolbar: function(tlist, nm)
513 var editor = this.editor;
514 var editorcore = this.editorcore;
515 // create a new element.
516 var wdiv = editor.wrap.createChild({
518 }, editor.wrap.dom.firstChild.nextSibling, true);
521 var tb = new Roo.Toolbar(wdiv);
524 tb.add(nm+ ": ");
527 for(var i in this.styles) {
532 if (styles && styles.length) {
534 // this needs a multi-select checkbox...
535 tb.addField( new Roo.form.ComboBox({
536 store: new Roo.data.SimpleStore({
538 fields: ['val', 'selected'],
541 name : '-roo-edit-className',
542 attrname : 'className',
547 triggerAction: 'all',
548 emptyText:'Select Style',
552 'select': function(c, r, i) {
553 // initial support only for on class per el..
554 tb.selectedNode.className = r ? r.get('val') : '';
555 editorcore.syncValue();
562 var tbc = Roo.form.HtmlEditor.ToolbarContext;
563 var tbops = tbc.options;
565 for (var i in tlist) {
568 tb.add(item.title + ": ");
571 //optname == used so you can configure the options available..
572 var opts = item.opts ? item.opts : false;
574 opts = tbops[item.optname];
579 // opts == pulldown..
580 tb.addField( new Roo.form.ComboBox({
581 store: typeof(tbc.stores[i]) != 'undefined' ? Roo.factory(tbc.stores[i],Roo.data) : new Roo.data.SimpleStore({
583 fields: ['val', 'display'],
586 name : '-roo-edit-' + i,
588 stylename : item.style ? item.style : false,
589 displayField: item.displayField ? item.displayField : 'val',
592 mode: typeof(tbc.stores[i]) != 'undefined' ? 'remote' : 'local',
594 triggerAction: 'all',
597 width: item.width ? item.width : 130,
599 'select': function(c, r, i) {
601 tb.selectedNode.style[c.stylename] = r.get('val');
604 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
613 tb.addField( new Roo.form.TextField({
621 tb.addField( new Roo.form.TextField({
622 name: '-roo-edit-' + i,
629 'change' : function(f, nv, ov) {
630 tb.selectedNode.setAttribute(f.attrname, nv);
645 // undo does not work.
647 var sn = tb.selectedNode;
649 var pn = sn.parentNode;
651 var stn = sn.childNodes[0];
652 var en = sn.childNodes[sn.childNodes.length - 1 ];
653 while (sn.childNodes.length) {
654 var node = sn.childNodes[0];
655 sn.removeChild(node);
657 pn.insertBefore(node, sn);
661 var range = editorcore.createRange();
663 range.setStart(stn,0);
664 range.setEnd(en,0); //????
665 //range.selectNode(sel);
668 var selection = editorcore.getSelection();
669 selection.removeAllRanges();
670 selection.addRange(range);
674 //_this.updateToolbar(null, null, pn);
675 _this.updateToolbar(null, null, null);
676 _this.footDisp.dom.innerHTML = '';
686 tb.el.on('click', function(e){
687 e.preventDefault(); // what does this do?
689 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
692 // dont need to disable them... as they will get hidden
697 buildFooter : function()
700 var fel = this.editor.wrap.createChild();
701 this.footer = new Roo.Toolbar(fel);
702 // toolbar has scrolly on left / right?
703 var footDisp= new Roo.Toolbar.Fill();
709 handler : function() {
710 _t.footDisp.scrollTo('left',0,true)
714 this.footer.add( footDisp );
719 handler : function() {
721 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
725 var fel = Roo.get(footDisp.el);
726 fel.addClass('x-editor-context');
727 this.footDispWrap = fel;
728 this.footDispWrap.overflow = 'hidden';
730 this.footDisp = fel.createChild();
731 this.footDispWrap.on('click', this.onContextClick, this)
735 onContextClick : function (ev,dom)
738 var cn = dom.className;
740 if (!cn.match(/x-ed-loc-/)) {
743 var n = cn.split('-').pop();
744 var ans = this.footerEls;
748 var range = this.editorcore.createRange();
750 range.selectNodeContents(sel);
751 //range.selectNode(sel);
754 var selection = this.editorcore.getSelection();
755 selection.removeAllRanges();
756 selection.addRange(range);
760 this.updateToolbar(null, null, sel);