Git Product home page Git Product logo

widget.coffee's Introduction

widget.coffee Bower version

widget.coffee is a light-weight framework to help developer write front-end javascript in a more easier way.

Install

Install using bower:

$ bower install widget.coffee --save

Quick Start

Html:

<html>
  <head>    
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="widget.js"></script>
    <script type="text/javascript" src="sample_widgets.js"></script>
  </head>
  <body>
    <div data-widget="Page">
      <button data-action-handler='resetAll'>Reset All</button>
      <hr>
      <div data-widget="Timer" data-widget-part='timer'>
        <div class="time">no record!</div>
        <button class="toggle-button">start</button>
        <button class="lap-button">lap</button>
        <button class="reset-button">reset</button>
      </div>
      <hr>
      <div data-widget="ScoreBoard">
        <div>History</div>
        <ol data-widget-part="scoreList"></ol>
        <button data-action-handler="clean">Clean</button>
      </div>  
    </div>
  </body>
</html>

Coffeescript:

# sample_widget.coffee

class @Timer extends Widget
  bindDom: ->
    @timeSpan = @element.find('.time')
    @toggleButton = @element.find('.toggle-button')
    @lapButton = @element.find('.lap-button')
    @resetButton = @element.find('.reset-button')
    @scoreBoard = Widget.findWidgetByType('ScoreBoard')

  enhancePage: ->        
    @toggleButton.click @toggle
    @lapButton.click @lap
    @resetButton.click @reset
        
  initialize: ->
    @running = false
    @refreshButtonStatus()
    @reset()

  tick: =>
    @time++
    @refreshTime()
   
  refreshTime: ->
    @timeSpan.text("#{@time} ms")
    
  toggle: =>       
    @controlTimer !@running

  controlTimer: (running) ->
    @running = running

    @refreshButtonStatus()
   
    if @running
      @time = 0
      @refreshTime()
      @token = setInterval(@tick, 1)
    else
      if @token?
        clearInterval(@token) 
        delete @token

  refreshButtonStatus: ->
    if @running
      @toggleButton.text('Stop')        
      @lapButton.removeAttr('disabled')        
      @resetButton.attr('disabled', 'disabled')
    else
      @toggleButton.text('Start')  
      @lapButton.attr('disabled', 'disabled')
      @resetButton.removeAttr('disabled')  

  lap: =>
    @scoreBoard.addScore(@time)
    
  reset: =>
    delete @time 
    @timeSpan.text('no record!')
  
class @ScoreBoard extends Widget
  bindDom: ->
    @bindWidgetParts()

  enhancePage: ->
    @bindActionHandlers()

  addScore: (score) ->
    $('<li>').text("#{score} ms").appendTo(@scoreList)

  clean: ->
    @scoreList.html('')


class @Page extends Widget
  bindDom: ->
    @scoreBoard = @findSubWidgetByType('ScoreBoard')
    @bindWidgetParts()

  enhancePage: ->
    @bindActionHandlers()

  resetAll: ->
    @timer.controlTimer(false)
    @timer.reset()
    @scoreBoard.clean()
 

Debugging

widget.coffee provides some convenient API for developer to debug the widget in runtime. Open Javascript console(browser built-in or FireBug), type

  Widget.findWidgetByType('Timer').toggle()

Who uses widget.coffee

widget.coffee is original developed for Sustainable Fishery Partnership Metrics 2.0 system. Later was refined as an independant javascript library uses by variant projects.

License

MIT

widget.coffee's People

Contributors

timnew avatar

Watchers

James Cloos avatar  avatar

widget.coffee's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.