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;
318 Roo.log('this.toolbars.BODY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
319 Roo.log(this.toolbars.BODY);
324 editor.on('hide', function( ) { this.footer.hide() }, this);
325 editor.on('show', function( ) { this.footer.show() }, this);
328 this.rendered = true;
331 editor.on('editorevent', this.updateToolbar, this);
332 // other toolbars need to implement this..
333 //editor.on('editmodechange', this.updateToolbar, this);
339 * Protected method that will not generally be called directly. It triggers
340 * a toolbar update by reading the markup state of the current selection in the editor.
342 updateToolbar: function(editor,ev,sel){
345 // capture mouse up - this is handy for selecting images..
346 // perhaps should go somewhere else...
347 if(!this.editorcore.activated){
348 this.editor.onFirstFocus();
352 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
353 // selectNode - might want to handle IE?
355 (ev.type == 'mouseup' || ev.type == 'click' ) &&
356 ev.target && ev.target.tagName == 'IMG') {
357 // they have click on an image...
358 // let's see if we can change the selection...
361 var nodeRange = sel.ownerDocument.createRange();
363 nodeRange.selectNode(sel);
365 nodeRange.selectNodeContents(sel);
367 //nodeRange.collapse(true);
368 var s = this.editorcore.win.getSelection();
370 s.addRange(nodeRange);
374 var updateFooter = sel ? false : true;
377 var ans = this.editorcore.getAllAncestors();
380 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
383 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editorcore.doc.body;
384 sel = sel ? sel : this.editorcore.doc.body;
385 sel = sel.tagName.length ? sel : this.editorcore.doc.body;
388 // pick a menu that exists..
389 var tn = sel.tagName.toUpperCase();
390 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
392 tn = sel.tagName.toUpperCase();
394 var lastSel = this.tb.selectedNode
396 this.tb.selectedNode = sel;
398 // if current menu does not match..
399 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
402 ///console.log("show: " + tn);
403 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
406 this.tb.items.first().el.innerHTML = tn + ': ';
410 if (this.tb.fields) {
411 this.tb.fields.each(function(e) {
413 e.setValue(sel.style[e.stylename]);
416 e.setValue(sel.getAttribute(e.attrname));
420 var hasStyles = false;
421 for(var i in this.styles) {
428 var st = this.tb.fields.item(0);
430 st.store.removeAll();
433 var cn = sel.className.split(/\s+/);
436 if (this.styles['*']) {
438 Roo.each(this.styles['*'], function(v) {
439 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
442 if (this.styles[tn]) {
443 Roo.each(this.styles[tn], function(v) {
444 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
448 st.store.loadData(avs);
452 // flag our selected Node.
453 this.tb.selectedNode = sel;
456 Roo.menu.MenuMgr.hideAll();
461 //this.footDisp.dom.innerHTML = '';
468 this.footerEls = ans.reverse();
469 Roo.each(this.footerEls, function(a,i) {
471 html += html.length ? ' > ' : '';
473 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
478 var sz = this.footDisp.up('td').getSize();
479 this.footDisp.dom.style.width = (sz.width -10) + 'px';
480 this.footDisp.dom.style.marginLeft = '5px';
482 this.footDisp.dom.style.overflow = 'hidden';
484 this.footDisp.dom.innerHTML = html;
486 //this.editorsyncValue();
493 onDestroy : function(){
496 this.tb.items.each(function(item){
498 item.menu.removeAll();
500 item.menu.el.destroy();
508 onFirstFocus: function() {
509 // need to do this for all the toolbars..
510 this.tb.items.each(function(item){
514 buildToolbar: function(tlist, nm)
516 Roo.log('build Toolbar!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
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);
650 // undo does not work.
652 var sn = tb.selectedNode;
654 var pn = sn.parentNode;
656 var stn = sn.childNodes[0];
657 var en = sn.childNodes[sn.childNodes.length - 1 ];
658 while (sn.childNodes.length) {
659 var node = sn.childNodes[0];
660 sn.removeChild(node);
662 pn.insertBefore(node, sn);
666 var range = editorcore.createRange();
668 range.setStart(stn,0);
669 range.setEnd(en,0); //????
670 //range.selectNode(sel);
673 var selection = editorcore.getSelection();
674 selection.removeAllRanges();
675 selection.addRange(range);
679 //_this.updateToolbar(null, null, pn);
680 _this.updateToolbar(null, null, null);
681 _this.footDisp.dom.innerHTML = '';
691 tb.el.on('click', function(e){
692 e.preventDefault(); // what does this do?
694 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
697 // dont need to disable them... as they will get hidden
702 buildFooter : function()
705 var fel = this.editor.wrap.createChild();
706 this.footer = new Roo.Toolbar(fel);
707 // toolbar has scrolly on left / right?
708 var footDisp= new Roo.Toolbar.Fill();
714 handler : function() {
715 _t.footDisp.scrollTo('left',0,true)
719 this.footer.add( footDisp );
724 handler : function() {
726 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
730 var fel = Roo.get(footDisp.el);
731 fel.addClass('x-editor-context');
732 this.footDispWrap = fel;
733 this.footDispWrap.overflow = 'hidden';
735 this.footDisp = fel.createChild();
736 this.footDispWrap.on('click', this.onContextClick, this)
740 onContextClick : function (ev,dom)
743 var cn = dom.className;
745 if (!cn.match(/x-ed-loc-/)) {
748 var n = cn.split('-').pop();
749 var ans = this.footerEls;
753 var range = this.editorcore.createRange();
755 range.selectNodeContents(sel);
756 //range.selectNode(sel);
759 var selection = this.editorcore.getSelection();
760 selection.removeAllRanges();
761 selection.addRange(range);
765 this.updateToolbar(null, null, sel);