helpers.js
6.07 KB
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
'use strict';
const url = require('url');
const _ = require('lodash');
const config = require('../config/common');
const assetUrl = config.assetUrl;
const unitfromat = function(value, params) {
if (params.rem) {
value = Number(value) / (params.width || 40) + 'rem';
} else if (params.percent) {
value = value + '%';
} else {
value = value + 'px';
}
return value;
};
module.exports = {
getAnalysis: function(data, index) {
if (!data) {
return '';
}
let fp = {
F_ID: data.id,
F_INDEX: data.order,
F_NM: data.param.name,
I_INDEX: index
};
if (data.component[index] && data.component[index].name) {
fp.I_NM = data.component[index].name;
}
return JSON.stringify(fp);
},
isLazyLoad: function(type, index, opt) {
if (type !== 'fix' && index > 8) { // 活动页中 不是浮动层及8张图后面的采用懒加载
return opt.fn(this); // eslint-disable-line
} else {
return opt.inverse(this); // eslint-disable-line
}
},
styleFormat: function(styleObj, opts) {
let style = '';
if (styleObj && _.isObject(styleObj)) {
let params = opts.hash;
let keys = ['width', 'height', 'top', 'left', 'right', 'bottom'];
keys.forEach(function(k) {
if (styleObj[k]) {
style += k + ':' + unitfromat(styleObj[k], params) + ';';
}
});
}
return style;
},
tabName: function(index, tabnames) {
var names = tabnames.split(',');
return names[index];
},
tabStyle: function(index, count) {
let width = (100 / Number(count)).toFixed(2);
let style = 'height:100%;width:' + width + '%;';
style += 'left:' + (Number(index) * Number(width)) + '%;';
return style;
},
stringify: function(obj) {
if (!obj) {
return '';
}
return JSON.stringify(obj);
},
repeat: function(n, options) {
if (n) {
let str = '';
for (let i = 0; i < n; i++) {
let opt = {
index: i,
first: i === 0
};
str += options.fn(options, {
data: _.merge(opt, options)
});
}
return str;
}
return options.inverse(this);
},
imgSrc: function(imgSrc) {
return url.resolve(assetUrl, imgSrc);
},
image2: function(imageUrl, opts) {
if (imageUrl && _.isString(imageUrl)) {
let params = opts.hash;
let urls = imageUrl.split('?');
let query = urls[1] || '';
let uri = urls[0];
if (uri.indexOf('http:') === 0) {
uri = uri.replace('http:', '');
}
if (query) {
query = query.replace(/{width}/g, params.w)
.replace(/{height}/g, params.h)
.replace(/{mode}/g, (params.mode || 2));
if (query.indexOf('imageView2') === 0) {
if (params.q && query.indexOf('/q/') > 0) {
query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
} else if (params.q) {
query += '/q/' + params.q;
}
} else if (query.indexOf('imageMogr2') === 0) {
if (params.q && query.indexOf('/quality/') > 0) {
query = query.replace(/\/quality\/\d+/g, '/quality/' + params.q);
} else if (params.q) {
query += '/quality/' + params.q;
}
} else if (query.indexOf('imageView/') === 0) {
if (params.q && query.indexOf('/q/') > 0) {
query = query.replace(/\/q\/\d+/g, '/q/' + params.q);
} else if (params.q) {
query += '/q/' + params.q;
}
if (params.mode) {
query = query.replace(/imageView\/\d{1}\//, 'imageView/' + params.mode + '/');
}
}
} else {
query = 'imageView2/2/interlace/1/q/' + (params.q || 75);
}
return uri + '?' + query;
} else {
return '';
}
},
isEqualOr: function() {
let args = Array.prototype.slice.call(arguments);
let v1 = args[0];
let opt = args[args.length - 1];
let isTrue = false;
for (let i = 1; i < args.length - 1; i++) {
if (v1 === args[i]) {
isTrue = true;
break;
}
}
if (isTrue) {
return opt.fn(this); // eslint-disable-line
} else {
return opt.inverse(this); // eslint-disable-line
}
},
ifand: function() {
let args = Array.prototype.slice.call(arguments);
let opt = args[args.length - 1];
let isTrue = true;
for (let i = 0; i < args.length - 1; i++) {
if (!args[i]) {
isTrue = false;
break;
}
}
if (isTrue) {
return opt.fn(this);
} else {
return opt.inverse(this);
}
},
ifor: function() {
let args = Array.prototype.slice.call(arguments);
let opt = args[args.length - 1];
let isTrue = false;
for (let i = 0; i < args.length - 1; i++) {
if (args[i]) {
isTrue = true;
break;
}
}
if (isTrue) {
return opt.fn(this);
} else {
return opt.inverse(this);
}
},
/**
* 小于某zhi
*
* @param letiable
* @param number
*/
within: function(letiable, number, opt) {
if (letiable < number) {
return opt.fn(this);
} else {
return opt.inverse(this);
}
}
};