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"]],
176 // should we really allow this??
177 // should this just be
190 Roo.form.HtmlEditor.ToolbarContext[ 'font-family'] = {
197 [ 'Times New Roman'],
204 Roo.each([ 'SPAN', 'DIV' , 'TD' , 'P' ], function(tg) {
205 if (typeof(Roo.form.HtmlEditor.ToolbarContext.types[tg]) == 'undefined') {
206 Roo.form.HtmlEditor.ToolbarContext.types[tg] = {};
209 Roo.form.HtmlEditor.ToolbarContext.types[tg]['font-family'] = Roo.form.HtmlEditor.ToolbarContext[ 'font-family'] ;
212 Roo.form.HtmlEditor.ToolbarContext.types
215 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
223 * @cfg {Object} disable List of toolbar elements to disable
228 * @cfg {Object} styles List of styles
229 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
231 * These must be defined in the page, so they get rendered correctly..
242 init : function(editor)
244 this.editor = editor;
247 var fid = editor.frameId;
249 function btn(id, toggle, handler){
250 var xid = fid + '-'+ id ;
254 cls : 'x-btn-icon x-edit-'+id,
255 enableToggle:toggle !== false,
256 scope: editor, // was editor...
257 handler:handler||editor.relayBtnCmd,
258 clickEvent:'mousedown',
259 tooltip: etb.buttonTips[id] || undefined, ///tips ???
263 // create a new element.
264 var wdiv = editor.wrap.createChild({
266 }, editor.wrap.dom.firstChild.nextSibling, true);
268 // can we do this more than once??
273 // disable everything...
274 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
279 this.toolbars[i] = this.buildToolbar(ty[i],i);
281 this.tb = this.toolbars.BODY;
285 editor.on('hide', function( ) { this.footer.hide() }, this);
286 editor.on('show', function( ) { this.footer.show() }, this);
289 this.rendered = true;
292 editor.on('editorevent', this.updateToolbar, this);
293 // other toolbars need to implement this..
294 //editor.on('editmodechange', this.updateToolbar, this);
300 * Protected method that will not generally be called directly. It triggers
301 * a toolbar update by reading the markup state of the current selection in the editor.
303 updateToolbar: function(editor,ev,sel){
306 // capture mouse up - this is handy for selecting images..
307 // perhaps should go somewhere else...
308 if(!this.editor.activated){
309 this.editor.onFirstFocus();
313 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
314 // selectNode - might want to handle IE?
316 (ev.type == 'mouseup' || ev.type == 'click' ) &&
317 ev.target && ev.target.tagName == 'IMG') {
318 // they have click on an image...
319 // let's see if we can change the selection...
322 var nodeRange = sel.ownerDocument.createRange();
324 nodeRange.selectNode(sel);
326 nodeRange.selectNodeContents(sel);
328 //nodeRange.collapse(true);
329 var s = editor.win.getSelection();
331 s.addRange(nodeRange);
335 var updateFooter = sel ? false : true;
338 var ans = this.editor.getAllAncestors();
341 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
344 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
345 sel = sel ? sel : this.editor.doc.body;
346 sel = sel.tagName.length ? sel : this.editor.doc.body;
349 // pick a menu that exists..
350 var tn = sel.tagName.toUpperCase();
351 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
353 tn = sel.tagName.toUpperCase();
355 var lastSel = this.tb.selectedNode
357 this.tb.selectedNode = sel;
359 // if current menu does not match..
360 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
363 ///console.log("show: " + tn);
364 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
367 this.tb.items.first().el.innerHTML = tn + ': ';
371 if (this.tb.fields) {
372 this.tb.fields.each(function(e) {
373 e.setValue(sel.getAttribute(e.attrname));
377 var hasStyles = false;
378 for(var i in this.styles) {
385 var st = this.tb.fields.item(0);
387 st.store.removeAll();
390 var cn = sel.className.split(/\s+/);
393 if (this.styles['*']) {
395 Roo.each(this.styles['*'], function(v) {
396 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
399 if (this.styles[tn]) {
400 Roo.each(this.styles[tn], function(v) {
401 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
405 st.store.loadData(avs);
409 // flag our selected Node.
410 this.tb.selectedNode = sel;
413 Roo.menu.MenuMgr.hideAll();
418 //this.footDisp.dom.innerHTML = '';
425 this.footerEls = ans.reverse();
426 Roo.each(this.footerEls, function(a,i) {
428 html += html.length ? ' > ' : '';
430 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
435 var sz = this.footDisp.up('td').getSize();
436 this.footDisp.dom.style.width = (sz.width -10) + 'px';
437 this.footDisp.dom.style.marginLeft = '5px';
439 this.footDisp.dom.style.overflow = 'hidden';
441 this.footDisp.dom.innerHTML = html;
443 //this.editorsyncValue();
450 onDestroy : function(){
453 this.tb.items.each(function(item){
455 item.menu.removeAll();
457 item.menu.el.destroy();
465 onFirstFocus: function() {
466 // need to do this for all the toolbars..
467 this.tb.items.each(function(item){
471 buildToolbar: function(tlist, nm)
473 var editor = this.editor;
474 // create a new element.
475 var wdiv = editor.wrap.createChild({
477 }, editor.wrap.dom.firstChild.nextSibling, true);
480 var tb = new Roo.Toolbar(wdiv);
483 tb.add(nm+ ": ");
486 for(var i in this.styles) {
491 if (styles && styles.length) {
493 // this needs a multi-select checkbox...
494 tb.addField( new Roo.form.ComboBox({
495 store: new Roo.data.SimpleStore({
497 fields: ['val', 'selected'],
500 name : '-roo-edit-className',
501 attrname : 'className',
506 triggerAction: 'all',
507 emptyText:'Select Style',
511 'select': function(c, r, i) {
512 // initial support only for on class per el..
513 tb.selectedNode.className = r ? r.get('val') : '';
523 for (var i in tlist) {
526 tb.add(item.title + ": ");
532 // opts == pulldown..
533 tb.addField( new Roo.form.ComboBox({
534 store: new Roo.data.SimpleStore({
539 name : '-roo-edit-' + i,
545 triggerAction: 'all',
548 width: item.width ? item.width : 130,
550 'select': function(c, r, i) {
551 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
560 tb.addField( new Roo.form.TextField({
568 tb.addField( new Roo.form.TextField({
569 name: '-roo-edit-' + i,
576 'change' : function(f, nv, ov) {
577 tb.selectedNode.setAttribute(f.attrname, nv);
592 // undo does not work.
594 var sn = tb.selectedNode;
596 var pn = sn.parentNode;
598 var stn = sn.childNodes[0];
599 var en = sn.childNodes[sn.childNodes.length - 1 ];
600 while (sn.childNodes.length) {
601 var node = sn.childNodes[0];
602 sn.removeChild(node);
604 pn.insertBefore(node, sn);
608 var range = editor.createRange();
610 range.setStart(stn,0);
611 range.setEnd(en,0); //????
612 //range.selectNode(sel);
615 var selection = editor.getSelection();
616 selection.removeAllRanges();
617 selection.addRange(range);
621 //_this.updateToolbar(null, null, pn);
622 _this.updateToolbar(null, null, null);
623 this.footDisp.dom.innerHTML = '';
633 tb.el.on('click', function(e){
634 e.preventDefault(); // what does this do?
636 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
639 // dont need to disable them... as they will get hidden
644 buildFooter : function()
647 var fel = this.editor.wrap.createChild();
648 this.footer = new Roo.Toolbar(fel);
649 // toolbar has scrolly on left / right?
650 var footDisp= new Roo.Toolbar.Fill();
656 handler : function() {
657 _t.footDisp.scrollTo('left',0,true)
661 this.footer.add( footDisp );
666 handler : function() {
668 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
672 var fel = Roo.get(footDisp.el);
673 fel.addClass('x-editor-context');
674 this.footDispWrap = fel;
675 this.footDispWrap.overflow = 'hidden';
677 this.footDisp = fel.createChild();
678 this.footDispWrap.on('click', this.onContextClick, this)
682 onContextClick : function (ev,dom)
685 var cn = dom.className;
687 if (!cn.match(/x-ed-loc-/)) {
690 var n = cn.split('-').pop();
691 var ans = this.footerEls;
695 var range = this.editor.createRange();
697 range.selectNodeContents(sel);
698 //range.selectNode(sel);
701 var selection = this.editor.getSelection();
702 selection.removeAllRanges();
703 selection.addRange(range);
707 this.updateToolbar(null, null, sel);