UNPKG

3.16 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9///
10var moment = require('moment');
11var numeral = require('numeral');
12var ClientDataQueryable = require('@themost/client').ClientDataQueryable;
13/**
14 * @class
15 * @param {HttpContext} context
16 * @constructor
17 */
18function HtmlViewHelper(context) {
19 /**
20 * @name HtmlViewHelper#context
21 * @type HttpContext
22 */
23 Object.defineProperty(this, 'context', {
24 get: function() {
25 return context;
26 }
27 });
28
29
30 Object.defineProperty(this, 'document', {
31 get: function() {
32 if (typeof document !== 'undefined') {
33 return document;
34 }
35 var document = null;
36 return document;
37 } , configurable:false, enumerable:false
38 });
39
40}
41
42/**
43 *
44 * @param {HttpContext} context
45 */
46HtmlViewHelper.create = function(context) {
47 return new HtmlViewHelper(context);
48};
49
50/**
51 * Returns an anti-forgery hidden input element
52 * @returns {String}
53 */
54HtmlViewHelper.prototype.antiforgery = function() {
55 var $view = this.parent;
56 //create token
57 var context = $view.context, value = context.getApplication().getEncryptionStrategy().encrypt(JSON.stringify({ id: Math.floor(Math.random() * 1000000), url:context.request.url, date:new Date() }));
58 //try to set cookie
59 context.response.setHeader('Set-Cookie','.CSRF='.concat(value));
60 return $view.writer.writeAttribute('type', 'hidden')
61 .writeAttribute('id', '_CSRFToken')
62 .writeAttribute('name', '_CSRFToken')
63 .writeAttribute('value', value)
64 .writeFullBeginTag('input')
65 .toString();
66};
67
68HtmlViewHelper.prototype.element = function(obj) {
69 return this.document.parentWindow.angular.element(obj);
70};
71/**
72 * Returns a two-letter string which represents the current culture e.g. en, fr etc
73 * @returns {string}
74 */
75HtmlViewHelper.prototype.lang = function() {
76 var c= this.context.culture();
77 if (typeof c === 'string') {
78 if (c.length>=2) {
79 return c.toLowerCase().substring(0,2);
80 }
81 }
82 //in all cases return default culture
83 return 'en';
84};
85/**
86 * Returns a an instance of moment.js formatter
87 * @param {*} value
88 * @returns {*|moment.Moment}
89 */
90HtmlViewHelper.prototype.moment = function(value) {
91 return moment(value);
92};
93/**
94 * Returns an instance of numeral.js formatter
95 * @param {*} value
96 * @returns {*}
97 */
98HtmlViewHelper.prototype.numeral = function(value) {
99 return numeral(value);
100};
101/**
102 * @returns {ClientDataQueryable}
103 */
104HtmlViewHelper.prototype.getRequestLink = function() {
105 return ClientDataQueryable.parse(this.context.request.url);
106};
107
108/**
109 * @param {string} appRelativeUrl
110 * @returns {string}
111 */
112HtmlViewHelper.prototype.resolveUrl = function(appRelativeUrl) {
113 return this.context.getApplication().resolveUrl(appRelativeUrl);
114};
115
116if (typeof exports !== 'undefined')
117{
118 module.exports.HtmlViewHelper = HtmlViewHelper;
119}
\No newline at end of file