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 var cx= Roo.form.HtmlEditor.ToolbarContext;
206 if (typeof(cx.types[tg]) == 'undefined') {
210 cx.types[tg]['font-family'] = cx[ 'font-family'] ;
213 Roo.form.HtmlEditor.ToolbarContext.types
216 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
224 * @cfg {Object} disable List of toolbar elements to disable
229 * @cfg {Object} styles List of styles
230 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
232 * These must be defined in the page, so they get rendered correctly..
243 init : function(editor)
245 this.editor = editor;
248 var fid = editor.frameId;
250 function btn(id, toggle, handler){
251 var xid = fid + '-'+ id ;
255 cls : 'x-btn-icon x-edit-'+id,
256 enableToggle:toggle !== false,
257 scope: editor, // was editor...
258 handler:handler||editor.relayBtnCmd,
259 clickEvent:'mousedown',
260 tooltip: etb.buttonTips[id] || undefined, ///tips ???
264 // create a new element.
265 var wdiv = editor.wrap.createChild({
267 }, editor.wrap.dom.firstChild.nextSibling, true);
269 // can we do this more than once??
274 // disable everything...
275 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
280 this.toolbars[i] = this.buildToolbar(ty[i],i);
282 this.tb = this.toolbars.BODY;
286 editor.on('hide', function( ) { this.footer.hide() }, this);
287 editor.on('show', function( ) { this.footer.show() }, this);
290 this.rendered = true;
293 editor.on('editorevent', this.updateToolbar, this);
294 // other toolbars need to implement this..
295 //editor.on('editmodechange', this.updateToolbar, this);
301 * Protected method that will not generally be called directly. It triggers
302 * a toolbar update by reading the markup state of the current selection in the editor.
304 updateToolbar: function(editor,ev,sel){
307 // capture mouse up - this is handy for selecting images..
308 // perhaps should go somewhere else...
309 if(!this.editor.activated){
310 this.editor.onFirstFocus();
314 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
315 // selectNode - might want to handle IE?
317 (ev.type == 'mouseup' || ev.type == 'click' ) &&
318 ev.target && ev.target.tagName == 'IMG') {
319 // they have click on an image...
320 // let's see if we can change the selection...
323 var nodeRange = sel.ownerDocument.createRange();
325 nodeRange.selectNode(sel);
327 nodeRange.selectNodeContents(sel);
329 //nodeRange.collapse(true);
330 var s = editor.win.getSelection();
332 s.addRange(nodeRange);
336 var updateFooter = sel ? false : true;
339 var ans = this.editor.getAllAncestors();
342 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
345 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
346 sel = sel ? sel : this.editor.doc.body;
347 sel = sel.tagName.length ? sel : this.editor.doc.body;
350 // pick a menu that exists..
351 var tn = sel.tagName.toUpperCase();
352 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
354 tn = sel.tagName.toUpperCase();
356 var lastSel = this.tb.selectedNode
358 this.tb.selectedNode = sel;
360 // if current menu does not match..
361 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
364 ///console.log("show: " + tn);
365 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
368 this.tb.items.first().el.innerHTML = tn + ': ';
372 if (this.tb.fields) {
373 this.tb.fields.each(function(e) {
374 e.setValue(sel.getAttribute(e.attrname));
378 var hasStyles = false;
379 for(var i in this.styles) {
386 var st = this.tb.fields.item(0);
388 st.store.removeAll();
391 var cn = sel.className.split(/\s+/);
394 if (this.styles['*']) {
396 Roo.each(this.styles['*'], function(v) {
397 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
400 if (this.styles[tn]) {
401 Roo.each(this.styles[tn], function(v) {
402 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
406 st.store.loadData(avs);
410 // flag our selected Node.
411 this.tb.selectedNode = sel;
414 Roo.menu.MenuMgr.hideAll();
419 //this.footDisp.dom.innerHTML = '';
426 this.footerEls = ans.reverse();
427 Roo.each(this.footerEls, function(a,i) {
429 html += html.length ? ' > ' : '';
431 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
436 var sz = this.footDisp.up('td').getSize();
437 this.footDisp.dom.style.width = (sz.width -10) + 'px';
438 this.footDisp.dom.style.marginLeft = '5px';
440 this.footDisp.dom.style.overflow = 'hidden';
442 this.footDisp.dom.innerHTML = html;
444 //this.editorsyncValue();
451 onDestroy : function(){
454 this.tb.items.each(function(item){
456 item.menu.removeAll();
458 item.menu.el.destroy();
466 onFirstFocus: function() {
467 // need to do this for all the toolbars..
468 this.tb.items.each(function(item){
472 buildToolbar: function(tlist, nm)
474 var editor = this.editor;
475 // create a new element.
476 var wdiv = editor.wrap.createChild({
478 }, editor.wrap.dom.firstChild.nextSibling, true);
481 var tb = new Roo.Toolbar(wdiv);
484 tb.add(nm+ ": ");
487 for(var i in this.styles) {
492 if (styles && styles.length) {
494 // this needs a multi-select checkbox...
495 tb.addField( new Roo.form.ComboBox({
496 store: new Roo.data.SimpleStore({
498 fields: ['val', 'selected'],
501 name : '-roo-edit-className',
502 attrname : 'className',
507 triggerAction: 'all',
508 emptyText:'Select Style',
512 'select': function(c, r, i) {
513 // initial support only for on class per el..
514 tb.selectedNode.className = r ? r.get('val') : '';
524 for (var i in tlist) {
527 tb.add(item.title + ": ");
533 // opts == pulldown..
534 tb.addField( new Roo.form.ComboBox({
535 store: new Roo.data.SimpleStore({
540 name : '-roo-edit-' + i,
546 triggerAction: 'all',
549 width: item.width ? item.width : 130,
551 'select': function(c, r, i) {
552 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
561 tb.addField( new Roo.form.TextField({
569 tb.addField( new Roo.form.TextField({
570 name: '-roo-edit-' + i,
577 'change' : function(f, nv, ov) {
578 tb.selectedNode.setAttribute(f.attrname, nv);
593 // undo does not work.
595 var sn = tb.selectedNode;
597 var pn = sn.parentNode;
599 var stn = sn.childNodes[0];
600 var en = sn.childNodes[sn.childNodes.length - 1 ];
601 while (sn.childNodes.length) {
602 var node = sn.childNodes[0];
603 sn.removeChild(node);
605 pn.insertBefore(node, sn);
609 var range = editor.createRange();
611 range.setStart(stn,0);
612 range.setEnd(en,0); //????
613 //range.selectNode(sel);
616 var selection = editor.getSelection();
617 selection.removeAllRanges();
618 selection.addRange(range);
622 //_this.updateToolbar(null, null, pn);
623 _this.updateToolbar(null, null, null);
624 this.footDisp.dom.innerHTML = '';
634 tb.el.on('click', function(e){
635 e.preventDefault(); // what does this do?
637 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
640 // dont need to disable them... as they will get hidden
645 buildFooter : function()
648 var fel = this.editor.wrap.createChild();
649 this.footer = new Roo.Toolbar(fel);
650 // toolbar has scrolly on left / right?
651 var footDisp= new Roo.Toolbar.Fill();
657 handler : function() {
658 _t.footDisp.scrollTo('left',0,true)
662 this.footer.add( footDisp );
667 handler : function() {
669 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
673 var fel = Roo.get(footDisp.el);
674 fel.addClass('x-editor-context');
675 this.footDispWrap = fel;
676 this.footDispWrap.overflow = 'hidden';
678 this.footDisp = fel.createChild();
679 this.footDispWrap.on('click', this.onContextClick, this)
683 onContextClick : function (ev,dom)
686 var cn = dom.className;
688 if (!cn.match(/x-ed-loc-/)) {
691 var n = cn.split('-').pop();
692 var ans = this.footerEls;
696 var range = this.editor.createRange();
698 range.selectNodeContents(sel);
699 //range.selectNode(sel);
702 var selection = this.editor.getSelection();
703 selection.removeAllRanges();
704 selection.addRange(range);
708 this.updateToolbar(null, null, sel);