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 || {};
41 Roo.form.HtmlEditor.ToolbarContext.fontFamilies = [
50 Roo.form.HtmlEditor.ToolbarContext.types = {
62 opts : [ [""],[ "left"],[ "right"],[ "center"],[ "top"]],
124 opts : [[""],[ "left"],[ "center"],[ "right"],[ "justify"],[ "char"]],
129 opts : [[""],[ "top"],[ "middle"],[ "bottom"],[ "baseline"]],
183 // should we really allow this??
184 // should this just be
199 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
207 * @cfg {Object} disable List of toolbar elements to disable
212 * @cfg {Object} styles List of styles
213 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
215 * These must be defined in the page, so they get rendered correctly..
226 init : function(editor)
228 this.editor = editor;
231 var fid = editor.frameId;
233 function btn(id, toggle, handler){
234 var xid = fid + '-'+ id ;
238 cls : 'x-btn-icon x-edit-'+id,
239 enableToggle:toggle !== false,
240 scope: editor, // was editor...
241 handler:handler||editor.relayBtnCmd,
242 clickEvent:'mousedown',
243 tooltip: etb.buttonTips[id] || undefined, ///tips ???
247 // create a new element.
248 var wdiv = editor.wrap.createChild({
250 }, editor.wrap.dom.firstChild.nextSibling, true);
252 // can we do this more than once??
257 // disable everything...
258 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
263 this.toolbars[i] = this.buildToolbar(ty[i],i);
265 this.tb = this.toolbars.BODY;
269 editor.on('hide', function( ) { this.footer.hide() }, this);
270 editor.on('show', function( ) { this.footer.show() }, this);
273 this.rendered = true;
276 editor.on('editorevent', this.updateToolbar, this);
277 // other toolbars need to implement this..
278 //editor.on('editmodechange', this.updateToolbar, this);
284 * Protected method that will not generally be called directly. It triggers
285 * a toolbar update by reading the markup state of the current selection in the editor.
287 updateToolbar: function(editor,ev,sel){
290 // capture mouse up - this is handy for selecting images..
291 // perhaps should go somewhere else...
292 if(!this.editor.activated){
293 this.editor.onFirstFocus();
297 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
298 // selectNode - might want to handle IE?
300 (ev.type == 'mouseup' || ev.type == 'click' ) &&
301 ev.target && ev.target.tagName == 'IMG') {
302 // they have click on an image...
303 // let's see if we can change the selection...
306 var nodeRange = sel.ownerDocument.createRange();
308 nodeRange.selectNode(sel);
310 nodeRange.selectNodeContents(sel);
312 //nodeRange.collapse(true);
313 var s = editor.win.getSelection();
315 s.addRange(nodeRange);
319 var updateFooter = sel ? false : true;
322 var ans = this.editor.getAllAncestors();
325 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
328 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
329 sel = sel ? sel : this.editor.doc.body;
330 sel = sel.tagName.length ? sel : this.editor.doc.body;
333 // pick a menu that exists..
334 var tn = sel.tagName.toUpperCase();
335 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
337 tn = sel.tagName.toUpperCase();
339 var lastSel = this.tb.selectedNode
341 this.tb.selectedNode = sel;
343 // if current menu does not match..
344 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
347 ///console.log("show: " + tn);
348 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
351 this.tb.items.first().el.innerHTML = tn + ': ';
355 if (this.tb.fields) {
356 this.tb.fields.each(function(e) {
357 e.setValue(sel.getAttribute(e.attrname));
361 var hasStyles = false;
362 for(var i in this.styles) {
369 var st = this.tb.fields.item(0);
371 st.store.removeAll();
374 var cn = sel.className.split(/\s+/);
377 if (this.styles['*']) {
379 Roo.each(this.styles['*'], function(v) {
380 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
383 if (this.styles[tn]) {
384 Roo.each(this.styles[tn], function(v) {
385 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
389 st.store.loadData(avs);
393 // flag our selected Node.
394 this.tb.selectedNode = sel;
397 Roo.menu.MenuMgr.hideAll();
402 //this.footDisp.dom.innerHTML = '';
409 this.footerEls = ans.reverse();
410 Roo.each(this.footerEls, function(a,i) {
412 html += html.length ? ' > ' : '';
414 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
419 var sz = this.footDisp.up('td').getSize();
420 this.footDisp.dom.style.width = (sz.width -10) + 'px';
421 this.footDisp.dom.style.marginLeft = '5px';
423 this.footDisp.dom.style.overflow = 'hidden';
425 this.footDisp.dom.innerHTML = html;
427 //this.editorsyncValue();
434 onDestroy : function(){
437 this.tb.items.each(function(item){
439 item.menu.removeAll();
441 item.menu.el.destroy();
449 onFirstFocus: function() {
450 // need to do this for all the toolbars..
451 this.tb.items.each(function(item){
455 buildToolbar: function(tlist, nm)
457 var editor = this.editor;
458 // create a new element.
459 var wdiv = editor.wrap.createChild({
461 }, editor.wrap.dom.firstChild.nextSibling, true);
464 var tb = new Roo.Toolbar(wdiv);
467 tb.add(nm+ ": ");
470 for(var i in this.styles) {
475 if (styles && styles.length) {
477 // this needs a multi-select checkbox...
478 tb.addField( new Roo.form.ComboBox({
479 store: new Roo.data.SimpleStore({
481 fields: ['val', 'selected'],
484 name : '-roo-edit-className',
485 attrname : 'className',
490 triggerAction: 'all',
491 emptyText:'Select Style',
495 'select': function(c, r, i) {
496 // initial support only for on class per el..
497 tb.selectedNode.className = r ? r.get('val') : '';
507 for (var i in tlist) {
510 tb.add(item.title + ": ");
516 // opts == pulldown..
517 tb.addField( new Roo.form.ComboBox({
518 store: new Roo.data.SimpleStore({
523 name : '-roo-edit-' + i,
529 triggerAction: 'all',
532 width: item.width ? item.width : 130,
534 'select': function(c, r, i) {
535 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
544 tb.addField( new Roo.form.TextField({
552 tb.addField( new Roo.form.TextField({
553 name: '-roo-edit-' + i,
560 'change' : function(f, nv, ov) {
561 tb.selectedNode.setAttribute(f.attrname, nv);
576 // undo does not work.
578 var sn = tb.selectedNode;
580 var pn = sn.parentNode;
582 var stn = sn.childNodes[0];
583 var en = sn.childNodes[sn.childNodes.length - 1 ];
584 while (sn.childNodes.length) {
585 var node = sn.childNodes[0];
586 sn.removeChild(node);
588 pn.insertBefore(node, sn);
592 var range = editor.createRange();
594 range.setStart(stn,0);
595 range.setEnd(en,0); //????
596 //range.selectNode(sel);
599 var selection = editor.getSelection();
600 selection.removeAllRanges();
601 selection.addRange(range);
605 //_this.updateToolbar(null, null, pn);
606 _this.updateToolbar(null, null, null);
607 this.footDisp.dom.innerHTML = '';
617 tb.el.on('click', function(e){
618 e.preventDefault(); // what does this do?
620 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
623 // dont need to disable them... as they will get hidden
628 buildFooter : function()
631 var fel = this.editor.wrap.createChild();
632 this.footer = new Roo.Toolbar(fel);
633 // toolbar has scrolly on left / right?
634 var footDisp= new Roo.Toolbar.Fill();
640 handler : function() {
641 _t.footDisp.scrollTo('left',0,true)
645 this.footer.add( footDisp );
650 handler : function() {
652 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
656 var fel = Roo.get(footDisp.el);
657 fel.addClass('x-editor-context');
658 this.footDispWrap = fel;
659 this.footDispWrap.overflow = 'hidden';
661 this.footDisp = fel.createChild();
662 this.footDispWrap.on('click', this.onContextClick, this)
666 onContextClick : function (ev,dom)
669 var cn = dom.className;
671 if (!cn.match(/x-ed-loc-/)) {
674 var n = cn.split('-').pop();
675 var ans = this.footerEls;
679 var range = this.editor.createRange();
681 range.selectNodeContents(sel);
682 //range.selectNode(sel);
685 var selection = this.editor.getSelection();
686 selection.removeAllRanges();
687 selection.addRange(range);
691 this.updateToolbar(null, null, sel);