move all view helpers out of their respective files and into

helpers.coffee. we need viewHelpers at top-level scope, but nothing
else per se - this is preparation for memory leak fixing
This commit is contained in:
Tyler Renelle
2013-04-04 16:17:30 -04:00
parent 367ca4a76b
commit 0abbc98b1c
6 changed files with 85 additions and 92 deletions
-17
View File
@@ -8,23 +8,6 @@ _ = require 'underscore'
lodash = require 'lodash'
derby = require 'derby'
module.exports.username = username = (auth) ->
if auth?.facebook?.displayName?
auth.facebook.displayName
else if auth?.facebook?
fb = auth.facebook
if fb._raw then "#{fb.name.givenName} #{fb.name.familyName}" else fb.name
else if auth?.local?
auth.local.username
else
'Anonymous'
module.exports.view = (view) ->
view.fn "username", (auth) -> username(auth)
view.fn "tnl", algos.tnl
module.exports.app = (appExports, model) ->
user = model.at '_user'
+83 -1
View File
@@ -1,5 +1,6 @@
moment = require 'moment'
_ = require 'underscore'
algos = require './algos'
# Absolute diff between two dates
daysBetween = (yesterday, now, dayStart) ->
@@ -23,6 +24,17 @@ removeWhitespace = (str) ->
return '' unless str
str.replace /\s/g, ''
username = (auth) ->
if auth?.facebook?.displayName?
auth.facebook.displayName
else if auth?.facebook?
fb = auth.facebook
if fb._raw then "#{fb.name.givenName} #{fb.name.familyName}" else fb.name
else if auth?.local?
auth.local.username
else
'Anonymous'
viewHelpers = (view) ->
view.fn "percent", (x, y) ->
x=1 if x==0
@@ -59,4 +71,74 @@ viewHelpers = (view) ->
view.fn "truarr", (num) -> num-1
module.exports = { viewHelpers, removeWhitespace, randomVal, daysBetween, dayMapping }
###
User
###
view.fn "username", (auth) -> username(auth)
view.fn "tnl", algos.tnl
###
Items
###
view.fn 'equipped', (user, type) ->
{gender, armorSet} = user?.preferences || {'m', 'v1'}
if type=='armor'
armor = user?.items?.armor || 0
if gender == 'f'
return if (parseInt(armor) == 0) then "f_armor_#{armor}_#{armorSet}" else "f_armor_#{armor}"
else
return "m_armor_#{armor}"
else if type=='head'
head = user?.items?.head || 0
if gender == 'f'
return if (parseInt(head) > 1) then "f_head_#{head}_#{armorSet}" else "f_head_#{head}"
else
return "m_head_#{head}"
view.fn "gold", (num) ->
if num
return (num).toFixed(1).split('.')[0]
else
return "0"
view.fn "silver", (num) ->
if num
(num).toFixed(2).split('.')[1]
else
return "00"
###
Tasks
###
view.fn 'taskClasses', (task) ->
return unless task
{type, completed, value, repeat} = task
classes = type
# show as completed if completed (naturally) or not required for today
if type in ['todo', 'daily']
if completed or (repeat and repeat[dayMapping[moment().day()]]==false)
classes += " completed"
else
classes += " uncompleted"
if value < -20
classes += ' color-worst'
else if value < -10
classes += ' color-worse'
else if value < -1
classes += ' color-bad'
else if value < 1
classes += ' color-neutral'
else if value < 5
classes += ' color-good'
else if value < 10
classes += ' color-better'
else
classes += ' color-best'
return classes
module.exports = { viewHelpers, removeWhitespace, randomVal, daysBetween, dayMapping, username }
-3
View File
@@ -24,9 +24,6 @@ profile = require './profile'
pets = require './pets'
helpers.viewHelpers view
character.view view
tasks.view view
items.view view
_ = require('underscore')
-40
View File
@@ -70,46 +70,6 @@ _.each ['weapon', 'armor', 'head', 'shield'], (key) ->
_.each items.pets, (pet) -> pet.notes = 'Find a hatching potion to pour on this egg, and one day it will hatch into a loyal pet.'
_.each items.hatchingPotions, (hatchingPotion) -> hatchingPotion.notes = "Pour this on an egg, and it will hatch as a #{hatchingPotion.text} pet."
###
view exports
###
module.exports.view = (view) ->
view.fn 'equipped', (user, type) ->
{gender, armorSet} = user?.preferences || {'m', 'v1'}
if type=='armor'
armor = user?.items?.armor || 0
if gender == 'f'
return if (parseInt(armor) == 0) then "f_armor_#{armor}_#{armorSet}" else "f_armor_#{armor}"
else
return "m_armor_#{armor}"
else if type=='head'
head = user?.items?.head || 0
if gender == 'f'
return if (parseInt(head) > 1) then "f_head_#{head}_#{armorSet}" else "f_head_#{head}"
else
return "m_head_#{head}"
view.fn "gold", (num) ->
if num
return (num).toFixed(1).split('.')[0]
else
return "0"
view.fn "silver", (num) ->
if num
(num).toFixed(2).split('.')[1]
else
return "00"
view.fn "copper", (num) ->
if num
c = (num).toFixed(4).split('.')[1]
c.toString().substr(2,2)
else
return "00"
###
server exports
###
+2 -1
View File
@@ -1,5 +1,6 @@
character = require './character'
browser = require './browser'
helpers = require './helpers'
module.exports.app = (appExports, model) ->
user = model.at('_user')
@@ -22,6 +23,6 @@ module.exports.app = (appExports, model) ->
uid = $(el).attr('data-uid')
model.ref '_profileActive', model.at("users.#{uid}")
model.set '_profileActiveMain', user.get('id') is uid
model.set '_profileActiveUsername', character.username model.get('_profileActive.auth')
model.set '_profileActiveUsername', helpers.username model.get('_profileActive.auth')
browser.setupTooltips(model)
-30
View File
@@ -4,36 +4,6 @@ _ = require 'underscore'
moment = require 'moment'
character = require './character'
module.exports.view = (view) ->
view.fn 'taskClasses', (task) ->
return unless task
{type, completed, value, repeat} = task
classes = type
# show as completed if completed (naturally) or not required for today
if type in ['todo', 'daily']
if completed or (repeat and repeat[helpers.dayMapping[moment().day()]]==false)
classes += " completed"
else
classes += " uncompleted"
if value < -20
classes += ' color-worst'
else if value < -10
classes += ' color-worse'
else if value < -1
classes += ' color-bad'
else if value < 1
classes += ' color-neutral'
else if value < 5
classes += ' color-good'
else if value < 10
classes += ' color-better'
else
classes += ' color-best'
return classes
module.exports.app = (appExports, model) ->
user = model.at('_user')