/
index.js
315 lines (283 loc) · 10.8 KB
/
index.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Geddy JavaScript Web development framework
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://http://apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var utils = require('utilities')
, path = require('path')
, helperUtils = require('./utils')
, flashUtils = require('./flash')
, Data
// FIXME: pass this config in
, geddyConfig = (typeof geddy != 'undefined' && geddy.config) || {}
, _getAssetPath;
_getAssetPath = function (assetType, src) {
var host
, basePath
, hasProto = /^http:\/\/|^https:\/\/|^data:/
, isAbs;
// Does not handle exotic protocols like SPDY, or file:// URLs
isAbs = hasProto.test(src) || utils.file.isAbsolute(src);
if (isAbs) {
return src;
}
host = geddyConfig.assetHost || ''
basePath = geddyConfig.assetBasePath || '/';
// Include poss. extra leading slash in path.join to ensure
// there's at least one
return host + path.join('/', basePath, assetType, src);
};
/**
@name helpers
@namespace helpers
*/
// Copy a data object so it can be used in helpers
exports.registerData = {
name: 'registerData',
action: function (data) {
Data = data;
helperUtils.registerData(data);
}
}
/***
@name helpers#urlFor
@public
@function
@return {String} A path of the URL is returned
@description Returns a URL based on the `options` provided
@param {String/Object} options
@param {String} [options] Simply returns the string, unless the string is `back`
then a link to the previous url is returned
@param {Boolean} [options.relpath=false] If true, the relative URL is returned
@param {String} [options.protocol] The protocol to use(Uses Geddy's current protocol('http' default))
@param {String} [options.username] Includes a username in the path. Requires `password` or
it'll be ignored
@param {String} [options.password] Includes a password in the path. Requires `username` or
it'll be ignored
@param {String} [options.subdomain] Specify a subdomain to prepend to `domain`
@param {String} [options.domain] Specify a domain to use. Required if `relPath` is false(Uses
Geddy's `hostname` option as default)
@param {String} [options.host] Alias for `domain`
@param {Integer} [options.port] Specify a port to connect to
@param {String} [options.controller] Specify the controller to use for the path(Uses the current
controller, but only if `action` or `id` are also being used)
@param {String} [options.action] Specify the action to use for the path(Uses the index action if
a `controller` and no `id` is given, uses the show action if
a `id` action is given but no `action` option)
@param {String} [options.id] Specify an ID to use for the path
@param {Boolean} [options.trailingSlash=false] If true, a "/" will be appended to the end of the path
@param {String} [options.fragment] Appends a fragment to the end of the path e,g: "#string"
@param {String} [options.anchor] Alias for `fragment`
*/
exports.urlFor = {
name: 'urlFor',
action: function (options) {
return helperUtils.urls.urlFor(options);
}
}
/**
@name helpers#contentTag
@public
@function
@return {String} A HTML `tag` with the given `content` and `htmlOptions`
@description Creates a HTML tag with all the give options
@param {String} tag The HTML tag to use, safe to use any tag with this
@param {String} content The content to use with the tag, if it's a self closing tag
special cases will be used for content e,g: 'src' attribute is content for images
@param {Object} htmlOptions Includes any HTML attribute you want to include
*/
exports.contentTag = {
name: 'contentTag',
action: function (tag, content, htmlOptions) {
return helperUtils.tags.contentTagString(tag, content, htmlOptions);
}
}
/**
@name helpers#selectTag
@public
@function
@return {String} A HTML `select tag` with the given `optionsArray` and `htmlOptions`
@description Creates a HTML select tag using the given `optionsArray` to create HTML option elements.
Example: selectTag(['open', 'close'], todo.status, { class:'span6', name:'status' })
@param {Array} optionsArray The array options used to generate the tag elements.
It could be an array of strings, numbers or an object with value and text properties to be used
for the value attribute and option element content respectively.
@param {String} selectedOption optionally specify the selected option
@param {Object} htmlOptions Includes any HTML attribute you want to include in the select tag
*/
exports.selectTag = {
name: 'selectTag',
action: function (optionsArray, selectedOption, htmlOptions) {
return helperUtils.tags.selectTagString(optionsArray, selectedOption, htmlOptions);
}
}
/**
@name helpers#linkTo
@public
@function
@return {String} A HTML tag with a link to `options`
@description Creates a HTML tag that links to `options` with `content` as the content and includes
the given `htmlOptions`
@param {String} content The content to include for the link
@param {String/Object} options Creates the URL to link to, look at `urlFor` for more details
@param {Object} htmlOptions Includes any HTML attribute you want to include
*/
exports.linkTo = {
name: 'linkTo',
action: function (content, options, htmlOptions) {
var opts = options || {}
, htmlOpts = htmlOptions || {}
, url;
// This is for imageLink to avoid escaping
// FIXME: Kinda stupid putting this in htmlOptions, but options is taken.
if(htmlOpts._escapeContent !== false) {
content = utils.string.escapeXML(content);
delete htmlOpts._escapeContent;
}
// If options is a function, assume it was from a action helper
if (typeof opts === 'function') {
opts = String(opts());
}
url = exports.urlFor.action(opts);
htmlOpts.href = htmlOpts.href || url;
return exports.contentTag.action('a', content, htmlOpts);
}
};
/**
@name helpers#scriptLink
@public
@function
@return {String} A HTML script tag pointing to `source`
@description Creates a HTML script tag pointing to `source` with all the give `htmlOptions`
@param {String} source The URL to point to
@param {Object} htmlOptions Includes any HTML attribute you want to include
*/
exports.scriptLink = {
name: 'scriptLink',
action: function (source, htmlOptions) {
var opts = htmlOptions || {}
, src = _getAssetPath('js', source);
utils.mixin(opts, {src: src});
return exports.contentTag.action('script', '', opts);
}
};
/**
@name helpers#styleLink
@public
@function
@return {String} A HTML link tag pointing to `source`
@description Creates a HTML link tag pointing to `source` with all the give `htmlOptions`
@param {String} source The URL to point to
@param {Object} htmlOptions Includes any HTML attribute you want to include
*/
exports.styleLink = {
name: 'styleLink',
action: function (source, htmlOptions) {
var opts = htmlOptions || {}
, src = _getAssetPath('css', source);
return exports.contentTag.action('link', src, opts);
}
};
/**
@name helpers#imageTag
@public
@function
@return {String} A HTML img tag pointing to `source`
@description Creates a HTML img tag pointing to `source` with all the give `htmlOptions`
@param {String} source The image URL
@param {Object} htmlOptions Includes any HTML attribute you want to include
@param {String} size Creates a width and height from the given size, the style must be in the
format "{width}x{height}"(e,g: '40x50') or simply a single size(uses the
size as both width and height.) if the size doesn't follow either format
it'll be ignored
*/
exports.imageTag = {
name: 'imageTag',
action: function (source, htmlOptions) {
var opts = htmlOptions || {}
, src = _getAssetPath('img', source);
// If size option is included
if('size' in opts) {
var size = opts.size
, pat = /([0-9]+x[0-9]*|[0-9]+)/;
if (size.match(pat)) {
delete opts.size;
if (size.match(/[0-9]+x[0-9]*/)) {
// It's separate width and height
opts.width = opts.width || size.replace(/x[0-9]*/, '');
opts.height = opts.height || size.replace(/[0-9]+x/, '');
}
else {
// Same size width and height
opts.width = opts.width || size;
opts.height = opts.height || size;
}
}
else {
delete opts.size;
}
}
return exports.contentTag.action('img', src, opts);
}
}
/**
@name helpers#imageLink
@public
@function
@return {String} A HTML link tag pointing to `link` with the content as the image in `source`
@description Creates a HTML link tag pointing to `link` with all the give `linkOptions` and includes
the content as the image from `source` and it's `imageOptions`
@param {String} source The image URL
@param {String} link the URL to point to
@param {Object} imageOptions Includes any HTML attribute you want to include(Supports the `size`
option availble in `imageTag`)
@param {Object} linkOptions Includes any HTML attribute you want to include
*/
exports.imageLink = {
name: 'imageLink',
action: function (source, link, imageOptions, linkOptions) {
imageOptions = imageOptions || {};
linkOptions = linkOptions || {};
// If link is a function, assume it was from a action helper
if (typeof link === 'function') {
link = String(link());
}
linkOptions = utils.object.merge(linkOptions, { href: link });
linkOptions._escapeContent = false;
var imageTag = exports.imageTag.action(source, imageOptions);
return exports.linkTo.action(imageTag, link, linkOptions)
}
}
// Docs at utils.string.truncate
exports.truncate = {
name: 'truncate',
action: utils.string.truncate
};
// Docs at utils.string.truncateHTML
exports.truncateHTML = {
name: 'truncateHTML',
action: utils.string.truncateHTML
};
exports.displayFlash = {
name: 'displayFlash'
, action: flashUtils.displayFlash
};
exports.t = {
name: 't'
, action: function () {
var i18n = this.i18n;
return i18n.t.apply(i18n, arguments);
}
};