forked from tauren/tmpl-precompile
/
namespace.coffee
147 lines (114 loc) · 4.04 KB
/
namespace.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
136
137
138
139
140
141
142
143
144
145
146
147
async = require 'async'
###
Namespacer(settings, callback)
Description: Creates a Namespacer instance for processing namespace data
settings:
"namespace": String(Required), namespace object when including templates to browser
"templates": Array(Required), names of templates to be precompiled
callback: (err, res) ->
###
class Namespacer
###
constructor
Description: Bind settings to object
###
constructor: (settings, callback) ->
if settings.namespace? then @groupNamespace = settings.namespace
else callback('Error: \'namespace\' is not configured')
if settings.templates? then @templates = settings.templates
else callback('Error: \'templates\' is not configured')
if settings.skiproot? then @skiproot = settings.skiproot
if @groupNamespace? and @templates?
@callback = callback
@namespaces = []
@result = []
@init()
init: ->
self = @
async.auto({
checkGroupNamespace: (callback) -> self.checkGroupNamespace(callback)
checkTemplateNamespaces: (callback) -> self.checkTemplateNamespaces(callback)
createNamespaces: ['checkGroupNamespace', 'checkTemplateNamespaces', (callback) ->
self.createNamespaces(callback)
]
}, (err) ->
if err? then self.callback(err)
else
self.callback(null, self.result)
)
###
checkGroupNamespace
Description: Checks group namespace if needed to be splitted
###
checkGroupNamespace : (callback) ->
self = @
# Checks if needed to split namespace
if @groupNamespace.indexOf('.') > 0
self.splitNamespace(self.groupNamespace, true)
callback(null)
else
@namespaces.push(@groupNamespace)
callback(null)
###
checkTemplateNamespaces
Description: Checks template namespaces if needed to be splitted
###
checkTemplateNamespaces : (callback) ->
self = @
counter = 0
next = ->
counter++
if counter is self.templates.length
arr = []
for i in [0...self.namespaces.length]
unless arr.indexOf(self.namespaces[i]) > 0
arr.push(self.namespaces[i])
if i is self.namespaces.length-1
self.namespaces = arr
callback(null)
for templateName in self.templates
if templateName.indexOf('/') > 0
self.splitNamespace(templateName)
next()
else next()
###
createNamespaces
Description: Prepends required namespace declarations for the browser
###
createNamespaces : (callback) ->
self = @
next = ->
if not self.skiproot
self.result[0] = 'var ' + self.result[0]
callback(null)
# Get the maximum index for group namespaces in the namespaces array
groupNamespaceLength = (@groupNamespace).split('.').length
# Appends the group namespace declarations
for g in [0...groupNamespaceLength]
if g > 0 or not @skiproot
self.result.push "#{@namespaces[g]} = #{@namespaces[g]} || {};"
if groupNamespaceLength is @namespaces.length
next()
else
for t in [groupNamespaceLength...@namespaces.length]
self.result.push "#{@groupNamespace}.#{@namespaces[t]} = #{@groupNamespace}.#{@namespaces[t]} || {};"
if t is @namespaces.length-1
next()
###
splitNamespace(name, isGroupNamespace)
Description: Helper, splits each string and adds it into the 'namespaces' array for later processing
Params:
name: Namespace name
isGroupNamespace: boolean value to determine if the current iteration is for group
###
splitNamespace: (name, isGroupNamespace) ->
arr = name.split(/\.|\//) # Split template into array
# Determine the maximum depth to compile
if isGroupNamespace then max = arr.length else max = arr.length - 1
@namespaces.push arr[0] # Push base namespace
str = arr[0] # Set base namespace
for i in [1...max]
str += ".#{arr[i]}" # Appends additional levels of namespacing
@namespaces.push str
module.exports = (settings, callback) ->
new Namespacer(settings, callback)