Modernize editor-stats package with package.cson

This commit is contained in:
Kevin Sawicki
2013-02-07 19:49:21 -08:00
committed by Corey Johnson & Kevin Sawicki
parent a70f5ea1f7
commit 34c570bed5
7 changed files with 67 additions and 31 deletions

View File

@@ -1,16 +0,0 @@
DeferredAtomPackage = require 'deferred-atom-package'
Stats = require './src/stats'
module.exports =
class EditorStats extends DeferredAtomPackage
loadEvents: ['editor-stats:toggle']
instanceClass: 'editor-stats/src/editor-stats-view'
stats: new Stats
activate: (rootView) ->
super
rootView.on 'keydown', => @stats.track()
rootView.on 'mouseup', => @stats.track()
onLoadEvent: (event, instance) -> instance.toggle(@stats)

View File

@@ -5,10 +5,10 @@ $ = require 'jquery'
module.exports =
class EditorStatsView extends ScrollView
@activate: (rootView, state) ->
@instance = new EditorStatsView(rootView)
@activate: ->
new EditorStatsView
@content: (rootView) ->
@content: ->
@div class: 'editor-stats-wrapper', tabindex: -1, =>
@div class: 'editor-stats', outlet: 'editorStats'
@@ -17,7 +17,7 @@ class EditorStatsView extends ScrollView
pb: 3
pr: 25
initialize: (@rootView) ->
initialize: ->
super
resizer = =>
@@ -30,7 +30,7 @@ class EditorStatsView extends ScrollView
@editorStats.empty()
@x ?= d3.scale.ordinal().domain d3.range(@stats.hours * 60)
@y ?= d3.scale.linear()
w = @rootView.vertical.width()
w = rootView.vertical.width()
h = @height()
data = d3.entries @stats.eventLog
max = d3.max data, (d) -> d.value
@@ -94,10 +94,10 @@ class EditorStatsView extends ScrollView
@attach()
attach: ->
@rootView.vertical.append(@)
rootView.vertical.append(this)
@draw()
detach: ->
super()
super
clearInterval(@updateInterval)
@rootView.focus()
rootView.focus()

View File

@@ -0,0 +1,19 @@
StatsTracker = require './stats-tracker'
module.exports =
stats: null
editorStatsView: null
activate: ->
@stats = new StatsTracker()
rootView.command 'editor-stats:toggle', => @createView().toggle(@stats)
deactivate: ->
@editorStatsView = null
@stats = null
createView: ->
unless @editorStatsView
EditorStatsView = require 'editor-stats/lib/editor-stats-view'
@editorStatsView = new EditorStatsView()
@editorStatsView

View File

@@ -1,5 +1,5 @@
module.exports =
class Stats
class StatsTracker
startDate: new Date
hours: 6
eventLog: []
@@ -12,6 +12,9 @@ class Stats
while date < future
@eventLog[@time(date)] = 0
rootView.on 'keydown', => @track()
rootView.on 'mouseup', => @track()
clear: ->
@eventLog = []

View File

@@ -0,0 +1 @@
'main': 'lib/editor-stats'

View File

@@ -1,9 +1,9 @@
$ = require 'jquery'
RootView = require 'root-view'
EditorStats = require 'editor-stats/src/editor-stats-view'
EditorStats = require 'editor-stats/lib/editor-stats-view'
describe "EditorStats", ->
[rootView, editorStats, time] = []
[editorStats, time] = []
simulateKeyUp = (key) ->
e = $.Event "keydown", keyCode: key.charCodeAt(0)
@@ -23,10 +23,7 @@ describe "EditorStats", ->
mins = if mins == 60 then '01' else mins + 1
time = "#{hours}:#{mins}"
editorStatsPackage = atom.loadPackage('editor-stats')
editorStatsPackage.getInstance()
editorStats = editorStatsPackage.stats
editorStats.clear()
editorStats = atom.loadPackage('editor-stats').packageMain.stats
afterEach ->
rootView.deactivate()

View File

@@ -0,0 +1,32 @@
module.exports =
class StatsTracker
startDate: new Date
hours: 6
eventLog: []
constructor: ->
date = new Date(@startDate)
future = new Date(date.getTime() + (36e5 * @hours))
@eventLog[@time(date)] = 0
while date < future
@eventLog[@time(date)] = 0
rootView.on 'keydown', => @track()
rootView.on 'mouseup', => @track()
clear: ->
@eventLog = []
track: ->
date = new Date
times = @time date
@eventLog[times] ?= 0
@eventLog[times] += 1
@eventLog.shift() if @eventLog.length > (@hours * 60)
time: (date) ->
date.setTime(date.getTime() + 6e4)
hour = date.getHours()
minute = date.getMinutes()
"#{hour}:#{minute}"