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 tbc = Roo.form.HtmlEditor.ToolbarContext;
556 var tbops = tbc.options;
558 for (var i in tlist) {
561 tb.add(item.title + ": ");
564 //optname == used so you can configure the options available..
565 var opts = item.opts ? item.opts : false;
567 opts = tbops[item.optname];
572 // opts == pulldown..
573 tb.addField( new Roo.form.ComboBox({
574 store: typeof(tbc.stores[i]) != 'undefined' ? tbc.stores[i] : new Roo.data.SimpleStore({
576 fields: ['val', 'display'],
579 name : '-roo-edit-' + i,
581 stylename : item.style ? item.style : false,
582 displayField: item.displayField ? item.displayField : 'val',
587 triggerAction: 'all',
590 width: item.width ? item.width : 130,
592 'select': function(c, r, i) {
594 tb.selectedNode.style[c.stylename] = r.get('val');
597 tb.selectedNode.setAttribute(c.attrname, r.get('val'));
606 tb.addField( new Roo.form.TextField({
614 tb.addField( new Roo.form.TextField({
615 name: '-roo-edit-' + i,
622 'change' : function(f, nv, ov) {
623 tb.selectedNode.setAttribute(f.attrname, nv);
638 // undo does not work.
640 var sn = tb.selectedNode;
642 var pn = sn.parentNode;
644 var stn = sn.childNodes[0];
645 var en = sn.childNodes[sn.childNodes.length - 1 ];
646 while (sn.childNodes.length) {
647 var node = sn.childNodes[0];
648 sn.removeChild(node);
650 pn.insertBefore(node, sn);
654 var range = editor.createRange();
656 range.setStart(stn,0);
657 range.setEnd(en,0); //????
658 //range.selectNode(sel);
661 var selection = editor.getSelection();
662 selection.removeAllRanges();
663 selection.addRange(range);
667 //_this.updateToolbar(null, null, pn);
668 _this.updateToolbar(null, null, null);
669 _this.footDisp.dom.innerHTML = '';
679 tb.el.on('click', function(e){
680 e.preventDefault(); // what does this do?
682 tb.el.setVisibilityMode( Roo.Element.DISPLAY);
685 // dont need to disable them... as they will get hidden
690 buildFooter : function()
693 var fel = this.editor.wrap.createChild();
694 this.footer = new Roo.Toolbar(fel);
695 // toolbar has scrolly on left / right?
696 var footDisp= new Roo.Toolbar.Fill();
702 handler : function() {
703 _t.footDisp.scrollTo('left',0,true)
707 this.footer.add( footDisp );
712 handler : function() {
714 _t.footDisp.select('span').last().scrollIntoView(_t.footDisp,true);
718 var fel = Roo.get(footDisp.el);
719 fel.addClass('x-editor-context');
720 this.footDispWrap = fel;
721 this.footDispWrap.overflow = 'hidden';
723 this.footDisp = fel.createChild();
724 this.footDispWrap.on('click', this.onContextClick, this)
728 onContextClick : function (ev,dom)
731 var cn = dom.className;
733 if (!cn.match(/x-ed-loc-/)) {
736 var n = cn.split('-').pop();
737 var ans = this.footerEls;
741 var range = this.editor.createRange();
743 range.selectNodeContents(sel);
744 //range.selectNode(sel);
747 var selection = this.editor.getSelection();
748 selection.removeAllRanges();
749 selection.addRange(range);
753 this.updateToolbar(null, null, sel);