Skip to content

Commit 9e6cbb5

Browse files
moving from pure js to coffee
1 parent fc469ee commit 9e6cbb5

13 files changed

+126
-134
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*.swp
2-
js/mobile.js
3-
js/init_plot.js
4-
css/mobile.css
2+
*.pyc
3+
node_modules/
4+
js/*.js

coffee/habitat.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$ = $ || {}
2+
3+
habitat =
4+
db: if $.couch then $.couch.db "habitat" else {}
5+
6+
habitat.db.uri = "http://habitat.habhub.org/habitat/"

coffee/habitat.tracker.coffee

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
habitat.tracker =
2+
flights:
3+
active: []
4+
upcoming: []
5+
options: {}
6+
vehicles: []
7+
8+
### reset tracker state to pre-init state ###
9+
reset: ->
10+
delete this.flights
11+
delete this.options
12+
delete this.vehicles
13+
14+
this.flights =
15+
active: []
16+
upcoming: []
17+
this.options = {}
18+
this.vehicles = []
19+
20+
init: (options) ->
21+
options = if options then options else {}
22+
options.filter = if options.filter then open|| null
23+
options.poll = options.poll || true
24+
this.options = options
25+
26+
""" pull flight list """
27+
this.update_flight_list()
28+
29+
update_flight_list: ->
30+
this.db.get_flights (data) ->
31+
current_ts = habitat.util.timestamp_now()
32+
33+
for flight in data
34+
flight_ts = habitat.util.rfc3339_to_timestamp flight.start
35+
36+
if flight_ts < current_ts
37+
habitat.tracker.flights.active.push flight
38+
else
39+
habitat.tracker.flights.upcoming.push flight
40+
41+
null
42+
43+
test: ->
44+
out = $('body')
45+
46+
out.append("Upcoming flights<br><br>")
47+
this.append(this.flights.upcoming)
48+
out.append("<br>Active flights<br><br>")
49+
this.append(this.flights.active)
50+
51+
append: (list) ->
52+
out = $('body')
53+
54+
for flight in list
55+
out.append "#{flight.start} #{flight.name} <br />"

coffee/habitat.tracker.db.coffee

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
habitat.tracker.db =
2+
3+
# pulls latest flights from habitat
4+
get_flights: (callback, selector) ->
5+
ts = Math.floor (new Date()).getTime() / 1000
6+
7+
habitat.db.view "flight/end_start_including_payloads", {
8+
startkey: [ts]
9+
include_docs: true
10+
success: (data) ->
11+
last = null
12+
list = []
13+
payloads
14+
15+
# merge payload_configs into flight docs
16+
for row in data.rows
17+
doc = row.doc
18+
19+
if doc.type is "flight" and doc.approved is true
20+
last = doc
21+
payloads = doc.payloads
22+
doc.payloads = []
23+
list.push doc
24+
else if payloads.indexOf doc._id is not -1
25+
last.payloads.push doc
26+
27+
# return results
28+
callback list
29+
null
30+
}
31+
32+
fetch_payloads: (flight_docs) ->
33+
list = []
34+
35+
for doc in flight_docs
36+
list.concat(doc.payloads);
37+
38+
null

coffee/habitat.util.coffee

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
habitat.util =
3+
timestamp_now: ->
4+
Math.floor (new Date()).getTime() / 1000
5+
6+
rfc3339_to_timestamp: (str) ->
7+
Math.floor (new Date str).getTime() / 1000
8+
9+
timestamp_to_rfc3339: (ts) ->
10+
date = new Date()
11+
date.setTime ts * 1000
12+
date.toISOString()

fabfile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from fabric.api import *
2+
3+
def compile_coffee():
4+
local("/usr/lib/node_modules/coffee-script/bin/coffee -b -c -o js/ coffee/*.coffee")
5+
6+
def build():
7+
compile_coffee()
8+
9+
def clean():
10+
local("rm -f js/*js")

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
55
<title>habitat api test </title>
6-
<script type="text/javascript" language="javascript" src="js/jquery-1.8.3-min.js"></script>
7-
<script type="text/javascript" language="javascript" src="js/jquery.couch.js"></script>
6+
<script type="text/javascript" language="javascript" src="js/lib/jquery-1.8.3-min.js"></script>
7+
<script type="text/javascript" language="javascript" src="js/lib/jquery.couch.js"></script>
88
<script type="text/javascript" language="javascript" src="js/habitat.js"></script>
99
<script type="text/javascript" language="javascript" src="js/habitat.util.js"></script>
1010
<script type="text/javascript" language="javascript" src="js/habitat.tracker.js"></script>

js/habitat.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

js/habitat.tracker.db.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

js/habitat.tracker.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)