@windwalker-io/unicorn-next 0.1.0

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 (175) hide show
  1. package/.editorconfig +18 -0
  2. package/.gulp.json +7 -0
  3. package/bin/release.mjs +47 -0
  4. package/dist/chunks/_arrayPush.js +168 -0
  5. package/dist/chunks/_arrayPush.js.map +1 -0
  6. package/dist/chunks/_baseRest.js +73 -0
  7. package/dist/chunks/_baseRest.js.map +1 -0
  8. package/dist/chunks/_commonjsHelpers.js +7 -0
  9. package/dist/chunks/_commonjsHelpers.js.map +1 -0
  10. package/dist/chunks/_getPrototype.js +130 -0
  11. package/dist/chunks/_getPrototype.js.map +1 -0
  12. package/dist/chunks/button-radio.js +147 -0
  13. package/dist/chunks/button-radio.js.map +1 -0
  14. package/dist/chunks/checkboxes-multi-select.js +44 -0
  15. package/dist/chunks/checkboxes-multi-select.js.map +1 -0
  16. package/dist/chunks/cloneDeep.js +287 -0
  17. package/dist/chunks/cloneDeep.js.map +1 -0
  18. package/dist/chunks/cropper.min.js +5 -0
  19. package/dist/chunks/cropper.min.js.map +1 -0
  20. package/dist/chunks/field-cascade-select.js +256 -0
  21. package/dist/chunks/field-cascade-select.js.map +1 -0
  22. package/dist/chunks/field-file-drag.js +218 -0
  23. package/dist/chunks/field-file-drag.js.map +1 -0
  24. package/dist/chunks/field-flatpickr.js +893 -0
  25. package/dist/chunks/field-flatpickr.js.map +1 -0
  26. package/dist/chunks/field-modal-select.js +403 -0
  27. package/dist/chunks/field-modal-select.js.map +1 -0
  28. package/dist/chunks/field-modal-tree.js +790 -0
  29. package/dist/chunks/field-modal-tree.js.map +1 -0
  30. package/dist/chunks/field-multi-uploader.js +256 -0
  31. package/dist/chunks/field-multi-uploader.js.map +1 -0
  32. package/dist/chunks/field-repeatable.js +132 -0
  33. package/dist/chunks/field-repeatable.js.map +1 -0
  34. package/dist/chunks/field-single-image-drag.js +338 -0
  35. package/dist/chunks/field-single-image-drag.js.map +1 -0
  36. package/dist/chunks/form.js +154 -0
  37. package/dist/chunks/form.js.map +1 -0
  38. package/dist/chunks/grid.js +345 -0
  39. package/dist/chunks/grid.js.map +1 -0
  40. package/dist/chunks/http-client.js +229 -0
  41. package/dist/chunks/http-client.js.map +1 -0
  42. package/dist/chunks/iframe-modal.js +124 -0
  43. package/dist/chunks/iframe-modal.js.map +1 -0
  44. package/dist/chunks/index.js +309 -0
  45. package/dist/chunks/index.js.map +1 -0
  46. package/dist/chunks/isArguments.js +146 -0
  47. package/dist/chunks/isArguments.js.map +1 -0
  48. package/dist/chunks/keep-tab.js +101 -0
  49. package/dist/chunks/keep-tab.js.map +1 -0
  50. package/dist/chunks/legacy.js +210 -0
  51. package/dist/chunks/legacy.js.map +1 -0
  52. package/dist/chunks/list-dependent.js +231 -0
  53. package/dist/chunks/list-dependent.js.map +1 -0
  54. package/dist/chunks/s3-multipart-uploader.js +172 -0
  55. package/dist/chunks/s3-multipart-uploader.js.map +1 -0
  56. package/dist/chunks/s3-uploader.js +136 -0
  57. package/dist/chunks/s3-uploader.js.map +1 -0
  58. package/dist/chunks/show-on.js +237 -0
  59. package/dist/chunks/show-on.js.map +1 -0
  60. package/dist/chunks/tinymce.js +196 -0
  61. package/dist/chunks/tinymce.js.map +1 -0
  62. package/dist/chunks/ui-bootstrap5.js +71 -0
  63. package/dist/chunks/ui-bootstrap5.js.map +1 -0
  64. package/dist/chunks/unicorn.js +2202 -0
  65. package/dist/chunks/unicorn.js.map +1 -0
  66. package/dist/chunks/validation.js +854 -0
  67. package/dist/chunks/validation.js.map +1 -0
  68. package/dist/editor.css +1 -0
  69. package/dist/index.d.ts +1427 -0
  70. package/dist/multi-level-menu.css +1 -0
  71. package/dist/switcher.css +1 -0
  72. package/dist/unicorn-next.css +12 -0
  73. package/dist/unicorn.js +125 -0
  74. package/dist/unicorn.js.map +1 -0
  75. package/fusionfile.mjs +155 -0
  76. package/images/ajax-loader.gif +0 -0
  77. package/images/placeholder/avatar.png +0 -0
  78. package/images/placeholder/image-16x10.png +0 -0
  79. package/images/placeholder/image-16x9.png +0 -0
  80. package/images/placeholder/image-1x1.png +0 -0
  81. package/images/placeholder/image-4x3.png +0 -0
  82. package/package.json +102 -0
  83. package/scss/bootstrap/multi-level-menu.scss +121 -0
  84. package/scss/editor.scss +116 -0
  85. package/scss/field/file-drag.scss +102 -0
  86. package/scss/field/single-image-drag.scss +88 -0
  87. package/scss/field/vue-drag-uploader.scss +160 -0
  88. package/scss/switcher.scss +156 -0
  89. package/src/app.ts +128 -0
  90. package/src/bootstrap/button-radio.ts +208 -0
  91. package/src/bootstrap/keep-tab.ts +155 -0
  92. package/src/composable/index.ts +21 -0
  93. package/src/composable/useCheckboxesMultiSelect.ts +22 -0
  94. package/src/composable/useFieldCascadeSelect.ts +9 -0
  95. package/src/composable/useFieldFileDrag.ts +9 -0
  96. package/src/composable/useFieldFlatpickr.ts +3 -0
  97. package/src/composable/useFieldModalSelect.ts +6 -0
  98. package/src/composable/useFieldModalTree.ts +3 -0
  99. package/src/composable/useFieldMultiUploader.ts +3 -0
  100. package/src/composable/useFieldRepeatable.ts +9 -0
  101. package/src/composable/useFieldSingleImageDrag.ts +5 -0
  102. package/src/composable/useForm.ts +43 -0
  103. package/src/composable/useGrid.ts +57 -0
  104. package/src/composable/useHttp.ts +8 -0
  105. package/src/composable/useIframeModal.ts +9 -0
  106. package/src/composable/useListDependent.ts +26 -0
  107. package/src/composable/useQueue.ts +13 -0
  108. package/src/composable/useS3Uploader.ts +32 -0
  109. package/src/composable/useShowOn.ts +9 -0
  110. package/src/composable/useStack.ts +13 -0
  111. package/src/composable/useTinymce.ts +29 -0
  112. package/src/composable/useTomSelect.ts +72 -0
  113. package/src/composable/useUIBootstrap5.ts +48 -0
  114. package/src/composable/useUniDirective.ts +32 -0
  115. package/src/composable/useValidation.ts +39 -0
  116. package/src/data.ts +36 -0
  117. package/src/events.ts +73 -0
  118. package/src/legacy/legacy.ts +186 -0
  119. package/src/legacy/loader.ts +125 -0
  120. package/src/module/checkboxes-multi-select.ts +54 -0
  121. package/src/module/field-cascade-select.ts +292 -0
  122. package/src/module/field-file-drag.ts +292 -0
  123. package/src/module/field-flatpickr.ts +127 -0
  124. package/src/module/field-modal-select.ts +174 -0
  125. package/src/module/field-modal-tree.ts +27 -0
  126. package/src/module/field-multi-uploader.ts +361 -0
  127. package/src/module/field-repeatable.ts +202 -0
  128. package/src/module/field-single-image-drag.ts +468 -0
  129. package/src/module/form.ts +223 -0
  130. package/src/module/grid.ts +465 -0
  131. package/src/module/http-client.ts +243 -0
  132. package/src/module/iframe-modal.ts +167 -0
  133. package/src/module/list-dependent.ts +321 -0
  134. package/src/module/s3-multipart-uploader.ts +300 -0
  135. package/src/module/s3-uploader.ts +234 -0
  136. package/src/module/show-on.ts +173 -0
  137. package/src/module/tinymce.ts +263 -0
  138. package/src/module/ui-bootstrap5.ts +107 -0
  139. package/src/module/validation.ts +1019 -0
  140. package/src/plugin/index.ts +1 -0
  141. package/src/plugin/php-adapter.ts +65 -0
  142. package/src/polyfill/form-request-submit.ts +31 -0
  143. package/src/polyfill/index.ts +9 -0
  144. package/src/service/animate.ts +58 -0
  145. package/src/service/crypto.ts +27 -0
  146. package/src/service/dom-watcher.ts +62 -0
  147. package/src/service/dom.ts +265 -0
  148. package/src/service/helper.ts +48 -0
  149. package/src/service/index.ts +10 -0
  150. package/src/service/lang.ts +122 -0
  151. package/src/service/loader.ts +152 -0
  152. package/src/service/router.ts +118 -0
  153. package/src/service/ui.ts +497 -0
  154. package/src/service/uri.ts +106 -0
  155. package/src/types/base.ts +9 -0
  156. package/src/types/index.ts +4 -0
  157. package/src/types/modal-tree.ts +12 -0
  158. package/src/types/plugin.ts +6 -0
  159. package/src/types/shims.d.ts +18 -0
  160. package/src/types/ui.ts +6 -0
  161. package/src/unicorn.ts +63 -0
  162. package/src/utilities/arr.ts +25 -0
  163. package/src/utilities/base.ts +9 -0
  164. package/src/utilities/data.ts +48 -0
  165. package/src/utilities/index.ts +5 -0
  166. package/src/utilities/tree.ts +20 -0
  167. package/src/vue/components/ModalTree/ModalTreeApp.vue +175 -0
  168. package/src/vue/components/ModalTree/TreeItem.vue +262 -0
  169. package/src/vue/components/ModalTree/TreeModal.vue +225 -0
  170. package/tests/test.js +4 -0
  171. package/tsconfig.js.json +25 -0
  172. package/tsconfig.json +17 -0
  173. package/vite.assets.config.ts +61 -0
  174. package/vite.config.test.ts +36 -0
  175. package/vite.config.ts +112 -0
