Created
January 10, 2013 14:32
-
-
Save jarek-foksa/4502450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # @authors | |
| # Jarosław Foksa <https://github.com/jarek-foksa> | |
| # | |
| # @copyright | |
| # Copyright © 2012 by Jarosław Foksa | |
| # | |
| # @license | |
| # Boxy License </doc/licenses/boxy-license.md> | |
| mouseObserver = imports 'widgets/mouseObserver' | |
| {html} = imports 'utils/domFactories' | |
| # @events | |
| # ----------------------------------------------- | |
| # Event name Args Tracking mode | |
| # ----------------------------------------------- | |
| # clicked () click | |
| # toggled (state) toggle | |
| # userToggled (state) toggle | |
| # ----------------------------------------------- | |
| button = | |
| id: 'sample-button' | |
| label: 'Sample Button' | |
| tooltip: 'This is sample label' | |
| icon: 'widgets/images/html5.svg' | |
| style: 'toolbar' | |
| trackingMode: 'toggle' # toggle/click | |
| enabled: true | |
| toggled: false | |
| init: -> | |
| @$element = html 'div.boxy.button-widget' | |
| @$element.owner = @ | |
| @_$row1 = html 'div.row-1', @$element | |
| @_$row2 = html 'div.row-2', @$element | |
| @_$icon = html 'div.icon', @_$row1 | |
| @_$label = html 'div.label', @_$row2 | |
| @_mouseObserver = mouseObserver.clone().init @$element | |
| if @id | |
| @setID @id | |
| if @label | |
| @setLabel @label | |
| if @tooltip | |
| @setTooltip @tooltip | |
| if @icon | |
| @setIcon @icon | |
| @setStyle @style | |
| if @enabled | |
| @enable() | |
| else | |
| @disable() | |
| if @toggled | |
| @toggleOn() | |
| else | |
| @toggleOff() | |
| @_initialized = true | |
| return @ | |
| free: -> | |
| @_mouseObserver.free() | |
| @_disable() | |
| enable: -> | |
| return if @enabled && @_initialized | |
| @enabled = true | |
| @$element.data.set 'enabled', true | |
| @_mouseObserver.enable() | |
| @_mouseObserver.listen 'clicked', @_mouseObserverCB = => @_onClicked() | |
| disable: -> | |
| return if !@enabled && @_initialized | |
| @enabled = false | |
| @$element.data.set 'enabled', false | |
| @_mouseObserver.disable() | |
| @_mouseObserver.unlisten 'clicked', @_mouseObserverCB | |
| toggle: -> | |
| if @toggled | |
| @toggleOff() | |
| else | |
| @toggleOn() | |
| toggleOn: -> | |
| return if @toggled && @_initialized | |
| @toggled = true | |
| @$element.data.set 'toggled', @toggled | |
| toggleOff: -> | |
| return if !@toggled && @_initialized | |
| @toggled = false | |
| @$element.data.set 'toggled', @toggled | |
| setID: (id) -> | |
| @id = id | |
| @$element.data.set 'id', id | |
| setLabel: (label) -> | |
| @label = label | |
| @_$label.textContent = label | |
| setTooltip: (tooltip) -> | |
| @tooltip = tooltip | |
| @$element.setAttribute 'title', tooltip | |
| setIcon: (iconURL) -> | |
| @icon = iconURL | |
| @_$icon.style.backgroundImage = "url(#{iconURL})" | |
| setStyle: (style) -> | |
| @style = style | |
| @$element.data.set 'style', style | |
| _onClicked: -> | |
| if @trackingMode == 'toggle' | |
| @toggle() | |
| @trigger 'userToggled', @toggled | |
| else | |
| @trigger 'clicked' | |
| zoomInButton = button.clone | |
| # @override | |
| id: 'zoomIn' | |
| label: 'Zoom in' | |
| style: 'toolbar' | |
| trackingMode: 'click' | |
| tooltip: 'Zoom in the drawing' | |
| icon: "#{__dirName__}/images/zoom-in.svg" | |
| zoomOutButton = button.clone | |
| # @override | |
| id: 'zoomOut' | |
| label: 'Zoom out' | |
| style: 'toolbar' | |
| trackingMode: 'click' | |
| tooltip: 'Zoom out the drawing' | |
| icon: "#{__dirName__}/images/zoom-out.svg" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment