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 * Note you can force an update by calling on('editorevent', scope, false)
341 updateToolbar: function(editor,ev,sel){
344 // capture mouse up - this is handy for selecting images..
345 // perhaps should go somewhere else...
346 if(!this.editorcore.activated){
347 this.editor.onFirstFocus();
353 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
354 // selectNode - might want to handle IE?
356 (ev.type == 'mouseup' || ev.type == 'click' ) &&
357 ev.target && ev.target.tagName == 'IMG') {
358 // they have click on an image...
359 // let's see if we can change the selection...
362 var nodeRange = sel.ownerDocument.createRange();
364 nodeRange.selectNode(sel);
366 nodeRange.selectNodeContents(sel);
368 //nodeRange.collapse(true);
369 var s = this.editorcore.win.getSelection();
371 s.addRange(nodeRange);
375 var updateFooter = sel ? false : true;
378 var ans = this.editorcore.getAllAncestors();
381 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
384 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
385 sel = sel ? sel : this.editorcore.doc.body;
386 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
389 // pick a menu that exists..
390 var tn = sel.tagName.toUpperCase();
391 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
393 tn = sel.tagName.toUpperCase();
395 var lastSel = this.tb.selectedNode;
397 this.tb.selectedNode = sel;
399 // if current menu does not match..
401 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode) || ev === false) {
404 ///console.log("show: " + tn);
405 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
408 this.tb.items.first().el.innerHTML = tn + ': ';
412 if (this.tb.fields) {
413 this.tb.fields.each(function(e) {
415 e.setValue(sel.style[e.stylename]);
418 e.setValue(sel.getAttribute(e.attrname));
422 var hasStyles = false;
423 for(var i in this.styles) {
430 var st = this.tb.fields.item(0);
432 st.store.removeAll();
435 var cn = sel.className.split(/\s+/);
438 if (this.styles['*']) {
440 Roo.each(this.styles['*'], function(v) {
441 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
444 if (this.styles[tn]) {
445 Roo.each(this.styles[tn], function(v) {
446 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
450 st.store.loadData(avs);
454 // flag our selected Node.
455 this.tb.selectedNode = sel;
458 Roo.menu.MenuMgr.hideAll();
463 //this.footDisp.dom.innerHTML = '';
470 this.footerEls = ans.reverse();
471 Roo.each(this.footerEls, function(a,i) {
473 html += html.length ? ' > ' : '';
475 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
480 var sz = this.footDisp.up('td').getSize();
481 this.footDisp.dom.style.width = (sz.width -10) + 'px';
482 this.footDisp.dom.style.marginLeft = '5px';
484 this.footDisp.dom.style.overflow = 'hidden';
486 this.footDisp.dom.innerHTML = html;
488 //this.editorsyncValue();
495 onDestroy : function(){
498 this.tb.items.each(function(item){
500 item.menu.removeAll();
502 item.menu.el.destroy();
510 onFirstFocus: function() {
511 // need to do this for all the toolbars..
512 this.tb.items.each(function(item){
516 buildToolbar: function(tlist, nm)
518 var editor = this.editor;
519 var editorcore = this.editorcore;
520 // create a new element.
521 var wdiv = editor.wrap.createChild({
523 }, editor.wrap.dom.firstChild.nextSibling, true);
526 var tb = new Roo.Toolbar(wdiv);
529 tb.add(nm+ ": ");
532 for(var i in this.styles) {
537 if (styles && styles.length) {
539 // this needs a multi-select checkbox...
540 tb.addField( new Roo.form.ComboBox({
541 store: new Roo.data.SimpleStore({
543 fields: ['val', 'selected'],
546 name : '-roo-edit-className',
547 attrname : 'className',
552 triggerAction: 'all',
553 emptyText:'Select Style',
557 'select': function(c, r, i) {
558 // initial support only for on class per el..
559 tb.selectedNode.className = r ? r.get('val') : '';
560 editorcore.syncValue();
567 var tbc = Roo.form.HtmlEditor.ToolbarContext;
568 var tbops = tbc.options;
570 for (var i in tlist) {
573 tb.add(item.title + ": ");
576 //optname == used so you can configure the options available..
577 var opts = item.opts ? item.opts : false;
579 opts = tbops[item.optname];
584 // opts == pulldown..
585 tb.addField( new Roo.form.ComboBox({
586 store: typeof(tbc.stores[i]) != 'undefined' ? Roo.factory(tbc.stores[i],Roo.data) : new Roo.data.SimpleStore({
588 fields: ['val', 'display'],
591 name : '-roo-edit-' + i,
593 stylename : item.style ? item.style : false,
594 displayField: item.displayField ? item.displayField : 'val',
597 mode: typeof(tbc.stores[i]) != 'undefined' ? 'remote' : 'local',
599 triggerAction: 'all',
602 width: item.width ? item.width : 130,
604 'select': function(c, r, i) {
606 tb.selectedNode.style[c.stylename] = r.get('val');
609 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
618 tb.addField( new Roo.form.TextField({
626 tb.addField( new Roo.form.TextField({
627 name: '-roo-edit-' + i,
634 'change' : function(f, nv, ov) {
635 tb.selectedNode.setAttribute(f.attrname, nv);
653 _this.editor.fireEvent('stylesheetsclick', _this.editor);
667 // undo does not work.
669 var sn = tb.selectedNode;
671 var pn = sn.parentNode;
673 var stn = sn.childNodes[0];
674 var en = sn.childNodes[sn.childNodes.length - 1 ];
675 while (sn.childNodes.length) {
676 var node = sn.childNodes[0];
677 sn.removeChild(node);
679 pn.insertBefore(node, sn);
683 var range = editorcore.createRange();
685 range.setStart(stn,0);
686 range.setEnd(en,0); //????
687 //range.selectNode(sel);
690 var selection = editorcore.getSelection();
691 selection.removeAllRanges();
692 selection.addRange(range);
696 //_this.updateToolbar(null, null, pn);
697 _this.updateToolbar(null, null, null);
698 _this.footDisp.dom.innerHTML = '';
708 tb.el.on('click', function(e){
709 e.preventDefault(); // what does this do?
711 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
714 // dont need to disable them... as they will get hidden
719 buildFooter : function()
722 var fel = this.editor.wrap.createChild();
723 this.footer = new Roo.Toolbar(fel);
724 // toolbar has scrolly on left / right?
725 var footDisp= new Roo.Toolbar.Fill();
731 handler : function() {
732 _t.footDisp.scrollTo('left',0,true)
736 this.footer.add( footDisp );
741 handler : function() {
743 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
747 var fel = Roo.get(footDisp.el);
748 fel.addClass('x-editor-context');
749 this.footDispWrap = fel;
750 this.footDispWrap.overflow = 'hidden';
752 this.footDisp = fel.createChild();
753 this.footDispWrap.on('click', this.onContextClick, this)
757 onContextClick : function (ev,dom)
760 var cn = dom.className;
762 if (!cn.match(/x-ed-loc-/)) {
765 var n = cn.split('-').pop();
766 var ans = this.footerEls;
770 var range = this.editorcore.createRange();
772 range.selectNodeContents(sel);
773 //range.selectNode(sel);
776 var selection = this.editorcore.getSelection();
777 selection.removeAllRanges();
778 selection.addRange(range);
782 this.updateToolbar(null, null, sel);