@itfin/components 1.3.95 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/package.json +1 -1
  2. package/src/assets/scss/_css_variables.scss +2 -2
  3. package/src/assets/scss/_variables.scss +18 -7
  4. package/src/assets/scss/components/_button.scss +10 -0
  5. package/src/assets/scss/components/_pagination.scss +4 -1
  6. package/src/assets/scss/components/_select.scss +1 -3
  7. package/src/assets/scss/components/_text-field.scss +17 -7
  8. package/src/assets/scss/main.scss +36 -2
  9. package/src/components/app/message.js +1 -1
  10. package/src/components/button/Button.vue +4 -2
  11. package/src/components/filter/FilterAmountRange.vue +50 -42
  12. package/src/components/filter/FilterBadge.vue +25 -22
  13. package/src/components/filter/FilterFacetsList.vue +1 -1
  14. package/src/components/filter/FilterPanel.vue +82 -27
  15. package/src/components/filter/index.stories.js +0 -2
  16. package/src/components/icon/Icon.vue +3 -1
  17. package/src/components/icon/components/fi_fingerprint.vue +4 -0
  18. package/src/components/icon/components/nomi-arrow-down.vue +4 -0
  19. package/src/components/icon/components/nomi-arrow-right-top.vue +4 -0
  20. package/src/components/icon/components/nomi-arrow-up.vue +4 -0
  21. package/src/components/icon/components/nomi-arrows.vue +7 -0
  22. package/src/components/icon/components/nomi-calendar-alt.vue +4 -0
  23. package/src/components/icon/components/nomi-calendar.vue +11 -0
  24. package/src/components/icon/components/nomi-card.vue +4 -0
  25. package/src/components/icon/components/nomi-close.vue +5 -0
  26. package/src/components/icon/components/nomi-eye-close.vue +4 -0
  27. package/src/components/icon/components/nomi-eye-open.vue +4 -0
  28. package/src/components/icon/components/nomi-filter.vue +4 -0
  29. package/src/components/icon/components/nomi-hide.vue +4 -0
  30. package/src/components/icon/components/nomi-money.vue +4 -0
  31. package/src/components/icon/components/nomi-move-left.vue +4 -0
  32. package/src/components/icon/components/nomi-move-right.vue +4 -0
  33. package/src/components/icon/components/nomi-person.vue +5 -0
  34. package/src/components/icon/components/nomi-pin.vue +7 -0
  35. package/src/components/icon/components/nomi-sort-asc.vue +7 -0
  36. package/src/components/icon/components/nomi-sort-desc.vue +7 -0
  37. package/src/components/icon/components/nomi-table-view.vue +4 -0
  38. package/src/components/icon/components/nomi-tag.vue +4 -0
  39. package/src/components/icon/components/nomi-target.vue +4 -0
  40. package/src/components/icon/components/nomi-text.vue +6 -0
  41. package/src/components/icon/components/nomi-unpin.vue +7 -0
  42. package/src/components/icon/convert-icons.js +11 -0
  43. package/src/components/icon/icons.js +302 -277
  44. package/src/components/icon/new-icons/arrow-down.svg +3 -0
  45. package/src/components/icon/new-icons/arrow-right-top.svg +3 -0
  46. package/src/components/icon/new-icons/arrow-up.svg +3 -0
  47. package/src/components/icon/new-icons/arrows.svg +6 -0
  48. package/src/components/icon/new-icons/calendar-alt.svg +3 -0
  49. package/src/components/icon/new-icons/calendar.svg +10 -0
  50. package/src/components/icon/new-icons/card.svg +3 -0
  51. package/src/components/icon/new-icons/clear.svg +3 -0
  52. package/src/components/icon/new-icons/close.svg +4 -0
  53. package/src/components/icon/new-icons/eye-close.svg +3 -0
  54. package/src/components/icon/new-icons/eye-open.svg +3 -0
  55. package/src/components/icon/new-icons/filter.svg +3 -0
  56. package/src/components/icon/new-icons/hide.svg +3 -0
  57. package/src/components/icon/new-icons/money.svg +3 -0
  58. package/src/components/icon/new-icons/move-left.svg +3 -0
  59. package/src/components/icon/new-icons/move-right.svg +3 -0
  60. package/src/components/icon/new-icons/person.svg +4 -0
  61. package/src/components/icon/new-icons/pin.svg +6 -0
  62. package/src/components/icon/new-icons/sort-asc.svg +6 -0
  63. package/src/components/icon/new-icons/sort-desc.svg +6 -0
  64. package/src/components/icon/new-icons/table-view.svg +3 -0
  65. package/src/components/icon/new-icons/tag.svg +3 -0
  66. package/src/components/icon/new-icons/target.svg +3 -0
  67. package/src/components/icon/new-icons/text.svg +5 -0
  68. package/src/components/icon/new-icons/unpin.svg +6 -0
  69. package/src/components/pagination/Pagination.vue +3 -2
  70. package/src/components/pagination/Pagination2.vue +176 -0
  71. package/src/components/panels/Panel.vue +5 -1
  72. package/src/components/panels/PanelItemEdit.vue +61 -0
  73. package/src/components/panels/PanelList.vue +2 -0
  74. package/src/components/select/Select.vue +1 -1
  75. package/src/components/sortable/draggable.js +2 -1
  76. package/src/components/table/Table2.vue +24 -1
  77. package/src/components/table/TableBody.vue +7 -2
  78. package/src/components/table/TableGroup.vue +8 -4
  79. package/src/components/table/TableHeader.vue +101 -24
  80. package/src/components/table/TableRows.vue +5 -3
  81. package/src/components/table/index.stories.js +22 -200
  82. package/src/components/table/table2.scss +179 -52
  83. package/src/components/text-field/MoneyField.vue +2 -2
  84. package/src/components/text-field/TextField.vue +12 -8
  85. package/src/components/tree/TreeEditor.vue +602 -0
  86. package/src/components/view/View.vue +119 -0
  87. package/src/components/view/index.stories.js +588 -0
  88. package/src/helpers/formatters.js +14 -1
  89. package/src/helpers/tree/cdbl.js +32 -0
  90. package/src/helpers/tree/cint.js +43 -0
  91. package/src/helpers/tree/domDrag.js +911 -0
  92. package/src/helpers/tree/domFinds.js +20 -0
  93. package/src/helpers/tree/domGetPointFromEvent.js +53 -0
  94. package/src/helpers/tree/domIsClientXYIn.js +65 -0
  95. package/src/helpers/tree/domRemove.js +50 -0
  96. package/src/helpers/tree/evem.js +27 -0
  97. package/src/helpers/tree/genID.js +56 -0
  98. package/src/helpers/tree/isEle.js +28 -0
  99. package/src/helpers/tree/isestr.js +35 -0
  100. package/src/helpers/tree/isint.js +40 -0
  101. package/src/helpers/tree/isnbr.js +24 -0
  102. package/src/helpers/tree/isnum.js +38 -0
  103. package/src/helpers/tree/ispint.js +41 -0
  104. package/src/helpers/tree/isstr.js +27 -0
  105. package/src/helpers/tree.js +30 -0
  106. package/src/helpers/vuetifyColor.js +136 -0
  107. package/src/locales/en.js +13 -0
  108. package/src/locales/uk.js +11 -0
  109. package/src/components/table/TableRow.vue +0 -221
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 前端找尋多DOM元素
3
+ *
4
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/domFinds.test.js Github}
5
+ * @memberOf wsemi
6
+ * @param {String} query 輸入查詢字串
7
+ * @returns {Array} 回傳DOM元素陣列
8
+ * @example
9
+ * need test in browser
10
+ *
11
+ * let eles = domFinds('[name="abc"]')
12
+ *
13
+ */
14
+ function domFinds(query) {
15
+ let r = document.querySelectorAll(query)
16
+ return r
17
+ }
18
+
19
+
20
+ export default domFinds
@@ -0,0 +1,53 @@
1
+ import size from 'lodash/size'
2
+ import get from 'lodash/get'
3
+
4
+
5
+ /**
6
+ * 前端DOM元素事件取得使用者滑鼠或第一觸控點座標
7
+ *
8
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/domGetPointFromEvent.test.js Github}
9
+ * @memberOf wsemi
10
+ * @param {HTEMLEvent} e 輸入dom事件物件
11
+ * @example
12
+ * need test in browser
13
+ *
14
+ * window.addEventListener('mousemove', (e) => {
15
+ * console.log(domGetPointFromEvent(e))
16
+ * })
17
+ *
18
+ */
19
+ function domGetPointFromEvent(e) {
20
+
21
+ //check
22
+ let cx = get(e, 'clientX', null)
23
+ let cy = get(e, 'clientY', null)
24
+ let px = get(e, 'pageX', null)
25
+ let py = get(e, 'pageY', null)
26
+ if (cx !== null && cy !== null && px !== null && py !== null) {
27
+ return {
28
+ clientX: cx,
29
+ clientY: cy,
30
+ pageX: px,
31
+ pageY: py,
32
+ }
33
+ }
34
+
35
+ //check
36
+ let touches = get(e, 'changedTouches', []) //touchend時touches長度為0, 故需改用changedTouches
37
+ if (size(touches) !== 1) {
38
+ return null
39
+ }
40
+
41
+ //p
42
+ let p = touches[0]
43
+
44
+ return {
45
+ clientX: get(p, 'clientX', null),
46
+ clientY: get(p, 'clientY', null),
47
+ pageX: get(p, 'pageX', null),
48
+ pageY: get(p, 'pageY', null),
49
+ }
50
+ }
51
+
52
+
53
+ export default domGetPointFromEvent
@@ -0,0 +1,65 @@
1
+ import isNumber from 'lodash/isNumber'
2
+ import isEle from './isEle.js'
3
+
4
+
5
+ /**
6
+ * 前端計算clientX與clientY是否位於DOM元素內
7
+ *
8
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/domIsClientXYIn.test.js Github}
9
+ * @memberOf wsemi
10
+ * @param {Number} clientX 輸入點的clientX座標
11
+ * @param {Number} clientY 輸入點的clientY座標
12
+ * @param {HTMLElement} ele 輸入元素
13
+ * @returns {Boolean} 回傳是否位於DOM元素內布林值
14
+ * @example
15
+ * need test in browser
16
+ *
17
+ * let p = {
18
+ * clientX: 'from event',
19
+ * clientY: 'from event',
20
+ * }
21
+ * let ele = document.querySelector('#id')
22
+ * let b = domIsClientXYIn(p.clientX, p.clientY, ele)
23
+ * // => true or false
24
+ *
25
+ */
26
+ function domIsClientXYIn(clientX, clientY, ele) {
27
+
28
+ //check
29
+ if (!isNumber(clientX)) {
30
+ console.log('clientX is not Number', clientX)
31
+ return
32
+ }
33
+ if (!isNumber(clientY)) {
34
+ console.log('clientY is not Number', clientY)
35
+ return
36
+ }
37
+ if (!isEle(ele)) {
38
+ console.log('ele is not HTMLElement', ele)
39
+ return
40
+ }
41
+
42
+ //calc rt
43
+ let rt
44
+ try {
45
+ rt = ele.getBoundingClientRect()
46
+ }
47
+ catch (err) {
48
+ console.log('invalid ele for getBoundingClientRect', err)
49
+ return
50
+ }
51
+
52
+ //r
53
+ let r = false
54
+ try {
55
+ r = clientX >= rt.left && clientX <= rt.right && clientY >= rt.top && clientY <= rt.bottom
56
+ }
57
+ catch (err) {
58
+ console.log('catch when calculating page information of element', err)
59
+ }
60
+
61
+ return r
62
+ }
63
+
64
+
65
+ export default domIsClientXYIn
@@ -0,0 +1,50 @@
1
+ import each from 'lodash/each'
2
+ import isEle from './isEle.js'
3
+ import isestr from './isestr.js'
4
+ import domFinds from './domFinds.js'
5
+
6
+
7
+ /**
8
+ * 前端找尋DOM元素並刪除
9
+ *
10
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/domRemove.test.js Github}
11
+ * @memberOf wsemi
12
+ * @param {String|HTMLElement} inp 輸入查詢字串或dom函數
13
+ * @example
14
+ * need test in browser
15
+ *
16
+ * let ele = document.querySelector('#id')
17
+ * domRemove(ele)
18
+ *
19
+ */
20
+ function domRemove(inp) {
21
+
22
+ function remove(ele) {
23
+ ele.parentNode.removeChild(ele)
24
+ }
25
+
26
+ //isEle
27
+ if (isEle(inp)) {
28
+ let ele = inp
29
+ remove(ele)
30
+ return
31
+ }
32
+
33
+ //query
34
+ if (!isestr(inp)) {
35
+ return
36
+ }
37
+ let query = inp
38
+
39
+ //find
40
+ let rs = domFinds(query)
41
+
42
+ //remove
43
+ each(rs, function(ele) {
44
+ remove(ele)
45
+ })
46
+
47
+ }
48
+
49
+
50
+ export default domRemove
@@ -0,0 +1,27 @@
1
+ import EventEmitter from 'eventemitter3'
2
+
3
+
4
+ /**
5
+ * EventEmitter from eventemitter3
6
+ *
7
+ * See: {@link https://github.com/primus/eventemitter3 eventemitter3}
8
+ *
9
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/evem.test.js Github}
10
+ * @memberOf wsemi
11
+ * @example
12
+ *
13
+ * let ev = wsemi.evem()
14
+ * ev.on('evName',function(msg){
15
+ * console.log(msg)
16
+ * // => {abc: 12.34}
17
+ * })
18
+ * let data = {abc:12.34}
19
+ * ev.emit('evName',data)
20
+ *
21
+ */
22
+ function evem() {
23
+ return new EventEmitter()
24
+ }
25
+
26
+
27
+ export default evem
@@ -0,0 +1,56 @@
1
+ import ispint from './ispint.js'
2
+ import cint from './cint.js'
3
+
4
+
5
+ let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
6
+ let radix = chars.length
7
+
8
+ /**
9
+ * 產生隨機id
10
+ *
11
+ * Depend on: {@link https://gist.github.com/Wind4/3baa40b26b89b686e4f2 Math.uuid.js}
12
+ *
13
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/genID.test.js Github}
14
+ * @memberOf wsemi
15
+ * @param {Integer} [len=32] 輸入uuid長度,為正整數,預設32
16
+ * @returns {String} 回傳uuid字串
17
+ * @example
18
+ *
19
+ * console.log(genID())
20
+ * // => Is1NyImU3A9fyqFyYBWuJu4ivXXcGZAb (is random)
21
+ *
22
+ */
23
+ function genID(len = 32) {
24
+ let uuid = []
25
+
26
+ //check
27
+ if (ispint(len)) {
28
+ len = cint(len)
29
+ }
30
+ else {
31
+ len = 32
32
+ }
33
+
34
+ //uuid
35
+ for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
36
+
37
+ //rfc4122, version 4 form
38
+ // //requires these characters
39
+ // uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
40
+ // uuid[14] = '4'
41
+ // //fill in random data. At i==19 set the high bits of clock sequence as per rfc4122, sec. 4.1.5
42
+ // let r
43
+ // for (i = 0; i < 36; i++) {
44
+ // if (!uuid[i]) {
45
+ // r = 0 | Math.random() * 16
46
+ // uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
47
+ // }
48
+ // }
49
+
50
+ let r = uuid.join('')
51
+
52
+ return r
53
+ }
54
+
55
+
56
+ export default genID
@@ -0,0 +1,28 @@
1
+ import isElement from 'lodash/isElement'
2
+
3
+
4
+ /**
5
+ * 前端判斷元素是否存在
6
+ *
7
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/isEle.test.js Github}
8
+ * @memberOf wsemi
9
+ * @param {Element} ele 輸入DOM元素
10
+ * @returns {Boolean} 回傳判斷布林值
11
+ * @example
12
+ * need test in browser
13
+ *
14
+ * let ele = document.querySelector('#id')
15
+ * console.log(isEle(ele))
16
+ * // => true or false
17
+ *
18
+ */
19
+ function isEle(ele) {
20
+ return isElement(ele)
21
+ // return (
22
+ // typeof HTMLElement === 'object' ? ele instanceof HTMLElement
23
+ // : ele && typeof ele === 'object' && ele !== null && ele.nodeType === 1 && typeof ele.nodeName === 'string'
24
+ // )
25
+ }
26
+
27
+
28
+ export default isEle
@@ -0,0 +1,35 @@
1
+ import isstr from './isstr.js'
2
+
3
+
4
+ /**
5
+ * 判斷是否為有效字串
6
+ *
7
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/isestr.test.js Github}
8
+ * @memberOf wsemi
9
+ * @param {*} v 輸入任意資料
10
+ * @returns {Boolean} 回傳判斷布林值
11
+ * @example
12
+ *
13
+ * console.log(isestr('1.25'))
14
+ * // => true
15
+ *
16
+ * console.log(isestr(125))
17
+ * // => false
18
+ *
19
+ * console.log(isestr(''))
20
+ * // => false
21
+ *
22
+ */
23
+ function isestr(v) {
24
+
25
+ //check
26
+ if (isstr(v)) {
27
+ if (v !== '') {
28
+ return true
29
+ }
30
+ }
31
+ return false
32
+ }
33
+
34
+
35
+ export default isestr
@@ -0,0 +1,40 @@
1
+ import isInteger from 'lodash/isInteger'
2
+ import isnum from './isnum.js'
3
+ import cdbl from './cdbl.js'
4
+
5
+
6
+ /**
7
+ * 判斷是否為整數
8
+ *
9
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/isint.test.js Github}
10
+ * @memberOf wsemi
11
+ * @param {*} v 輸入任意資料
12
+ * @returns {Boolean} 回傳判斷布林值
13
+ * @example
14
+ *
15
+ * console.log(isint('1.25'))
16
+ * // => false
17
+ *
18
+ * console.log(isint('125'))
19
+ * // => true
20
+ *
21
+ * console.log(isint(1.25))
22
+ * // => false
23
+ *
24
+ * console.log(isint(125))
25
+ * // => true
26
+ *
27
+ */
28
+ function isint(v) {
29
+
30
+ if (isnum(v)) {
31
+ v = cdbl(v)
32
+ return isInteger(v)
33
+ }
34
+ else {
35
+ return false
36
+ }
37
+ }
38
+
39
+
40
+ export default isint
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 判斷是否為數字
3
+ *
4
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/isnbr.test.js Github}
5
+ * @memberOf wsemi
6
+ * @param {*} v 輸入任意資料
7
+ * @returns {Boolean} 回傳判斷布林值
8
+ * @example
9
+ *
10
+ * console.log(isnbr(1.25))
11
+ * // => true
12
+ *
13
+ * console.log(isnbr('1.25'))
14
+ * // => false
15
+ *
16
+ */
17
+ function isnbr(v) {
18
+
19
+ let c = Object.prototype.toString.call(v)
20
+ return c === '[object Number]'
21
+ }
22
+
23
+
24
+ export default isnbr
@@ -0,0 +1,38 @@
1
+ import isestr from './isestr.js'
2
+ import isnbr from './isnbr.js'
3
+
4
+
5
+ /**
6
+ * 判斷是否為數字
7
+ *
8
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/isnum.test.js Github}
9
+ * @memberOf wsemi
10
+ * @param {*} v 輸入任意資料
11
+ * @returns {Boolean} 回傳判斷布林值
12
+ * @example
13
+ *
14
+ * console.log(isnum(0))
15
+ * // => true
16
+ *
17
+ * console.log(isnum(1.25))
18
+ * // => true
19
+ *
20
+ * console.log(isnum('-125'))
21
+ * // => true
22
+ *
23
+ */
24
+ function isnum(v) {
25
+
26
+ let b = false
27
+ if (isestr(v)) {
28
+ b = !isNaN(Number(v))
29
+ }
30
+ else if (isnbr(v)) {
31
+ b = true
32
+ }
33
+
34
+ return b
35
+ }
36
+
37
+
38
+ export default isnum
@@ -0,0 +1,41 @@
1
+ import isint from './isint.js'
2
+ import cint from './cint.js'
3
+
4
+
5
+ /**
6
+ * 判斷是否為正整數
7
+ * 正整數不包含0,為大於0的整數
8
+ *
9
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/ispint.test.js Github}
10
+ * @memberOf wsemi
11
+ * @param {*} v 輸入任意資料
12
+ * @returns {Boolean} 回傳判斷布林值
13
+ * @example
14
+ *
15
+ * console.log(ispint(0))
16
+ * // => false
17
+ *
18
+ * console.log(ispint(125))
19
+ * // => true
20
+ *
21
+ * console.log(ispint('125'))
22
+ * // => true
23
+ *
24
+ * console.log(ispint(1.25))
25
+ * // => false
26
+ *
27
+ */
28
+ function ispint(v) {
29
+
30
+ //check
31
+ if (!isint(v)) {
32
+ return false
33
+ }
34
+
35
+ let r = cint(v) > 0
36
+
37
+ return r
38
+ }
39
+
40
+
41
+ export default ispint
@@ -0,0 +1,27 @@
1
+ /**
2
+ * 判斷是否為字串
3
+ *
4
+ * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/isstr.test.js Github}
5
+ * @memberOf wsemi
6
+ * @param {*} v 輸入任意資料
7
+ * @returns {Boolean} 回傳判斷布林值
8
+ * @example
9
+ *
10
+ * console.log(isstr(0))
11
+ * // => false
12
+ *
13
+ * console.log(isstr('0'))
14
+ * // => true
15
+ *
16
+ * console.log(isstr(''))
17
+ * // => true
18
+ *
19
+ */
20
+ function isstr(v) {
21
+
22
+ let c = Object.prototype.toString.call(v)
23
+ return c === '[object String]'
24
+ }
25
+
26
+
27
+ export default isstr
@@ -0,0 +1,30 @@
1
+ export
2
+ function flattenTree (items, spaces = 4, level = 0, parentNode = null, path = [], childrenKey = 'Children') {
3
+ return items.reduce((acc, val, index) => acc.concat({
4
+ ...val,
5
+ path: [...path, childrenKey, index],
6
+ parentNode,
7
+ level,
8
+ levelPrefix: String.fromCharCode(0xA0).repeat(level * spaces)
9
+ }, ...flattenTree(val[childrenKey] || [], spaces, level + 1, val, [...path, childrenKey, index])), []);
10
+ }
11
+
12
+ export
13
+ function buildTree (items, idKey = 'id', parentKey = 'parentId', childrenKey = 'Children') {
14
+
15
+ const buildNode = (items, id, parent) => items
16
+ .filter(item => item[parentKey] === parent)
17
+ .map(item => ({ ...item, [childrenKey]: buildNode(items, item[idKey], item[idKey]) }));
18
+
19
+ return buildNode(items);
20
+ }
21
+
22
+ export
23
+ function removeNode (tree, compareFn, childrenKey = 'Children') {
24
+ return tree.reduce((acc, val) => {
25
+ if (compareFn(val)) {
26
+ return acc;
27
+ }
28
+ return acc.concat({ ...val, [childrenKey]: removeNode(val[childrenKey] || [], compareFn) });
29
+ }, []);
30
+ }
@@ -0,0 +1,136 @@
1
+ import camelCase from 'lodash/camelCase';
2
+ import get from 'lodash/get';
3
+
4
+ const colors = {
5
+ red: { base: '#f44336', lighten5: '#ffebee', lighten4: '#ffcdd2', lighten3: '#ef9a9a', lighten2: '#e57373', lighten1: '#ef5350', darken1: '#e53935', darken2: '#d32f2f', darken3: '#c62828', darken4: '#b71c1c', accent1: '#ff8a80', accent2: '#ff5252', accent3: '#ff1744', accent4: '#d50000' },
6
+ pink: { base: '#e91e63', lighten5: '#fce4ec', lighten4: '#f8bbd0', lighten3: '#f48fb1', lighten2: '#f06292', lighten1: '#ec407a', darken1: '#d81b60', darken2: '#c2185b', darken3: '#ad1457', darken4: '#880e4f', accent1: '#ff80ab', accent2: '#ff4081', accent3: '#f50057', accent4: '#c51162' },
7
+ purple: { base: '#9c27b0', lighten5: '#f3e5f5', lighten4: '#e1bee7', lighten3: '#ce93d8', lighten2: '#ba68c8', lighten1: '#ab47bc', darken1: '#8e24aa', darken2: '#7b1fa2', darken3: '#6a1b9a', darken4: '#4a148c', accent1: '#ea80fc', accent2: '#e040fb', accent3: '#d500f9', accent4: '#aa00ff' },
8
+ deepPurple: { base: '#673ab7', lighten5: '#ede7f6', lighten4: '#d1c4e9', lighten3: '#b39ddb', lighten2: '#9575cd', lighten1: '#7e57c2', darken1: '#5e35b1', darken2: '#512da8', darken3: '#4527a0', darken4: '#311b92', accent1: '#b388ff', accent2: '#7c4dff', accent3: '#651fff', accent4: '#6200ea' },
9
+ indigo: { base: '#3f51b5', lighten5: '#e8eaf6', lighten4: '#c5cae9', lighten3: '#9fa8da', lighten2: '#7986cb', lighten1: '#5c6bc0', darken1: '#3949ab', darken2: '#303f9f', darken3: '#283593', darken4: '#1a237e', accent1: '#8c9eff', accent2: '#536dfe', accent3: '#3d5afe', accent4: '#304ffe' },
10
+ blue: { base: '#2196f3', lighten5: '#e3f2fd', lighten4: '#bbdefb', lighten3: '#90caf9', lighten2: '#64b5f6', lighten1: '#42a5f5', darken1: '#1e88e5', darken2: '#1976d2', darken3: '#1565c0', darken4: '#0d47a1', accent1: '#82b1ff', accent2: '#448aff', accent3: '#2979ff', accent4: '#2962ff' },
11
+ lightBlue: { base: '#03a9f4', lighten5: '#e1f5fe', lighten4: '#b3e5fc', lighten3: '#81d4fa', lighten2: '#4fc3f7', lighten1: '#29b6f6', darken1: '#039be5', darken2: '#0288d1', darken3: '#0277bd', darken4: '#01579b', accent1: '#80d8ff', accent2: '#40c4ff', accent3: '#00b0ff', accent4: '#0091ea' },
12
+ cyan: { base: '#00bcd4', lighten5: '#e0f7fa', lighten4: '#b2ebf2', lighten3: '#80deea', lighten2: '#4dd0e1', lighten1: '#26c6da', darken1: '#00acc1', darken2: '#0097a7', darken3: '#00838f', darken4: '#006064', accent1: '#84ffff', accent2: '#18ffff', accent3: '#00e5ff', accent4: '#00b8d4' },
13
+ teal: { base: '#009688', lighten5: '#e0f2f1', lighten4: '#b2dfdb', lighten3: '#80cbc4', lighten2: '#4db6ac', lighten1: '#26a69a', darken1: '#00897b', darken2: '#00796b', darken3: '#00695c', darken4: '#004d40', accent1: '#a7ffeb', accent2: '#64ffda', accent3: '#1de9b6', accent4: '#00bfa5' },
14
+ green: { base: '#4caf50', lighten5: '#e8f5e9', lighten4: '#c8e6c9', lighten3: '#a5d6a7', lighten2: '#81c784', lighten1: '#66bb6a', darken1: '#43a047', darken2: '#388e3c', darken3: '#2e7d32', darken4: '#1b5e20', accent1: '#b9f6ca', accent2: '#69f0ae', accent3: '#00e676', accent4: '#00c853' },
15
+ lightGreen: { base: '#8bc34a', lighten5: '#f1f8e9', lighten4: '#dcedc8', lighten3: '#c5e1a5', lighten2: '#aed581', lighten1: '#9ccc65', darken1: '#7cb342', darken2: '#689f38', darken3: '#558b2f', darken4: '#33691e', accent1: '#ccff90', accent2: '#b2ff59', accent3: '#76ff03', accent4: '#64dd17' },
16
+ lime: { base: '#cddc39', lighten5: '#f9fbe7', lighten4: '#f0f4c3', lighten3: '#e6ee9c', lighten2: '#dce775', lighten1: '#d4e157', darken1: '#c0ca33', darken2: '#afb42b', darken3: '#9e9d24', darken4: '#827717', accent1: '#f4ff81', accent2: '#eeff41', accent3: '#c6ff00', accent4: '#aeea00' },
17
+ yellow: { base: '#ffeb3b', lighten5: '#fffde7', lighten4: '#fff9c4', lighten3: '#fff59d', lighten2: '#fff176', lighten1: '#ffee58', darken1: '#fdd835', darken2: '#fbc02d', darken3: '#f9a825', darken4: '#f57f17', accent1: '#ffff8d', accent2: '#ffff00', accent3: '#ffea00', accent4: '#ffd600' },
18
+ amber: { base: '#ffc107', lighten5: '#fff8e1', lighten4: '#ffecb3', lighten3: '#ffe082', lighten2: '#ffd54f', lighten1: '#ffca28', darken1: '#ffb300', darken2: '#ffa000', darken3: '#ff8f00', darken4: '#ff6f00', accent1: '#ffe57f', accent2: '#ffd740', accent3: '#ffc400', accent4: '#ffab00' },
19
+ orange: { base: '#ff9800', lighten5: '#fff3e0', lighten4: '#ffe0b2', lighten3: '#ffcc80', lighten2: '#ffb74d', lighten1: '#ffa726', darken1: '#fb8c00', darken2: '#f57c00', darken3: '#ef6c00', darken4: '#e65100', accent1: '#ffd180', accent2: '#ffab40', accent3: '#ff9100', accent4: '#ff6d00' },
20
+ deepOrange: { base: '#ff5722', lighten5: '#fbe9e7', lighten4: '#ffccbc', lighten3: '#ffab91', lighten2: '#ff8a65', lighten1: '#ff7043', darken1: '#f4511e', darken2: '#e64a19', darken3: '#d84315', darken4: '#bf360c', accent1: '#ff9e80', accent2: '#ff6e40', accent3: '#ff3d00', accent4: '#dd2c00' },
21
+ brown: { base: '#795548', lighten5: '#efebe9', lighten4: '#d7ccc8', lighten3: '#bcaaa4', lighten2: '#a1887f', lighten1: '#8d6e63', darken1: '#6d4c41', darken2: '#5d4037', darken3: '#4e342e', darken4: '#3e2723' },
22
+ blueGrey: { base: '#607d8b', lighten5: '#eceff1', lighten4: '#cfd8dc', lighten3: '#b0bec5', lighten2: '#90a4ae', lighten1: '#78909c', darken1: '#546e7a', darken2: '#455a64', darken3: '#37474f', darken4: '#263238' },
23
+ grey: { base: '#9e9e9e', lighten5: '#fafafa', lighten4: '#f5f5f5', lighten3: '#eeeeee', lighten2: '#e0e0e0', lighten1: '#bdbdbd', darken1: '#757575', darken2: '#616161', darken3: '#424242', darken4: '#212121' },
24
+ shades: { black: '#000000', white: '#ffffff', transparent: 'transparent' }
25
+ };
26
+
27
+ function getKey0 (c) {
28
+ // 'blue-grey' => 'blueGrey'
29
+ let key0 = c.trim();
30
+ key0 = camelCase(key0);
31
+ return key0;
32
+ }
33
+
34
+ function getKey1 (c) {
35
+ // 'lighten-5' => 'lighten5', null => 'base'
36
+ if (typeof c !== 'string') {
37
+ c = 'base';
38
+ }
39
+ let key1 = c.trim();
40
+ key1 = key1.replace('-', '').trim();
41
+ return key1;
42
+ }
43
+
44
+ function getHexColor (key0, key1, def) {
45
+ let hex = get(colors, `${key0}.${key1}`, '');
46
+ if (hex === null) {
47
+ hex = def;
48
+ }
49
+ return hex;
50
+ }
51
+
52
+ function color2hex (color) {
53
+ // speed
54
+ if (color === 'white') {
55
+ return '#fff';
56
+ } else if (color === 'black') {
57
+ return '#000';
58
+ } else if (color === 'transparent') {
59
+ return 'rgba(0,0,0,0)';
60
+ }
61
+
62
+ const cc = color.split(' ');
63
+
64
+ // key0, key1
65
+ const key0 = getKey0(cc[0]);
66
+ const key1 = getKey1(cc[1]);
67
+
68
+ return getHexColor(key0, key1, color);
69
+ }
70
+
71
+ export default color2hex;
72
+
73
+ function hexToRGB(hex) {
74
+ const r = parseInt(hex.substring(1, 3), 16);
75
+ const g = parseInt(hex.substring(3, 5), 16);
76
+ const b = parseInt(hex.substring(5, 7), 16);
77
+ return [r, g, b];
78
+ }
79
+
80
+ function RGBToHex(rgb) {
81
+ const r = rgb[0].toString(16).padStart(2, '0');
82
+ const g = rgb[1].toString(16).padStart(2, '0');
83
+ const b = rgb[2].toString(16).padStart(2, '0');
84
+ return '#' + r + g + b;
85
+ }
86
+
87
+ /**
88
+ * Calculate new color based on start color and step
89
+ *
90
+ * @param startColor - hex color to start with
91
+ * @param step - step number
92
+ * @param totalSteps - total number of steps
93
+ * @param isLighten - if true, will generate lighter color, otherwise darker
94
+ */
95
+ function calculateNewColor(startColor, step, totalSteps, isLighten) {
96
+ const startRGB = hexToRGB(startColor);
97
+ const maxRGB = Math.max(...startRGB);
98
+
99
+ const newRGB = [];
100
+
101
+ for (let i = 0; i < 3; i++) {
102
+ let newValue = startRGB[i];
103
+
104
+ if (isLighten) {
105
+ newValue += Math.round((maxRGB - startRGB[i]) * (step / totalSteps));
106
+ } else {
107
+ newValue -= Math.round(startRGB[i] * (step / totalSteps));
108
+ }
109
+
110
+ newRGB.push(newValue);
111
+ }
112
+
113
+ return RGBToHex(newRGB);
114
+ }
115
+
116
+ /**
117
+ * Generate gradient colors based on start color
118
+ *
119
+ * @param startColor - hex color to start with (will be the first color in the array)
120
+ * @param numColors - number of colors to return
121
+ * @param isLighten - if true, will generate lighter colors, otherwise darker
122
+ */
123
+ export function generateGradientColors(startColor, numColors = 5, isLighten = true) {
124
+ if (!startColor || !numColors) {
125
+ return [];
126
+ }
127
+ const colors = [];
128
+ colors.push(startColor);
129
+
130
+ for (let i = 1; i < numColors; i++) {
131
+ const newColor = calculateNewColor(startColor, i, numColors, isLighten);
132
+ colors.push(newColor);
133
+ }
134
+
135
+ return colors;
136
+ }