|
4 | 4 | (global.VueLineClamp = factory()); |
5 | 5 | }(this, (function () { 'use strict'; |
6 | 6 |
|
7 | | -const css = 'display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis'; |
8 | | -const currentValueProp = "vLineClampValue"; |
| 7 | +var css = 'display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis'; |
| 8 | +var currentValueProp = "vLineClampValue"; |
9 | 9 |
|
10 | 10 | function defaultFallbackFunc(el, bindings, lines) { |
11 | | - if(lines){ |
12 | | - let lineHeight = parseInt(bindings.arg); |
| 11 | + if (lines) { |
| 12 | + var lineHeight = parseInt(bindings.arg); |
13 | 13 | if (isNaN(lineHeight)) { |
14 | 14 | console.warn('line-height argument for vue-line-clamp must be a number (of pixels), falling back to 16px'); |
15 | 15 | lineHeight = 16; |
16 | 16 | } |
17 | 17 |
|
18 | | - let maxHeight = lineHeight * lines; |
| 18 | + var maxHeight = lineHeight * lines; |
19 | 19 |
|
20 | | - el.style.maxHeight = maxHeight ? maxHeight+'px' : ''; |
| 20 | + el.style.maxHeight = maxHeight ? maxHeight + 'px' : ''; |
21 | 21 | el.style.overflowX = 'hidden'; |
22 | | - el.style.lineHeight = lineHeight+'px'; // to ensure consistency |
| 22 | + el.style.lineHeight = lineHeight + 'px'; // to ensure consistency |
23 | 23 | } else { |
24 | 24 | el.style.maxHeight = el.style.overflowX = ''; |
25 | 25 | } |
26 | 26 | } |
27 | 27 |
|
28 | | -const truncateText = function (el, bindings, useFallbackFunc) { |
29 | | - let lines = parseInt(bindings.value); |
| 28 | +var truncateText = function truncateText(el, bindings, useFallbackFunc) { |
| 29 | + var lines = parseInt(bindings.value); |
30 | 30 | if (isNaN(lines)) { |
31 | 31 | console.error('Parameter for vue-line-clamp must be a number'); |
32 | | - return |
33 | | - } |
34 | | - else if (lines !== el[currentValueProp]) { |
| 32 | + return; |
| 33 | + } else if (lines !== el[currentValueProp]) { |
35 | 34 | el[currentValueProp] = lines; |
36 | 35 |
|
37 | 36 | if (useFallbackFunc) { |
38 | 37 | useFallbackFunc(el, bindings, lines); |
39 | | - } |
40 | | - else { |
| 38 | + } else { |
41 | 39 | el.style.webkitLineClamp = lines ? lines : ''; |
42 | 40 | } |
43 | 41 | } |
44 | 42 | }; |
45 | 43 |
|
46 | | -const VueLineClamp = { |
47 | | - install (Vue, options) { |
| 44 | +var VueLineClamp = { |
| 45 | + install: function install(Vue, options) { |
48 | 46 | options = Object.assign({ |
49 | 47 | importCss: false |
50 | 48 | }, options); |
51 | 49 |
|
52 | 50 | if (options.importCss) { |
53 | | - const stylesheets = window.document.styleSheets, |
54 | | - rule = `.vue-line-clamp{${css}}`; |
| 51 | + var stylesheets = window.document.styleSheets, |
| 52 | + rule = '.vue-line-clamp{' + css + '}'; |
55 | 53 | if (stylesheets && stylesheets[0] && stylesheets.insertRule) { |
56 | 54 | stylesheets.insertRule(rule); |
57 | 55 | } else { |
58 | | - let link = window.document.createElement('style'); |
| 56 | + var link = window.document.createElement('style'); |
59 | 57 | link.id = 'vue-line-clamp'; |
60 | 58 | link.appendChild(window.document.createTextNode(rule)); |
61 | 59 | window.document.head.appendChild(link); |
62 | 60 | } |
63 | 61 | } |
64 | 62 |
|
65 | | - const useFallbackFunc = |
66 | | - "webkitLineClamp" in document.body.style |
67 | | - ? undefined |
68 | | - : options.fallbackFunc || defaultFallbackFunc; |
| 63 | + var useFallbackFunc = "webkitLineClamp" in document.body.style ? undefined : options.fallbackFunc || defaultFallbackFunc; |
69 | 64 |
|
70 | 65 | Vue.directive('line-clamp', { |
71 | 66 | currentValue: 0, |
72 | | - bind (el) { |
| 67 | + bind: function bind(el) { |
73 | 68 | if (!options.importCss) { |
74 | 69 | el.style.cssText += css; |
75 | | - } |
76 | | - else { |
| 70 | + } else { |
77 | 71 | el.classList.add('vue-line-clamp'); |
78 | 72 | } |
| 73 | + }, |
79 | 74 |
|
| 75 | + inserted: function inserted(el, bindings) { |
| 76 | + return truncateText(el, bindings, useFallbackFunc); |
| 77 | + }, |
| 78 | + updated: function updated(el, bindings) { |
| 79 | + return truncateText(el, bindings, useFallbackFunc); |
80 | 80 | }, |
81 | | - inserted: (el, bindings) => truncateText(el, bindings, useFallbackFunc), |
82 | | - updated: (el, bindings) => truncateText(el, bindings, useFallbackFunc), |
83 | | - componentUpdated: (el, bindings) => truncateText(el, bindings, useFallbackFunc) |
| 81 | + componentUpdated: function componentUpdated(el, bindings) { |
| 82 | + return truncateText(el, bindings, useFallbackFunc); |
| 83 | + } |
84 | 84 | }); |
85 | 85 | } |
86 | 86 | }; |
|
0 commit comments