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 || {};
40 Roo.form.HtmlEditor.ToolbarContext.types = {
52 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
114 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
119 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
173 // should we really allow this??
174 // should this just be
189 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
197 * @cfg {Object} disable List of toolbar elements to disable
202 * @cfg {Object} styles List of styles
203 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
205 * These must be defined in the page, so they get rendered correctly..
216 init : function(editor)
218 this.editor = editor;
221 var fid = editor.frameId;
223 function btn(id, toggle, handler){
224 var xid = fid + '-'+ id ;
228 cls : 'x-btn-icon x-edit-'+id,
229 enableToggle:toggle !== false,
230 scope: editor, // was editor...
231 handler:handler||editor.relayBtnCmd,
232 clickEvent:'mousedown',
233 tooltip: etb.buttonTips[id] || undefined, ///tips ???
237 // create a new element.
238 var wdiv = editor.wrap.createChild({
240 }, editor.wrap.dom.firstChild.nextSibling, true);
242 // can we do this more than once??
247 // disable everything...
248 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
253 this.toolbars[i] = this.buildToolbar(ty[i],i);
255 this.tb = this.toolbars.BODY;
259 editor.on('hide', function( ) { this.footer.hide() }, this);
260 editor.on('show', function( ) { this.footer.show() }, this);
263 this.rendered = true;
266 editor.on('editorevent', this.updateToolbar, this);
267 // other toolbars need to implement this..
268 //editor.on('editmodechange', this.updateToolbar, this);
274 * Protected method that will not generally be called directly. It triggers
275 * a toolbar update by reading the markup state of the current selection in the editor.
277 updateToolbar: function(editor,ev,sel){
280 // capture mouse up - this is handy for selecting images..
281 // perhaps should go somewhere else...
282 if(!this.editor.activated){
283 this.editor.onFirstFocus();
287 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
288 // selectNode - might want to handle IE?
290 (ev.type == 'mouseup' || ev.type == 'click' ) &&
291 ev.target && ev.target.tagName == 'IMG') {
292 // they have click on an image...
293 // let's see if we can change the selection...
296 var nodeRange = sel.ownerDocument.createRange();
298 nodeRange.selectNode(sel);
300 nodeRange.selectNodeContents(sel);
302 //nodeRange.collapse(true);
303 var s = editor.win.getSelection();
305 s.addRange(nodeRange);
309 var updateFooter = sel ? false : true;
312 var ans = this.editor.getAllAncestors();
315 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
318 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
319 sel = sel ? sel : this.editor.doc.body;
320 sel = sel.tagName.length ? sel : this.editor.doc.body;
323 // pick a menu that exists..
324 var tn = sel.tagName.toUpperCase();
325 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
327 tn = sel.tagName.toUpperCase();
329 var lastSel = this.tb.selectedNode
331 this.tb.selectedNode = sel;
333 // if current menu does not match..
334 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
337 ///console.log("show: " + tn);
338 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
341 this.tb.items.first().el.innerHTML = tn + ': ';
345 if (this.tb.fields) {
346 this.tb.fields.each(function(e) {
347 e.setValue(sel.getAttribute(e.attrname));
351 var hasStyles = false;
352 for(var i in this.styles) {
359 var st = this.tb.fields.item(0);
361 st.store.removeAll();
364 var cn = sel.className.split(/\s+/);
367 if (this.styles['*']) {
369 Roo.each(this.styles['*'], function(v) {
370 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
373 if (this.styles[tn]) {
374 Roo.each(this.styles[tn], function(v) {
375 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
379 st.store.loadData(avs);
383 // flag our selected Node.
384 this.tb.selectedNode = sel;
387 Roo.menu.MenuMgr.hideAll();
392 //this.footDisp.dom.innerHTML = '';
399 this.footerEls = ans.reverse();
400 Roo.each(this.footerEls, function(a,i) {
402 html += html.length ? ' > ' : '';
404 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
409 var sz = this.footDisp.up('td').getSize();
410 this.footDisp.dom.style.width = (sz.width -10) + 'px';
411 this.footDisp.dom.style.marginLeft = '5px';
413 this.footDisp.dom.style.overflow = 'hidden';
415 this.footDisp.dom.innerHTML = html;
417 //this.editorsyncValue();
424 onDestroy : function(){
427 this.tb.items.each(function(item){
429 item.menu.removeAll();
431 item.menu.el.destroy();
439 onFirstFocus: function() {
440 // need to do this for all the toolbars..
441 this.tb.items.each(function(item){
445 buildToolbar: function(tlist, nm)
447 var editor = this.editor;
448 // create a new element.
449 var wdiv = editor.wrap.createChild({
451 }, editor.wrap.dom.firstChild.nextSibling, true);
454 var tb = new Roo.Toolbar(wdiv);
457 tb.add(nm+ ": ");
460 for(var i in this.styles) {
465 if (styles && styles.length) {
467 // this needs a multi-select checkbox...
468 tb.addField( new Roo.form.ComboBox({
469 store: new Roo.data.SimpleStore({
471 fields: ['val', 'selected'],
474 name : '-roo-edit-className',
475 attrname : 'className',
480 triggerAction: 'all',
481 emptyText:'Select Style',
485 'select': function(c, r, i) {
486 // initial support only for on class per el..
487 tb.selectedNode.className = r ? r.get('val') : '';
497 for (var i in tlist) {
500 tb.add(item.title + ": ");
506 // opts == pulldown..
507 tb.addField( new Roo.form.ComboBox({
508 store: new Roo.data.SimpleStore({
513 name : '-roo-edit-' + i,
519 triggerAction: 'all',
522 width: item.width ? item.width : 130,
524 'select': function(c, r, i) {
525 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
534 tb.addField( new Roo.form.TextField({
542 tb.addField( new Roo.form.TextField({
543 name: '-roo-edit-' + i,
550 'change' : function(f, nv, ov) {
551 tb.selectedNode.setAttribute(f.attrname, nv);
566 // undo does not work.
568 var sn = tb.selectedNode;
570 var pn = sn.parentNode;
572 var stn = sn.childNodes[0];
573 var en = sn.childNodes[sn.childNodes.length - 1 ];
574 while (sn.childNodes.length) {
575 var node = sn.childNodes[0];
576 sn.removeChild(node);
578 pn.insertBefore(node, sn);
582 var range = editor.createRange();
584 range.setStart(stn,0);
585 range.setEnd(en,0); //????
586 //range.selectNode(sel);
589 var selection = editor.getSelection();
590 selection.removeAllRanges();
591 selection.addRange(range);
595 //_this.updateToolbar(null, null, pn);
596 _this.updateToolbar(null, null, pn);
597 this.footDisp.dom.innerHTML = '';
607 tb.el.on('click', function(e){
608 e.preventDefault(); // what does this do?
610 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
613 // dont need to disable them... as they will get hidden
618 buildFooter : function()
621 var fel = this.editor.wrap.createChild();
622 this.footer = new Roo.Toolbar(fel);
623 // toolbar has scrolly on left / right?
624 var footDisp= new Roo.Toolbar.Fill();
630 handler : function() {
631 _t.footDisp.scrollTo('left',0,true)
635 this.footer.add( footDisp );
640 handler : function() {
642 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
646 var fel = Roo.get(footDisp.el);
647 fel.addClass('x-editor-context');
648 this.footDispWrap = fel;
649 this.footDispWrap.overflow = 'hidden';
651 this.footDisp = fel.createChild();
652 this.footDispWrap.on('click', this.onContextClick, this)
656 onContextClick : function (ev,dom)
659 var cn = dom.className;
661 if (!cn.match(/x-ed-loc-/)) {
664 var n = cn.split('-').pop();
665 var ans = this.footerEls;
669 var range = this.editor.createRange();
671 range.selectNodeContents(sel);
672 //range.selectNode(sel);
675 var selection = this.editor.getSelection();
676 selection.removeAllRanges();
677 selection.addRange(range);
681 this.updateToolbar(null, null, sel);