@@ -0,0 +1,102 @@
1
+ .c-file-drag {
2
+ --bs-card-border-color: var(--bs-gray-400);
3
+ --fd-delete-color: var(--bs-danger);
4
+
5
+ overflow: hidden;
6
+ border: 1px solid var(--bs-card-border-color, #ddd);
7
+
8
+ label {
9
+ border: none;
10
+ }
11
+ }
12
+
13
+ .c-file-drag-input {
14
+ position: relative;
15
+ display: inline-block;
16
+ width: 100%;
17
+ min-height: 100px;
18
+ cursor: pointer;
19
+
20
+ input {
21
+ position: relative;
22
+ z-index: 2;
23
+ width: 100%;
24
+ margin: 0;
25
+ overflow: hidden;
26
+ opacity: 0;
27
+ height: 100%;
28
+ cursor: pointer;
29
+
30
+ &.hover + label {
31
+ background-color: #efefef;
32
+ }
33
+
34
+ &.is-invalid ~ .c-file-drag-input__label {
35
+ border-color: var(--bs-danger);
36
+ }
37
+
38
+ &:disabled {
39
+ opacity: 0;
40
+ cursor: no-drop;
41
+
42
+ + label {
43
+ background-color: #eee;
44
+ color: #999;
45
+ }
46
+
47
+ + label button {
48
+ display: none;
49
+ }
50
+ }
51
+ }
52
+
53
+ &__label {
54
+ position: absolute;
55
+ top: 0;
56
+ bottom: 0;
57
+ left: 0;
58
+ right: 0;
59
+ z-index: 1;
60
+ padding: .375rem .75rem;
61
+ color: #495057;
62
+ background-color: #fff;
63
+ border: 1px solid var(--bs-gray-400);
64
+ border-radius: .25rem;
65
+ height: 100%;
66
+ text-align: center;
67
+ display: flex;
68
+ flex-direction: column;
69
+ align-items: center;
70
+ justify-content: center;
71
+ gap: .5rem;
72
+ transition: all .3s;
73
+ cursor: pointer;
74
+
75
+ > span {
76
+ display: inline-block;
77
+ width: 100%;
78
+ }
79
+ }
80
+
81
+ label::after {
82
+ content: none !important;
83
+ }
84
+
85
+ &__button {
86
+ //font-size: .85rem;
87
+ //padding: .25rem .5rem;
88
+
89
+ //--bs-btn-border-color: var(--bs-secondary);
90
+ //--bs-btn-color: var(--bs-secondary);
91
+ }
92
+ }
93
+
94
+ .c-file-drag-preview {
95
+ .c-file-drag-preview__delete {
96
+ --bs-link-color-rgb: var(--bs-dark-rgb);
97
+
98
+ &.active {
99
+ --bs-link-color-rgb: var(--bs-primary-rgb);
100
+ }
101
+ }
102
+ }
@@ -0,0 +1,88 @@
1
+ // Part of starter file.
2
+
3
+ .c-sid-default {
4
+ &__left-col {
5
+ width: 30%;
6
+ margin-right: 15px;
7
+ justify-content: center;
8
+ }
9
+
10
+ &__left-col img {
11
+ max-height: 250px;
12
+ }
13
+
14
+ &__right-col {
15
+ overflow: hidden;
16
+ }
17
+
18
+ &__dragarea {
19
+ font-weight: bold;
20
+ text-align: center;
21
+ padding: 9% 0;
22
+ color: #ccc;
23
+ border: 2px dashed #ccc;
24
+ border-radius: 7px;
25
+ cursor: default;
26
+
27
+ &.hover {
28
+ color: #333;
29
+ border-color: #333;
30
+ background-color: #f9f9f9;
31
+ }
32
+ }
33
+
34
+ &__img-loader {
35
+ display: flex;
36
+ justify-content: center;
37
+ align-items: center;
38
+ width: 100%;
39
+ height: 180px;
40
+ }
41
+
42
+ &__size-info {
43
+ margin-top: 5px;
44
+ font-size: 13px;
45
+ }
46
+
47
+ &__remove {
48
+ margin-left: 5px;
49
+ }
50
+
51
+ &__modal .btn {
52
+ position: relative;
53
+ }
54
+ }
55
+
56
+ .c-sid-modal {
57
+ .modal-body {
58
+ position: relative;
59
+ }
60
+
61
+ &__content {
62
+ position: relative;
63
+ z-index: 3;
64
+ }
65
+
66
+ &__loading {
67
+ position: absolute;
68
+ left: 0;
69
+ right: 0;
70
+ top: 0;
71
+ bottom: 0;
72
+ display: flex;
73
+ justify-content: center;
74
+ align-items: center;
75
+ z-index: 1;
76
+ }
77
+
78
+ &__img-container {
79
+
80
+ }
81
+ }
82
+
83
+ // A workaround for form validation
84
+ .is-invalid[uni-field-validate] {
85
+ > uni-sid ~ [data-field-error] {
86
+ display: block;
87
+ }
88
+ }
@@ -0,0 +1,160 @@
1
+ // Part of ke file.
2
+
3
+ $img-width: 155px !default;
4
+ $img-height: 155px !default;
5
+
6
+ .vue-drag-uploader {
7
+ display: flex;
8
+ flex-wrap: wrap;
9
+ padding: 15px 5px 5px 15px;
10
+ border: 1px dotted #999;
11
+ border-radius: 4px;
12
+ cursor: pointer;
13
+ color: #999;
14
+ min-height: 250px;
15
+
16
+ &--readonly {
17
+ border: 1px solid #999;
18
+ }
19
+
20
+ &--ondrag {
21
+ border: 1px dotted #666;
22
+ background-color: rgba(0, 0, 0, .05);
23
+ color: #666;
24
+ }
25
+
26
+ &__draggable-wrapper,
27
+ &__transition-wrapper {
28
+ display: flex;
29
+ flex-wrap: wrap;
30
+ }
31
+
32
+ &__item {
33
+ width: $img-width;
34
+ height: $img-height;
35
+ border: 1px solid rgba(0, 0, 0, .25);
36
+ border-radius: 3px;
37
+ cursor: pointer;
38
+ margin-right: 15px;
39
+ margin-bottom: 15px;
40
+ }
41
+
42
+ .add-button {
43
+ display: flex;
44
+ align-items: center;
45
+ text-align: center;
46
+ transition: background-color .5s;
47
+
48
+ &:hover {
49
+ background-color: rgba(0, 0, 0, .05);
50
+ }
51
+
52
+ &__body {
53
+ margin: 0 auto;
54
+ }
55
+
56
+ &__icon {
57
+ margin-bottom: 10px;
58
+ }
59
+
60
+ &__text {
61
+ font-size: 14px;
62
+ }
63
+ }
64
+
65
+ .preview-img {
66
+ position: relative;
67
+ cursor: pointer;
68
+ padding: 1px;
69
+
70
+ > * {
71
+ position: absolute;
72
+ }
73
+
74
+ &:hover {
75
+ .error-message {
76
+ &__message {
77
+ display: block;
78
+ padding: 10px;
79
+ }
80
+ }
81
+ }
82
+
83
+ &__body {
84
+ left: 0;
85
+ top: 0;
86
+ width: 100%;
87
+ height: 100%;
88
+ background: transparent no-repeat center center;
89
+ background-size: cover;
90
+ }
91
+
92
+ &__title {
93
+ margin: 0 auto;
94
+ }
95
+
96
+ &__overlay {
97
+ display: flex;
98
+ align-items: center;
99
+ left: 0;
100
+ top: 0;
101
+ bottom: 0;
102
+ right: 0;
103
+ background-color: rgba(0, 0, 0, .35);
104
+ opacity: 0;
105
+ transition: opacity .5s;
106
+
107
+ &:hover {
108
+ opacity: 1;
109
+ }
110
+ }
111
+
112
+ &__remove-icon {
113
+ position: absolute;
114
+ top: 5px;
115
+ right: 5px;
116
+ color: white;
117
+ opacity: .75;
118
+ transition: opacity .5s;
119
+
120
+ &:hover {
121
+ opacity: 1;
122
+ }
123
+ }
124
+
125
+ &__progress {
126
+ height: 5px;
127
+ background-color: rgba(255, 255, 255, .8);
128
+ width: 100%;
129
+ bottom: 0;
130
+ left: 0;
131
+ }
132
+
133
+ &__progress-bar {
134
+ background-color: var(--bs-primary, #007bff);
135
+ width: 0;
136
+ height: 100%;
137
+ }
138
+ }
139
+
140
+ .error-message {
141
+ width: 100%;
142
+ background-color: #dc3545;
143
+ color: white;
144
+ font-size: 14px;
145
+ word-break: break-all;
146
+ top: calc(100% - 26px);
147
+ min-height: 26px;
148
+
149
+ &__notice {
150
+ display: inline-block;
151
+ padding: 3px;
152
+ text-align: center;
153
+ width: 100%;
154
+ }
155
+
156
+ &__message {
157
+ display: none;
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,156 @@
1
+ /* The switch - the box around the slider */
2
+ // @see https://www.w3schools.com/howto/howto_css_switch.asp
3
+
4
+ $colors: 'primary', 'secondary', 'success', 'info', 'danger', 'warning', 'dark', 'light';
5
+
6
+ .unicorn-switch {
7
+ position: relative;
8
+ display: inline-block;
9
+ width: 60px;
10
+ height: 34px;
11
+ margin-bottom: 0;
12
+
13
+ /* Hide default HTML checkbox */
14
+ input {
15
+ width: 0;
16
+ height: 0;
17
+ position: relative;
18
+ left: 30px;
19
+
20
+ &:focus {
21
+ box-shadow: none !important;
22
+ }
23
+ }
24
+
25
+ /* The slider */
26
+ .switch-slider {
27
+ position: absolute;
28
+ cursor: pointer;
29
+ top: 0;
30
+ left: 0;
31
+ right: 0;
32
+ bottom: 0;
33
+ -webkit-transition: .4s;
34
+ transition: .4s;
35
+
36
+ // BS variables
37
+ background-color: var(--bs-btn-bg);
38
+
39
+ &:hover {
40
+ background-color: var(--bs-btn-hover-bg);
41
+ }
42
+
43
+ @each $color in $colors {
44
+ &.btn-#{$color} {
45
+ // BS variables
46
+ background-color: var(--bs-btn-bg, var(--bs-#{$color}, var(--#{$color})));
47
+
48
+ &:hover {
49
+ background-color: var(--bs-btn-hover-bg, var(--bs-#{$color}, var(--#{$color})));
50
+ }
51
+ }
52
+ }
53
+ }
54
+
55
+ input:focus + .switch-slider {
56
+ box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
57
+ }
58
+
59
+ .switch-slider.bg-default {
60
+ background-color: #ccc;
61
+ }
62
+
63
+ .switch-slider:before {
64
+ position: absolute;
65
+ content: "";
66
+ height: 26px;
67
+ width: 26px;
68
+ left: 4px;
69
+ bottom: 4px;
70
+ background-color: white;
71
+ -webkit-transition: .4s;
72
+ transition: .25s;
73
+ }
74
+
75
+ input:checked + .switch-slider:before {
76
+ transform: translateX(26px);
77
+ }
78
+
79
+ input:not(:checked) + .switch-slider {
80
+ background-color: #ccc !important;
81
+
82
+ &:hover {
83
+ background-color: #aaa !important;
84
+ }
85
+ }
86
+
87
+ input[disabled] + .switch-slider,
88
+ input[readonly] + .switch-slider {
89
+ opacity: .65;
90
+ cursor: not-allowed;
91
+ pointer-events: none;
92
+ }
93
+
94
+ /* Rounded sliders */
95
+ .switch-slider.slider-round {
96
+ border-radius: .25rem;
97
+ }
98
+
99
+ .switch-slider.slider-round:before {
100
+ border-radius: .18rem;
101
+ }
102
+
103
+ // Circle sliders
104
+ .switch-slider.slider-circle {
105
+ border-radius: 50px;
106
+ }
107
+
108
+ .switch-slider.slider-circle:before {
109
+ border-radius: 100px;
110
+ }
111
+
112
+ // Size
113
+ &.switch-sm {
114
+ width: 46px;
115
+ height: 24px;
116
+
117
+ .switch-slider:before {
118
+ height: 18px;
119
+ width: 18px;
120
+ left: 3px;
121
+ bottom: 3px;
122
+ }
123
+
124
+ input:checked + .switch-slider:before {
125
+ transform: translateX(22px);
126
+ }
127
+
128
+ //.switch-slider.slider-round {
129
+ // border-radius: .2rem;
130
+ //}
131
+ //
132
+ //.switch-slider.slider-round:before {
133
+ // border-radius: .15rem;
134
+ //}
135
+ }
136
+
137
+ &.switch-lg {
138
+ width: 100px;
139
+ height: 34px;
140
+
141
+ .switch-slider:before {
142
+ height: 26px;
143
+ width: 45px;
144
+ left: 4px;
145
+ bottom: 4px;
146
+ }
147
+
148
+ input:checked + .switch-slider:before {
149
+ transform: translateX(47px);
150
+ }
151
+ }
152
+ }
153
+
154
+ .has-invalid .unicorn-switch .switch-slider {
155
+ border: 1px solid #dc3545;
156
+ }
package/src/app.ts ADDED
@@ -0,0 +1,128 @@
1
+
2
+ import { Mixin } from 'ts-mixer';
3
+ import { data } from './data';
4
+ import { EventAwareInterface, EventMixin } from './events';
5
+ import { domready } from './service';
6
+ import { Constructor, UnicornPlugin } from './types';
7
+
8
+ export type InjectionKey<T = any> = string | symbol | Constructor<T>;
9
+
10
+ export interface UnicornApp extends EventAwareInterface {}
11
+
12
+ export class UnicornApp extends Mixin(EventMixin) implements EventAwareInterface {
13
+ registry = new Map();
14
+ plugins = new Map();
15
+ // _listeners = {};
16
+ waits: Promise<any>[] = [];
17
+ options: Record<string, any>;
18
+ defaultOptions: Record<string, any> = {};
19
+ domready = domready;
20
+ data = data;
21
+
22
+ constructor(options = {}) {
23
+ super();
24
+ this.options = Object.assign({}, this.defaultOptions, options);
25
+
26
+ // Wait dom ready
27
+ if (typeof document !== 'undefined') {
28
+ this.wait((resolve: Function) => {
29
+ document.addEventListener('DOMContentLoaded', () => resolve());
30
+ });
31
+
32
+ // Ready
33
+ document.addEventListener('DOMContentLoaded', () => {
34
+ this.completed().then(() => this.trigger('loaded'));
35
+ });
36
+ }
37
+ }
38
+
39
+ use(plugin: UnicornPlugin, options: Record<string, any> = {}) {
40
+ if (Array.isArray(plugin)) {
41
+ plugin.forEach(p => this.use(p));
42
+ return this;
43
+ }
44
+
45
+ // if (plugin.is === undefined) {
46
+ // throw new Error(`Plugin: ${plugin.name} must instance of : ${Plugin.name}`);
47
+ // }
48
+
49
+ plugin?.install?.(this, options);
50
+
51
+ this.trigger('plugin.installed', plugin);
52
+
53
+ this.plugins.set(plugin, plugin);
54
+
55
+ return this;
56
+ }
57
+
58
+ detach(plugin: any) {
59
+ if (plugin.uninstall) {
60
+ plugin.uninstall(this);
61
+ }
62
+
63
+ this.trigger('plugin.uninstalled', plugin);
64
+
65
+ return this;
66
+ }
67
+
68
+ inject<T>(id: InjectionKey<T>): T;
69
+ inject<T>(id: InjectionKey<T>, def: T): T;
70
+ inject<T>(id: InjectionKey<T>, def?: T): T;
71
+ inject<T>(id: InjectionKey<T>, def?: T): T | undefined {
72
+ if (!typeof this.registry.has(id)) {
73
+ if (def !== undefined) {
74
+ return def;
75
+ }
76
+
77
+ throw new Error(`Injectable: ${(id as any).name} not found.`);
78
+ }
79
+
80
+ return this.registry.get(id);
81
+ }
82
+
83
+ provide<T>(id: InjectionKey<T>, value: any) {
84
+ this.registry.set(id, value);
85
+
86
+ return this;
87
+ }
88
+
89
+ // trigger(event, ...args) {
90
+ // return this.tap(super.trigger(event, ...args), () => {
91
+ // if (this.data('windwalker.debug')) {
92
+ // console.debug(`[Unicorn Event] ${event}`, args, this.listeners(event));
93
+ // }
94
+ // });
95
+ // }
96
+
97
+ wait(callback: Function): Promise<any> {
98
+ const p = new Promise((resolve, reject) => {
99
+ const promise = callback(resolve, reject);
100
+
101
+ if (promise && 'then' in promise) {
102
+ promise.then(resolve).catch(reject);
103
+ }
104
+ });
105
+
106
+ this.waits.push(p);
107
+
108
+ return p;
109
+ }
110
+
111
+ completed(): Promise<any[]> {
112
+ const promise = Promise.all(this.waits);
113
+
114
+ this.waits = [];
115
+
116
+ return promise;
117
+ }
118
+
119
+ macro(name: string, callback: Function) {
120
+ if ((this as any)[name]) {
121
+ throw new Error(`Macro: ${name} already exists.`);
122
+ }
123
+
124
+ (this as any)[name] = callback;
125
+
126
+ return this;
127
+ }
128
+ }