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"]],
132 style : 'fontFamily',
133 displayField: 'display',
134 optname : 'font-family',
183 // should we really allow this??
184 // should this just be
195 style : 'fontFamily',
196 displayField: 'display',
197 optname : 'font-family',
204 style : 'fontFamily',
205 displayField: 'display',
206 optname : 'font-family',
213 style : 'fontFamily',
214 displayField: 'display',
215 optname : 'font-family',
226 // this should be configurable.. - you can either set it up using stores, or modify options somehwere..
227 Roo.form.HtmlEditor.ToolbarContext.stores = false;
229 Roo.form.HtmlEditor.ToolbarContext.options = {
231 [ 'Helvetica,Arial,sans-serif', 'Helvetica'],
232 [ 'Courier New', 'Courier New'],
233 [ 'Tahoma', 'Tahoma'],
234 [ 'Times New Roman,serif', 'Times'],
235 [ 'Verdana','Verdana' ]
239 // fixme - these need to be configurable..
242 Roo.form.HtmlEditor.ToolbarContext.types
245 Roo.apply(Roo.form.HtmlEditor.ToolbarContext.prototype, {
253 * @cfg {Object} disable List of toolbar elements to disable
258 * @cfg {Object} styles List of styles
259 * eg. { '*' : [ 'headline' ] , 'TD' : [ 'underline', 'double-underline' ] }
261 * These must be defined in the page, so they get rendered correctly..
272 init : function(editor)
274 this.editor = editor;
277 var fid = editor.frameId;
279 function btn(id, toggle, handler){
280 var xid = fid + '-'+ id ;
284 cls : 'x-btn-icon x-edit-'+id,
285 enableToggle:toggle !== false,
286 scope: editor, // was editor...
287 handler:handler||editor.relayBtnCmd,
288 clickEvent:'mousedown',
289 tooltip: etb.buttonTips[id] || undefined, ///tips ???
293 // create a new element.
294 var wdiv = editor.wrap.createChild({
296 }, editor.wrap.dom.firstChild.nextSibling, true);
298 // can we do this more than once??
303 // disable everything...
304 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
309 this.toolbars[i] = this.buildToolbar(ty[i],i);
311 this.tb = this.toolbars.BODY;
315 editor.on('hide', function( ) { this.footer.hide() }, this);
316 editor.on('show', function( ) { this.footer.show() }, this);
319 this.rendered = true;
322 editor.on('editorevent', this.updateToolbar, this);
323 // other toolbars need to implement this..
324 //editor.on('editmodechange', this.updateToolbar, this);
330 * Protected method that will not generally be called directly. It triggers
331 * a toolbar update by reading the markup state of the current selection in the editor.
333 updateToolbar: function(editor,ev,sel){
336 // capture mouse up - this is handy for selecting images..
337 // perhaps should go somewhere else...
338 if(!this.editor.activated){
339 this.editor.onFirstFocus();
343 // http://developer.yahoo.com/yui/docs/simple-editor.js.html
344 // selectNode - might want to handle IE?
346 (ev.type == 'mouseup' || ev.type == 'click' ) &&
347 ev.target && ev.target.tagName == 'IMG') {
348 // they have click on an image...
349 // let's see if we can change the selection...
352 var nodeRange = sel.ownerDocument.createRange();
354 nodeRange.selectNode(sel);
356 nodeRange.selectNodeContents(sel);
358 //nodeRange.collapse(true);
359 var s = editor.win.getSelection();
361 s.addRange(nodeRange);
365 var updateFooter = sel ? false : true;
368 var ans = this.editor.getAllAncestors();
371 var ty= Roo.form.HtmlEditor.ToolbarContext.types;
374 sel = ans.length ? (ans[0] ? ans[0] : ans[1]) : this.editor.doc.body;
375 sel = sel ? sel : this.editor.doc.body;
376 sel = sel.tagName.length ? sel : this.editor.doc.body;
379 // pick a menu that exists..
380 var tn = sel.tagName.toUpperCase();
381 //sel = typeof(ty[tn]) != 'undefined' ? sel : this.editor.doc.body;
383 tn = sel.tagName.toUpperCase();
385 var lastSel = this.tb.selectedNode
387 this.tb.selectedNode = sel;
389 // if current menu does not match..
390 if ((this.tb.name != tn) || (lastSel != this.tb.selectedNode)) {
393 ///console.log("show: " + tn);
394 this.tb = typeof(ty[tn]) != 'undefined' ? this.toolbars[tn] : this.toolbars['*'];
397 this.tb.items.first().el.innerHTML = tn + ': ';
401 if (this.tb.fields) {
402 this.tb.fields.each(function(e) {
404 e.setValue(sel.style[e.stylename]);
407 e.setValue(sel.getAttribute(e.attrname));
411 var hasStyles = false;
412 for(var i in this.styles) {
419 var st = this.tb.fields.item(0);
421 st.store.removeAll();
424 var cn = sel.className.split(/\s+/);
427 if (this.styles['*']) {
429 Roo.each(this.styles['*'], function(v) {
430 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
433 if (this.styles[tn]) {
434 Roo.each(this.styles[tn], function(v) {
435 avs.push( [ v , cn.indexOf(v) > -1 ? 1 : 0 ] );
439 st.store.loadData(avs);
443 // flag our selected Node.
444 this.tb.selectedNode = sel;
447 Roo.menu.MenuMgr.hideAll();
452 //this.footDisp.dom.innerHTML = '';
459 this.footerEls = ans.reverse();
460 Roo.each(this.footerEls, function(a,i) {
462 html += html.length ? ' > ' : '';
464 html += '<span class="x-ed-loc-' + i + '">' + a.tagName + '</span>';
469 var sz = this.footDisp.up('td').getSize();
470 this.footDisp.dom.style.width = (sz.width -10) + 'px';
471 this.footDisp.dom.style.marginLeft = '5px';
473 this.footDisp.dom.style.overflow = 'hidden';
475 this.footDisp.dom.innerHTML = html;
477 //this.editorsyncValue();
484 onDestroy : function(){
487 this.tb.items.each(function(item){
489 item.menu.removeAll();
491 item.menu.el.destroy();
499 onFirstFocus: function() {
500 // need to do this for all the toolbars..
501 this.tb.items.each(function(item){
505 buildToolbar: function(tlist, nm)
507 var editor = this.editor;
508 // create a new element.
509 var wdiv = editor.wrap.createChild({
511 }, editor.wrap.dom.firstChild.nextSibling, true);
514 var tb = new Roo.Toolbar(wdiv);
517 tb.add(nm+ ": ");
520 for(var i in this.styles) {
525 if (styles && styles.length) {
527 // this needs a multi-select checkbox...
528 tb.addField( new Roo.form.ComboBox({
529 store: new Roo.data.SimpleStore({
531 fields: ['val', 'selected'],
534 name : '-roo-edit-className',
535 attrname : 'className',
540 triggerAction: 'all',
541 emptyText:'Select Style',
545 'select': function(c, r, i) {
546 // initial support only for on class per el..
547 tb.selectedNode.className = r ? r.get('val') : '';
555 var tbops = Roo.form.HtmlEditor.ToolbarContext.options;
557 for (var i in tlist) {
560 tb.add(item.title + ": ");
563 //optname == used so you can configure the options available..
564 var opts = item.opts ? item.opts : false;
566 opts = tbops[item.optname];
571 // opts == pulldown..
572 tb.addField( new Roo.form.ComboBox({
573 store: new Roo.data.SimpleStore({
575 fields: ['val', 'display'],
578 name : '-roo-edit-' + i,
580 stylename : item.style ? item.style : false,
581 displayField: item.displayField ? item.displayField : 'val',
586 triggerAction: 'all',
589 width: item.width ? item.width : 130,
591 'select': function(c, r, i) {
593 tb.selectedNode.style[c.stylename] = r.get('val');
596 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
605 tb.addField( new Roo.form.TextField({
613 tb.addField( new Roo.form.TextField({
614 name: '-roo-edit-' + i,
621 'change' : function(f, nv, ov) {
622 tb.selectedNode.setAttribute(f.attrname, nv);
637 // undo does not work.
639 var sn = tb.selectedNode;
641 var pn = sn.parentNode;
643 var stn = sn.childNodes[0];
644 var en = sn.childNodes[sn.childNodes.length - 1 ];
645 while (sn.childNodes.length) {
646 var node = sn.childNodes[0];
647 sn.removeChild(node);
649 pn.insertBefore(node, sn);
653 var range = editor.createRange();
655 range.setStart(stn,0);
656 range.setEnd(en,0); //????
657 //range.selectNode(sel);
660 var selection = editor.getSelection();
661 selection.removeAllRanges();
662 selection.addRange(range);
666 //_this.updateToolbar(null, null, pn);
667 _this.updateToolbar(null, null, null);
668 _this.footDisp.dom.innerHTML = '';
678 tb.el.on('click', function(e){
679 e.preventDefault(); // what does this do?
681 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
684 // dont need to disable them... as they will get hidden
689 buildFooter : function()
692 var fel = this.editor.wrap.createChild();
693 this.footer = new Roo.Toolbar(fel);
694 // toolbar has scrolly on left / right?
695 var footDisp= new Roo.Toolbar.Fill();
701 handler : function() {
702 _t.footDisp.scrollTo('left',0,true)
706 this.footer.add( footDisp );
711 handler : function() {
713 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
717 var fel = Roo.get(footDisp.el);
718 fel.addClass('x-editor-context');
719 this.footDispWrap = fel;
720 this.footDispWrap.overflow = 'hidden';
722 this.footDisp = fel.createChild();
723 this.footDispWrap.on('click', this.onContextClick, this)
727 onContextClick : function (ev,dom)
730 var cn = dom.className;
732 if (!cn.match(/x-ed-loc-/)) {
735 var n = cn.split('-').pop();
736 var ans = this.footerEls;
740 var range = this.editor.createRange();
742 range.selectNodeContents(sel);
743 //range.selectNode(sel);
746 var selection = this.editor.getSelection();
747 selection.removeAllRanges();
748 selection.addRange(range);
752 this.updateToolbar(null, null, sel);