drawingmode.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. DrawingBoard.Control.DrawingMode = DrawingBoard.Control.extend({
  2. name: 'drawingmode',
  3. defaults: {
  4. pencil: true,
  5. eraser: true,
  6. filler: true
  7. },
  8. initialize: function() {
  9. this.prevMode = this.board.getMode();
  10. $.each(["pencil", "eraser", "filler"], $.proxy(function(k, value) {
  11. if (this.opts[value]) {
  12. this.$el.append('<button class="drawing-board-control-drawingmode-' + value + '-button" data-mode="' + value + '"></button>');
  13. }
  14. }, this));
  15. this.$el.on('click', 'button[data-mode]', $.proxy(function(e) {
  16. var value = $(e.currentTarget).attr('data-mode');
  17. var mode = this.board.getMode();
  18. if (mode !== value) this.prevMode = mode;
  19. var newMode = mode === value ? this.prevMode : value;
  20. this.board.setMode( newMode );
  21. e.preventDefault();
  22. }, this));
  23. this.board.ev.bind('board:mode', $.proxy(function(mode) {
  24. this.toggleButtons(mode);
  25. }, this));
  26. this.toggleButtons( this.board.getMode() );
  27. },
  28. toggleButtons: function(mode) {
  29. this.$el.find('button[data-mode]').each(function(k, item) {
  30. var $item = $(item);
  31. $item.toggleClass('active', mode === $item.attr('data-mode'));
  32. });
  33. }
  34. });