-
Notifications
You must be signed in to change notification settings - Fork 17
/
tap_i18n_db-client.coffee
135 lines (106 loc) · 3.91 KB
/
tap_i18n_db-client.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
removeTrailingUndefs = share.helpers.removeTrailingUndefs
extend = $.extend
share.i18nCollectionTransform = (doc, collection) ->
for route in collection._disabledOnRoutes
if route.test(window.location.pathname)
return doc
collection_base_language = collection._base_language
language = TAPi18n.getLanguage()
if not language? or not doc.i18n?
delete doc.i18n
return doc
dialect_of = share.helpers.dialectOf language
doc = _.extend({}, doc) # protect original object
if dialect_of? and doc.i18n[dialect_of]?
if language != collection_base_language
extend(true, doc, doc.i18n[dialect_of])
else
# if the collection's base language is the dialect that is used as the
# current language
doc = extend(true, {}, doc.i18n[dialect_of], doc)
if doc.i18n[language]?
extend(true, doc, doc.i18n[language])
delete doc.i18n
return doc
share.i18nCollectionExtensions = (obj) ->
original =
find: obj.find
findOne: obj.findOne
local_session = new ReactiveDict()
for method of original
do (method) ->
obj[method] = (selector, options) ->
local_session.get("force_lang_switch_reactivity_hook")
return original[method].apply(obj, removeTrailingUndefs [selector, options])
obj.forceLangSwitchReactivity = _.once ->
Deps.autorun () ->
local_session.set "force_lang_switch_reactivity_hook", TAPi18n.getLanguage()
return
obj._disabledOnRoutes = []
obj._disableTransformationOnRoute = (route) ->
obj._disabledOnRoutes.push(route)
if Package.autopublish?
obj.forceLangSwitchReactivity()
return obj
TAPi18n.subscribe = (name) ->
local_session = new ReactiveDict
local_session.set("ready", false)
# parse arguments
params = Array.prototype.slice.call(arguments, 1)
callbacks = {}
if params.length
lastParam = params[params.length - 1]
if (typeof lastParam == "function")
callbacks.onReady = params.pop()
else if (lastParam and (typeof lastParam.onReady == "function" or
typeof lastParam.onError == "function"))
callbacks = params.pop()
# We want the onReady/onError methods to be called only once (not for every language change)
onReadyCalled = false
onErrorCalled = false
original_onReady = callbacks.onReady
callbacks.onReady = ->
if onErrorCalled
return
local_session.set("ready", true)
if original_onReady?
original_onReady()
if callbacks.onError?
callbacks.onError = ->
if onReadyCalled
_.once callbacks.onError
subscription = null
subscription_computation = null
subscribe = ->
# subscription_computation, depends on TAPi18n.getLanguage(), to
# resubscribe once the language gets changed.
subscription_computation = Deps.autorun () ->
lang_tag = TAPi18n.getLanguage()
subscription =
Meteor.subscribe.apply @, removeTrailingUndefs [].concat(name, params, lang_tag, callbacks)
# if the subscription is already ready:
local_session.set("ready", subscription.ready())
# If TAPi18n is called in a computation, to maintain Meteor.subscribe
# behavior (which never gets invalidated), we don't want the computation to
# get invalidated when TAPi18n.getLanguage get invalidated (when language get
# changed).
current_computation = Deps.currentComputation
if currentComputation?
# If TAPi18n.subscribe was called in a computation, call subscribe in a
# non-reactive context, but make sure that if the computation is getting
# invalidated also the subscription computation
# (invalidations are allowed up->bottom but not bottom->up)
Deps.onInvalidate ->
subscription_computation.invalidate()
Deps.nonreactive () ->
subscribe()
else
# If there is no computation
subscribe()
return {
ready: () ->
local_session.get("ready")
stop: () ->
subscription_computation.stop()
_getSubscription: -> subscription
}