-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgeneration.js
More file actions
513 lines (484 loc) · 17.4 KB
/
Copy pathgeneration.js
File metadata and controls
513 lines (484 loc) · 17.4 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
* Name: generation.js
* Author: lgc0208@foxmail.com
* Version: 1.0.0
* Description: 实现参考文献格式生成器功能函数
* History:
1. Date: 2022-6-22
Author: lgc0208@foxmail.com
Modification: 实现对 GB/T 7714-2015 和 IEEE 格式的支持
*/
var _ = {
doc: document,
el: function (id) {
return this.doc.getElementById(id);
},
createEl: function (name, obj) {
var el = this.doc.createElement(name);
_.each(obj, function (value, key) {
switch (key) {
case 'html':
case 'text':
el.innerHTML = value;
break;
case 'style':
_.each(value, function (value, key) {
el.style[key] = value;
});
break;
default:
el.setAttribute(key, value);
}
});
return el;
},
// 遍历函数
each: function (arr, fn, max) {
max = max || Number.MAX_VALUE;
var i, item;
if (_.isArray(arr, false)) {
for (i = 0; item = arr[i]; i++) {
if (i >= max || fn(item, i) === false) {
break;
}
}
} else {
for (i in arr) {
max--;
if (max < 0 || fn(arr[i], i) === false) {
break;
}
}
}
},
isArray: function (value, strict) {
var type = Object.prototype.toString.apply(value),
isObj = value.length === undefined || _.isFunction(value);
if (strict !== false) {
return type === '[object Array]';
} else {
return !isObj;
}
},
isFunction: function (value) {
return typeof value === 'function' || typeof value === 'Function';
},
// 添加模板
templateIn: function (template, obj) {
var result = template;
_.each(obj, function (item, i) {
result = result.replace(new RegExp('{' + i + '}', 'g'), item);
});
return result;
},
mixIn: function (className, obj) {
var args = arguments,
proto = className.prototype,
i, o;
for (i = 1; o = args[i]; i++) {
_.each(o, function (value, key) {
proto[key] = value;
});
}
},
// 绑定事件函数
bind: function (element, event, fn, scope) {
var f = function () {
fn.apply(scope || this, arguments);
}
if (element.addEventListener) {
element.addEventListener(event, f, false);
} else if (element.attachEvent) {
element.attachEvent('on' + event, f);
}
},
trim: function (str) {
str = str.replace(/^\s+/, '');
var end = str.length - 1,
regx = /\s/;
while (regx.test(str.charAt(end))) {
end--;
}
return str.slice(0, end + 1);
}
}
// 运行流程
var Generation = function () {
this.initHooks();
this.initTypes();
this.showTypeOptions();
this.showInputs(this.defaultType, this.defaultFormatType);
this.bindEvents();
}
// 函数基础配置
Generation.config = {
itemRegx: /{(\[.*?\])?(.+?)(\[.*?\])?}/g,
defaultFormatType: 'GB/T 7714-2015',
defaultType: 'J',
types: {
'GB/T 7714-2015': {
J: {
name: '期刊',
required: '作者,题目,期刊名,出版年份,期数',
format: '{作者}. {题目}[J]. {期刊名}, {出版年份}{[, ]卷号}{[(]期数[)]}{[:]起止页码}.'
},
N: {
name: '报纸',
required: '作者,题目,报纸名,出版日期,版次',
format: '{作者}. {题目}[N]. {报纸名}, {出版日期}{[(]版次[)]}.'
},
M: {
name: '图书',
required: '作者,题目,版次,出版单位,出版年份',
format: '{作者}. {题目}[M]. {其他责任人[, ]}{版次[. ]}{出版地[: ]}{出版单位}{[, ]出版年份}{[ :]起止页码}.'
},
C: {
name: '会议录,论文集',
required: '作者,题目,论文集名,出版单位,出版年份',
format: '{作者}. {题目}[C]: {主编[.]}{论文集名}, {出版地[: ]}{出版单位[, ]}{出版年份}.'
},
D: {
name: '学位论文',
required: '作者,题目,保存单位,年份',
format: '{作者}. {题目}[D]. {保存地点[: ]}{保存单位}{[, ]年份}.'
},
R: {
name: '报告',
required: '作者,题目,报告地,主办单位,报告年份',
format: '{作者}. {题目}[R]. {报告地[: ]}{主办单位}{[, ]报告年份}.'
},
P: {
name: '专利',
required: '作者,专利名,专利号,公开日期',
format: '{作者}. {专利名[: ]}{专利号}[P]. {公开日期}'
},
S: {
name: '标准',
required: '作者,标准名,标准号,出版地,出版单位,出版年',
format: '{作者}. {标准名[: ]}{标准号}[S]. {出版地[: ]}{出版者}, {出版年}{[:]起止页码}'
},
'EB/OL': {
name: '电子资源',
required: '责任者,题目,引用日期,访问路径',
format: '{责任者}. {题目}[EB/OL]. {出版日期}[{引用日期}]. {访问路径}.'
}
},
'IEEE': {
J: {
name: '期刊',
required: '作者,题目,期刊名,卷数,起止页码,年份',
format: '{作者}, \"{题目}\", {期刊名}, vol.{卷数}, pp.{起止页码}, {月份[.]}{年份}.'
},
C: {
name: '会议',
required: '作者,题目,会议名称,地点,年份,起止页码',
format: '{作者}, \"{题目},\" in {会议名称}, {地点}, {年份}, pp.{起止页码}'
},
M: {
name: '图书',
required: '作者,书名,出版地,出版社,年份',
format: '{作者}, {书名}, {版本号[, ]}{卷数[. ]}{出版地}:{出版社}, {年份}{[, ]起止页码}'
},
'EB/OL': {
name: '电子资源',
required: '责任者,题目,地区,发布单位,年份,访问路径',
format: '{责任者}. {题目}. {地区}:{发布单位}, {年份}. [Online]. Available: {访问路径}'
}
}
},
descriptions: {
'作者': '多位作者请用英文逗号“,”分开',
'其他责任人': '多位作者请用英文逗号“,”分开',
'引用日期': '格式为 年-月-日,例如 2022-06-22',
'起止页码': '格式为 xx-yy,例如 4-16'
}
}
// 格式解析
Generation.fn = {
// 初始化标准种类
initTypes: function () {
var that = this;
_.each(this.types['GB/T 7714-2015'], function (type) {
var format = type['format'],
regx = that.itemRegx,
regxArr = format.match(regx);
type.required = type.required.split(',');
type.regxArr = {};
type.items = [];
_.each(regxArr, function (item, i) {
regx.lastIndex = 0;
var result = regx.exec(item);
if (result[1]) {
result[1] = result[1].substr(1, result[1].length - 2);
}
if (result[3]) {
result[3] = result[3].substr(1, result[3].length - 2);
}
type.regxArr[result[0]] = result;
type.items[i] = result[2];
});
});
_.each(this.types['IEEE'], function (type) {
var format = type['format'],
regx = that.itemRegx,
regxArr = format.match(regx);
type.required = type.required.split(',');
type.regxArr = {};
type.items = [];
_.each(regxArr, function (item, i) {
regx.lastIndex = 0;
var result = regx.exec(item);
if (result[1]) {
result[1] = result[1].substr(1, result[1].length - 2);
}
if (result[3]) {
result[3] = result[3].substr(1, result[3].length - 2);
}
type.regxArr[result[0]] = result;
type.items[i] = result[2];
});
});
},
// 判断该项是否是必选项
isRequired: function (itemName, typeName, formatName) {
var type = this.types[formatName][typeName];
if (!type) {
return false;
}
var required = type.required,
result = false,
i, item;
for (i = 0; item = required[i]; i++) {
if (item == itemName) {
result = true;
break;
}
}
return result;
},
// 获取格式类型下对应的文献类型
getItems: function (typeName, formatName) {
var type = this.types[formatName][typeName];
console.log(type);
return type && type.items;
},
// 解析格式
formatGeneration: function (typeName, formatName, obj) {
var that = this,
type = this.types[formatName][typeName];
if (!type) {
return false;
}
var regx = this.itemRegx;
var result = type.format.replace(regx, function (match) {
var regxArr = type.regxArr[match],
name = regxArr[2],
result = '';
if (obj[name]) {
regxArr[1] && (result += regxArr[1]);
if (name == '作者' || name == '其他责任人') {
obj[name] = that.formatAuthors(obj[name]);
}
result += obj[name];
regxArr[3] && (result += regxArr[3]);
}
return result;
});
return result;
},
// 多作者情况下的格式更改
formatAuthors: function (authors) {
var authorArr = authors.split(/,|,/),
isEn = false,
enRegx = /^[a-zA-Z \.-]+$/,
formated = /^[a-zA-Z]{2,}( [A-Z])*$/,
temp, lastName, i, len, result = [];
_.each(authorArr, function (author) {
author = _.trim(author);
if ((isEn = enRegx.test(author))) {
author = author.replace(/\.|-/g, '');
if (!formated.test(author)) {
temp = author.split(' ');
len = temp.length;
lastName = temp[--len];
author = lastName.charAt(0).toUpperCase() + lastName.substr(1);
for (i = 0; i < len; i++) {
if (temp[i]) {
author += ' ';
author += temp[i].charAt(0).toUpperCase();
}
}
}
}
result.push(author);
}, 3);
if (authorArr.length > 3) {
result.push(isEn ? 'et. al' : '等')
}
return result.join(', ')
}
}
// 运行控制逻辑
Generation.ctrl = {
formatSelector: '',
typeSelector: '',
inputForm: '',
resultDiv: '',
addBtn: '',
selectType: '',
initHooks: function () {
this.formatSelector = _.el('formatSelector')
this.typeSelector = _.el('typeSelector');
this.inputForm = _.el('inputForm');
this.resultDiv = _.el('resultDiv');
this.addBtn = _.el('addBtn');
},
// 展示需要用户输入的数据项
showInputs: function (typeName, formatName) {
this.formatType = formatName;
this.selectType = typeName;
var template = '<p><label>{inputName}<span class="required">{required}</span></label><input type="text" name="{inputName}" value=""/>{desc}</p>';
var that = this,
// 获取该文献类型下可以包含的数据项
items = this.getItems(typeName, formatName),
// 存储用以展现的 HTML 代码格式
html = '';
// 对文献类型包含的数据项进行查看
_.each(items, function (item, i) {
var obj = {
inputName: item,
required: that.isRequired(item, typeName, formatName) ? '*' : ' ',
desc: ''
};
if (that.descriptions[item]) {
obj.desc = '<span class="desc">' + that.descriptions[item] + '</span>';
}
html += _.templateIn(template, obj);
});
this.inputForm.innerHTML = html;
},
// 展示可选择的文献类型
showTypeOptions: function () {
var that = this,
types = this.types,
formatSelector = this.formatSelector,
typeSelector = this.typeSelector,
html = '';
// 格式类型
_.each(types, function (item, i) {
var option = _.createEl('option', {
text: i,
value: i
});
if (that.defaultFormatType == i) {
option.selected = true;
option.defaultselected = true;
}
formatSelector.appendChild(option);
});
// 文献类型
_.each(types[formatSelector.value], function (item, i) {
var option = _.createEl('option', {
text: item.name,
value: i
});
if (that.defaultType == i) {
option.selected = true;
option.defaultselected = true;
}
typeSelector.appendChild(option);
});
},
// 清空表单
cleanForm: function () {
var formEls = this.inputForm.elements;
_.each(formEls, function (el) {
el.value = '';
});
},
// 用户改变格式类型时对对应的文献类型进行更改
showFormatChange: function () {
var that = this,
types = this.types,
typeSelector = this.typeSelector,
html = '';
for (var i = typeSelector.length - 1; i >= 0; i--) {
typeSelector.removeChild(typeSelector[i])
}
// 文献类型
_.each(types[formatSelector.value], function (item, i) {
var option = _.createEl('option', {
text: item.name,
value: i
});
if (that.defaultType == i) {
option.selected = true;
option.defaultselected = true;
}
typeSelector.appendChild(option);
});
},
// 用户改变文献类型的响应函数
onTypeChange: function () {
var formatType = this.formatSelector.value;
var typeName = this.typeSelector.value;
this.showInputs(typeName, formatType);
},
// 用户改变格式类型的响应函数
onFormatChange: function () {
var formatType = this.formatSelector.value;
var typeName = this.defaultType;
this.showFormatChange();
this.showInputs(typeName, formatType);
},
// 添加条目
addContent: function () {
var formEls = this.inputForm.elements,
formatName = this.formatSelector.value,
formContent = {},
result, html;
_.each(formEls, function (item) {
if (item.name) {
formContent[item.name] = item.value;
}
});
result = this.formatGeneration(this.selectType, formatName, formContent);
html = '<div class="resultItem"><div class="referanceContent">' + result + '</div><a href="javascript:" class="delete">Delete</a><a href="javascript:" class="edit">Edit</a></div>';
this.resultDiv.innerHTML += html;
this.cleanForm();
},
// 监听结果编辑时间
listenResultDivEvents: function (e) {
e = e || window.event;
var target = e.target || e.srcElement,
resultDiv = this.resultDiv,
attr;
switch (target.className) {
// 用户选择删除
case 'delete':
resultDiv.removeChild(target.parentNode);
break;
// 用户选择编辑
case 'edit':
_.each(target.parentNode.childNodes, function (item) {
if (item.className == 'referanceContent') {
item.setAttribute('contentEditable', true);
item.focus();
item.onblur = function () {
item.removeAttribute('contentEditable');
}
}
});
break;
}
},
// 绑定事件
bindEvents: function () {
_.bind(this.formatSelector, 'change', this.onFormatChange, this);
_.bind(this.typeSelector, 'change', this.onTypeChange, this);
_.bind(this.addBtn, 'click', this.addContent, this);
_.bind(this.resultDiv, 'click', this.listenResultDivEvents, this);
}
}
_.mixIn(Generation, Generation.config, Generation.fn, Generation.ctrl);
new Generation();