PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/src/format-bar.js

https://github.com/studiovlijmscherp/sir-trevor-js
JavaScript | 106 lines | 71 code | 27 blank | 8 comment | 7 complexity | fa15af68b7450f8edce011a1cad9c9cf MD5 | raw file
  1. /*
  2. Format Bar
  3. --
  4. Displayed on focus on a text area.
  5. Renders with all available options for the editor instance
  6. */
  7. SirTrevor.FormatBar = (function(){
  8. var FormatBar = function(options) {
  9. this.options = _.extend({}, SirTrevor.DEFAULTS.formatBar, options || {});
  10. this._ensureElement();
  11. this._bindFunctions();
  12. this.initialize.apply(this, arguments);
  13. };
  14. _.extend(FormatBar.prototype, FunctionBind, SirTrevor.Events, Renderable, {
  15. className: 'st-format-bar',
  16. bound: ["onFormatButtonClick", "renderBySelection", "hide"],
  17. initialize: function() {
  18. var formatName, format, btn;
  19. this.$btns = [];
  20. for (formatName in SirTrevor.Formatters) {
  21. if (SirTrevor.Formatters.hasOwnProperty(formatName)) {
  22. format = SirTrevor.Formatters[formatName];
  23. btn = $("<button>", {
  24. 'class': 'st-format-btn st-format-btn--' + formatName + ' ' + (format.iconName ? 'st-icon st-icon-' +format.iconName : ''),
  25. 'text': format.text,
  26. 'data-type': formatName,
  27. 'data-cmd': format.cmd
  28. });
  29. this.$btns.push(btn);
  30. btn.appendTo(this.$el);
  31. }
  32. }
  33. this.$b = $(document);
  34. this.$el.bind('click', '.st-format-btn', this.onFormatButtonClick);
  35. },
  36. hide: function() {
  37. this.$el.removeClass('st-format-bar--is-ready');
  38. },
  39. show: function() {
  40. this.$el.addClass('st-format-bar--is-ready');
  41. },
  42. remove: function(){ this.$el.remove(); },
  43. renderBySelection: function(rectangles) {
  44. var selection = window.getSelection(),
  45. range = selection.getRangeAt(0),
  46. boundary = range.getBoundingClientRect(),
  47. coords = {};
  48. coords.top = boundary.top + 20 + window.pageYOffset - this.$el.height() + 'px';
  49. coords.left = ((boundary.left + boundary.right) / 2) - (this.$el.width() / 2) + 'px';
  50. this.highlightSelectedButtons();
  51. this.show();
  52. this.$el.css(coords);
  53. },
  54. highlightSelectedButtons: function() {
  55. var formatter;
  56. _.each(this.$btns, function($btn) {
  57. formatter = SirTrevor.Formatters[$btn.attr('data-type')];
  58. $btn.toggleClass("st-format-btn--is-active",
  59. formatter.isActive());
  60. }, this);
  61. },
  62. onFormatButtonClick: function(ev){
  63. ev.stopPropagation();
  64. var btn = $(ev.target),
  65. format = SirTrevor.Formatters[btn.attr('data-type')];
  66. if (_.isUndefined(format)) return false;
  67. // Do we have a click function defined on this formatter?
  68. if(!_.isUndefined(format.onClick) && _.isFunction(format.onClick)) {
  69. format.onClick(); // Delegate
  70. } else {
  71. // Call default
  72. document.execCommand(btn.attr('data-cmd'), false, format.param);
  73. }
  74. this.highlightSelectedButtons();
  75. return false;
  76. }
  77. });
  78. return FormatBar;
  79. })();