-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
111 lines (103 loc) · 2.93 KB
/
helpers.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
// @themost-framework 2.0 Codename Blueshift Copyright (c) 2017-2025, THEMOST LP All rights reserved
var moment = require('moment');
var numeral = require('numeral');
var ClientDataQueryable = require('@themost/client').ClientDataQueryable;
/**
* @class
* @param {HttpContext} context
* @constructor
*/
function HtmlViewHelper(context) {
/**
* @name HtmlViewHelper#context
* @type HttpContext
*/
Object.defineProperty(this, 'context', {
get: function() {
return context;
}
});
Object.defineProperty(this, 'document', {
get: function() {
if (typeof document !== 'undefined') {
return document;
}
var document = null;
return document;
} , configurable:false, enumerable:false
});
}
/**
*
* @param {HttpContext} context
*/
HtmlViewHelper.create = function(context) {
return new HtmlViewHelper(context);
};
/**
* Returns an anti-forgery hidden input element
* @returns {String}
*/
HtmlViewHelper.prototype.antiforgery = function() {
var $view = this.parent;
//create token
var context = $view.context, value = context.getApplication().getEncryptionStrategy().encrypt(JSON.stringify({ id: Math.floor(Math.random() * 1000000), url:context.request.url, date:new Date() }));
//try to set cookie
context.response.setHeader('Set-Cookie','.CSRF='.concat(value));
return $view.writer.writeAttribute('type', 'hidden')
.writeAttribute('id', '_CSRFToken')
.writeAttribute('name', '_CSRFToken')
.writeAttribute('value', value)
.writeFullBeginTag('input')
.toString();
};
HtmlViewHelper.prototype.element = function(obj) {
return this.document.parentWindow.angular.element(obj);
};
/**
* Returns a two-letter string which represents the current culture e.g. en, fr etc
* @returns {string}
*/
HtmlViewHelper.prototype.lang = function() {
var c= this.context.culture();
if (typeof c === 'string') {
if (c.length>=2) {
return c.toLowerCase().substring(0,2);
}
}
//in all cases return default culture
return 'en';
};
/**
* Returns a an instance of moment.js formatter
* @param {*} value
* @returns {*|moment.Moment}
*/
HtmlViewHelper.prototype.moment = function(value) {
return moment(value);
};
/**
* Returns an instance of numeral.js formatter
* @param {*} value
* @returns {*}
*/
HtmlViewHelper.prototype.numeral = function(value) {
return numeral(value);
};
/**
* @returns {ClientDataQueryable}
*/
HtmlViewHelper.prototype.getRequestLink = function() {
return ClientDataQueryable.parse(this.context.request.url);
};
/**
* @param {string} appRelativeUrl
* @returns {string}
*/
HtmlViewHelper.prototype.resolveUrl = function(appRelativeUrl) {
return this.context.getApplication().resolveUrl(appRelativeUrl);
};
if (typeof exports !== 'undefined')
{
module.exports.HtmlViewHelper = HtmlViewHelper;
}