2013-01-20 22:55:45 +00:00
content = require ( ' ./content ' )
2013-02-06 19:12:05 +00:00
_ = require ' underscore '
2013-02-06 18:59:56 +00:00
module.exports.resetDom = (model) ->
window . DERBY . app . dom . clear ( )
window . DERBY . app . view . render ( model )
model . fn ' _tnl ' , ' _user.stats.lvl ' , (lvl) -> ( lvl * 100 ) / 5
2013-01-20 22:55:45 +00:00
# ##
Loads JavaScript files from ( 1 ) public / js / * and ( 2 ) external sources
We use this file ( instead of < Scripts : > or < Tail : > inside . html ) so we can utilize require ( ) to concatinate for
faster page load , and $ . getScript for asyncronous external script loading
# ##
module.exports.loadJavaScripts = (model) ->
# Load public/js/* files
# TODO use Bower
require ' ../../public/js/jquery.min '
require ' ../../public/js/jquery-ui.min ' unless model . get ( ' _view.mobileDevice ' )
require ' ../../public/js/bootstrap.min ' #http://twitter.github.com/bootstrap/assets/js/bootstrap.min.js
require ' ../../public/js/jquery.cookie ' #https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js
require ' ../../public/js/bootstrap-tour ' #https://raw.github.com/pushly/bootstrap-tour/master/bootstrap-tour.js
require ' ../../public/js/jquery.bootstrap-growl.min '
# JS files not needed right away (google charts) or entirely optional (analytics)
# Each file getsload asyncronously via $.getScript, so it doesn't bog page-load
unless model . get ( ' _view.mobileDevice ' )
# Google Charts
$ . getScript " https://www.google.com/jsapi " , ->
# Specifying callback in options param is vital! Otherwise you get blank screen, see http://stackoverflow.com/a/12200566/362790
google . load " visualization " , " 1 " , { packages : [ " corechart " ] , callback: -> }
# Note, Google Analyatics giving beef if in this file. Moved back to index.html. It's ok, it's async - really the
# syncronous requires up top are what benefit the most from this file.
# ##
Setup jQuery UI Sortable
# ##
module.exports.setupSortable = (model) ->
unless ( model . get ( ' _view.mobileDevice ' ) == true ) #don't do sortable on mobile
# Make the lists draggable using jQuery UI
# Note, have to setup helper function here and call it for each type later
# due to variable binding of "type"
setupSortable = (type) ->
$ ( " ul. #{ type } s " ) . sortable
dropOnEmpty: false
cursor: " move "
items: " li "
opacity: 0.4
scroll: true
axis: ' y '
update: (e, ui) ->
item = ui . item [ 0 ]
domId = item . id
id = item . getAttribute ' data-id '
to = $ ( " ul. #{ type } s " ) . children ( ) . index ( item )
# Use the Derby ignore option to suppress the normal move event
# binding, since jQuery UI will move the element in the DOM.
# Also, note that refList index arguments can either be an index
# or the item's id property
model . at ( " _ #{ type } List " ) . pass ( ignore: domId ) . move { id } , to
2013-02-06 19:12:05 +00:00
_ . each [ ' habit ' , ' daily ' , ' todo ' , ' reward ' ] , (type) -> setupSortable ( type )
2013-01-20 22:55:45 +00:00
module.exports.setupTooltips = (model) ->
$ ( ' [rel=tooltip] ' ) . tooltip ( )
$ ( ' [rel=popover] ' ) . popover ( )
# FIXME: this isn't very efficient, do model.on set for specific attrs for popover
model . on ' set ' , ' * ' , ->
$ ( ' [rel=tooltip] ' ) . tooltip ( )
$ ( ' [rel=popover] ' ) . popover ( )
module.exports.setupTour = (model) ->
tour = new Tour ( )
for step in content . tourSteps
tour . addStep
html: true
element: step . element
title: step . title
content: step . content
placement: step . placement
tour . start ( )
2013-01-20 21:52:56 +00:00
# ##
Sets up " +1 Exp " , " Level Up " , etc notifications
# ##
module.exports.setupGrowlNotifications = (model) ->
return unless jQuery ? # Only run this in the browser
user = model . at ' _user '
statsNotification = (html, type) ->
#don't show notifications if user dead
2013-02-05 03:35:07 +00:00
return if user . get ( ' stats.lvl ' ) == 0
2013-01-20 21:52:56 +00:00
$ . bootstrapGrowl html ,
type: type # (null, 'info', 'error', 'success')
top_offset: 20
align: ' right ' # ('left', 'right', or 'center')
width: 250 # (integer, or 'auto')
delay: 3000
allow_dismiss: true
stackup_spacing: 10 # spacing between consecutive stacecked growls.
2013-02-05 03:35:07 +00:00
user . on ' set ' , ' flags.itemsEnabled ' , (captures, args) ->
2013-01-25 21:16:28 +00:00
return unless captures == true
2013-01-30 23:22:52 +00:00
message = " Congratulations, you have unlocked the Item Store! You can now buy weapons, armor, potions, etc. Read each item ' s comment for more information. "
2013-01-25 21:16:28 +00:00
$ ( ' ul.items ' ) . popover
2013-01-30 23:22:52 +00:00
title: " Item Store Unlocked "
2013-01-25 21:16:28 +00:00
placement: ' left '
trigger: ' manual '
html: true
content: " <div class= ' item-store-popover ' >
2013-02-04 06:20:55 +00:00
< img src = ' /img/BrowserQuest/chest.png ' / >
#{message} <a href='#' onClick=\"$('ul.items').popover('hide');return false;\">[Close]</a>
< / div > "
2013-01-25 21:16:28 +00:00
$ ( ' ul.items ' ) . popover ' show '
2013-02-05 03:35:07 +00:00
user . on ' set ' , ' flags.partyEnabled ' , (captures, args) ->
2013-01-30 23:22:52 +00:00
return unless captures == true
message = " Congratulations, you have unlocked the Party System! You can now group with your friends by adding their User Ids. "
2013-02-06 01:48:43 +00:00
$ ( ' .main-avatar ' ) . popover
2013-02-07 03:55:34 +00:00
title: " Party System Unlocked "
2013-01-30 23:22:52 +00:00
placement: ' bottom '
trigger: ' manual '
html: true
content: " <div class= ' party-system-popover ' >
2013-02-06 01:48:43 +00:00
< img src = ' /img/party-unlocked.png ' style = ' float:right;padding:5px; ' / >
#{message} <a href='#' onClick=\"$('.main-avatar').popover('hide');return false;\">[Close]</a>
2013-02-04 06:20:55 +00:00
< / div > "
2013-02-06 01:48:43 +00:00
$ ( ' .main-avatar ' ) . popover ' show '
2013-01-30 23:22:52 +00:00
2013-01-20 21:52:56 +00:00
# Setup listeners which trigger notifications
2013-02-05 03:35:07 +00:00
user . on ' set ' , ' stats.hp ' , (captures, args) ->
2013-01-20 21:52:56 +00:00
num = captures - args
rounded = Math . abs ( num . toFixed ( 1 ) )
if num < 0
statsNotification " <i class= ' icon-heart ' ></i>HP - #{ rounded } " , ' error ' # lost hp from purchase
2013-02-05 03:35:07 +00:00
user . on ' set ' , ' stats.gp ' , (captures, args) ->
2013-01-20 21:52:56 +00:00
num = captures - args
rounded = Math . abs ( num . toFixed ( 1 ) )
# made purchase
if num < 0
# FIXME use 'warning' when unchecking an accidently completed daily/todo, and notify of exp too
statsNotification " <i class= ' icon-star ' ></i>GP - #{ rounded } " , ' success '
2013-02-05 02:13:19 +00:00
# gained gp (and thereby exp)
2013-01-20 21:52:56 +00:00
else if num > 0
num = Math . abs ( num )
statsNotification " <i class= ' icon-star ' ></i>Exp,GP + #{ rounded } " , ' success '
2013-02-05 03:35:07 +00:00
user . on ' set ' , ' stats.lvl ' , (captures, args) ->
2013-01-20 21:52:56 +00:00
if captures > args
2013-01-20 22:55:45 +00:00
statsNotification ( ' <i class= " icon-chevron-up " ></i> Level Up! ' , ' info ' )