@egjs/flicking 4.12.0-beta.1 → 4.12.0-beta.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egjs/flicking",
3
- "version": "4.12.0-beta.1",
3
+ "version": "4.12.0-beta.3",
4
4
  "description": "Everyday 30 million people experience. It's reliable, flexible and extendable carousel.",
5
5
  "main": "dist/flicking.cjs.js",
6
6
  "module": "dist/flicking.esm.js",
@@ -6,7 +6,8 @@
6
6
  import Flicking, { FlickingOptions } from "./Flicking";
7
7
  import { ChangedEvent, MoveEndEvent, MoveEvent } from "./type/event";
8
8
  import { LiteralUnion, ValueOf } from "./type/internal";
9
- import { EVENTS, MOVE_DIRECTION } from "./const/external";
9
+ import { CLASS, EVENTS, MOVE_DIRECTION } from "./const/external";
10
+ import { getDataAttributes, getElement, includes, toArray } from "./utils";
10
11
 
11
12
  /**
12
13
  * @interface
@@ -39,6 +40,7 @@ export interface CrossFlickingOptions {
39
40
  * @property {string | undefined} [panels.html] An `outerHTML` of the panel element<ko>패널 엘리먼트의 `outerHTML`</ko>
40
41
  */
41
42
  export interface VerticalState {
43
+ key: string;
42
44
  start: number;
43
45
  end: number;
44
46
  element: HTMLElement;
@@ -59,11 +61,39 @@ export class CrossFlicking extends Flicking {
59
61
 
60
62
  // Internal State
61
63
  private _verticalState: VerticalState[];
62
-
63
64
  private _moveDirection: LiteralUnion<ValueOf<typeof MOVE_DIRECTION>> | null;
64
65
  private _nextIndex: number;
65
66
 
66
67
  // Components
68
+ /**
69
+ * {@link Control} instance of the Flicking
70
+ * @ko 현재 Flicking에 활성화된 {@link Control} 인스턴스
71
+ * @type {Control}
72
+ * @default SnapControl
73
+ * @readonly
74
+ * @see Control
75
+ * @see SnapControl
76
+ * @see FreeControl
77
+ */
78
+ public get verticalFlicking() {
79
+ return this._verticalFlicking;
80
+ }
81
+
82
+ // Internal States
83
+ /**
84
+ * Whether Flicking's {@link Flicking#init init()} is called.
85
+ * This is `true` when {@link Flicking#init init()} is called, and is `false` after calling {@link Flicking#destroy destroy()}.
86
+ * @ko Flicking의 {@link Flicking#init init()}이 호출되었는지를 나타내는 멤버 변수.
87
+ * 이 값은 {@link Flicking#init init()}이 호출되었으면 `true`로 변하고, {@link Flicking#destroy destroy()}호출 이후에 다시 `false`로 변경됩니다.
88
+ * @type {boolean}
89
+ * @default false
90
+ * @readonly
91
+ */
92
+ public get verticalState() {
93
+ return this._verticalState;
94
+ }
95
+
96
+ // Options Getter
67
97
  /**
68
98
  * Change active panel index on mouse/touch hold while animating.
69
99
  * `index` of the `willChange`/`willRestore` event will be used as new index.
@@ -78,7 +108,6 @@ export class CrossFlicking extends Flicking {
78
108
  }
79
109
 
80
110
  // Options Setter
81
- // UI / LAYOUT
82
111
  // public set align(val: FlickingOptions["align"]) {
83
112
  // this._align = val;
84
113
  // }
@@ -90,7 +119,7 @@ export class CrossFlicking extends Flicking {
90
119
  super(root);
91
120
 
92
121
  // Internal states
93
- this._verticalState = [];
122
+ this._verticalState = this._createVerticalState();
94
123
  this._moveDirection = null;
95
124
  this._nextIndex = 0;
96
125
 
@@ -98,7 +127,7 @@ export class CrossFlicking extends Flicking {
98
127
  this._verticalOptions = verticalOptions;
99
128
 
100
129
  // Create core components
101
- // this._viewport = new Viewport(this, getElement(root));
130
+ this._verticalFlicking = this._createVerticalFlicking();
102
131
  }
103
132
 
104
133
  /**
@@ -110,46 +139,121 @@ export class CrossFlicking extends Flicking {
110
139
  * @return {Promise<void>}
111
140
  */
112
141
  public init(): Promise<void> {
113
- return super.init().then(() => {
114
- // data-index로 분류하기 전에 임시로 모든 children에 대해 vertical flicking으로 해보자.
115
- // camera.children들에 대해 갯수 세기
116
- let verticalPanels = ``;
117
- this._verticalState = this.camera.children.reduce(
118
- (state: VerticalState[], child: HTMLElement) => {
142
+ return super.init().then(() => this._addComponentEvents());
143
+ }
144
+
145
+ private _addComponentEvents(): void {
146
+ this.on(EVENTS.HOLD_START, this._onHorizontalHoldStart);
147
+ this.on(EVENTS.MOVE, this._onHorizontalMove);
148
+ this.on(EVENTS.MOVE_END, this._onHorizontalMoveEnd);
149
+
150
+ this._verticalFlicking.forEach((flicking) => {
151
+ flicking.on(EVENTS.HOLD_START, this._onVerticalHoldStart);
152
+ flicking.on(EVENTS.MOVE, this._onVerticalMove);
153
+ flicking.on(EVENTS.MOVE_END, this._onVerticalMoveEnd);
154
+ flicking.on(EVENTS.CHANGED, this._onVerticalChanged);
155
+ });
156
+ }
157
+
158
+ private _createVerticalState(): VerticalState[] {
159
+ // data-index로 분류하기 전에 임시로 모든 children에 대해 vertical flicking으로 해보자.
160
+ // panels에 data-attributes가 붙어있을 때와 안 붙어있을 때를 다르게 처리
161
+ // 붙어있다면 가상의 viewport들을 index 갯수만큼 만들어줘야 한다
162
+ const cameraEl = this.camera.element;
163
+ const panels = toArray(cameraEl.children) as HTMLElement[];
164
+ let verticalState: VerticalState[];
165
+ let verticalPanels: string = "";
166
+
167
+ // check data attribute exists
168
+ const groupKeys: string[] = [];
169
+ const groupPanels: Record<string, HTMLElement[]> = {};
170
+ const verticalCamera = document.createElement("div");
171
+ verticalCamera.classList.add(CLASS.CAMERA);
172
+
173
+ panels.forEach((panel) => {
174
+ const groupKey = getDataAttributes(panel, "data-flicking-").groupkey;
175
+ if (groupKey && !includes(groupKeys, groupKey)) {
176
+ groupKeys.push(groupKey);
177
+ groupPanels[groupKey] = [panel];
178
+ } else if (groupKey) {
179
+ groupPanels[groupKey].push(panel);
180
+ }
181
+ return groupKey;
182
+ });
183
+
184
+ if (groupKeys.length) {
185
+ panels.forEach(() => this.remove(0));
186
+ verticalState = groupKeys.reduce(
187
+ (state: VerticalState[], key: string) => {
119
188
  const start = state.length ? +state[state.length - 1].end + 1 : 0;
120
- verticalPanels += child.children[0].innerHTML;
189
+ const element = groupPanels[key].reduce(
190
+ (el: HTMLElement, panel: HTMLElement) => {
191
+ verticalPanels += panel.outerHTML;
192
+ el.appendChild(panel);
193
+ return el;
194
+ },
195
+ document.createElement("div")
196
+ );
121
197
  return [
122
198
  ...state,
123
199
  {
200
+ key,
124
201
  start,
125
- end: start + child.children[0].children.length - 1,
126
- element: child,
202
+ end: start + groupPanels[key].length - 1,
203
+ element: element,
127
204
  },
128
205
  ];
129
206
  },
130
207
  []
131
208
  );
132
- this.camera.children.forEach((child) => {
133
- child.children[0].innerHTML = verticalPanels;
209
+
210
+ verticalCamera.innerHTML = verticalPanels;
211
+ cameraEl.innerHTML = "";
212
+ groupKeys.forEach(() => {
213
+ const panel = document.createElement("div");
214
+ panel.classList.add(CLASS.VIEWPORT, CLASS.VERTICAL);
215
+ panel.innerHTML = verticalCamera.outerHTML;
216
+ this.append(panel);
134
217
  });
135
- this._verticalFlicking = this.camera.children.map((child, i) => {
136
- return new Flicking(child, {
137
- ...this.verticalOptions,
138
- horizontal: false,
139
- panelsPerView: 1,
140
- defaultIndex: this._verticalState[i].start,
218
+ } else {
219
+ verticalState = panels.reduce(
220
+ (state: VerticalState[], panel: HTMLElement, i: number) => {
221
+ const start = state.length ? +state[state.length - 1].end + 1 : 0;
222
+ verticalPanels += panel.innerHTML;
223
+ return [
224
+ ...state,
225
+ {
226
+ key: i.toString(),
227
+ start,
228
+ end: start + panel.children.length - 1,
229
+ element: panel,
230
+ },
231
+ ];
232
+ },
233
+ []
234
+ );
235
+
236
+ verticalCamera.innerHTML = verticalPanels;
237
+ panels.forEach((panel) => {
238
+ [CLASS.VIEWPORT, CLASS.VERTICAL].forEach((className) => {
239
+ if (!panel.classList.contains(className)) {
240
+ panel.classList.add(className);
241
+ }
141
242
  });
243
+ panel.innerHTML = verticalCamera.outerHTML;
142
244
  });
245
+ }
143
246
 
144
- this.on(EVENTS.HOLD_START, this._onHorizontalHoldStart);
145
- this.on(EVENTS.MOVE, this._onHorizontalMove);
146
- this.on(EVENTS.MOVE_END, this._onHorizontalMoveEnd);
247
+ return verticalState;
248
+ }
147
249
 
148
- this._verticalFlicking.forEach((child) => {
149
- child.on(EVENTS.HOLD_START, this._onVerticalHoldStart);
150
- child.on(EVENTS.MOVE, this._onVerticalMove);
151
- child.on(EVENTS.MOVE_END, this._onVerticalMoveEnd);
152
- child.on(EVENTS.CHANGED, this._onVerticalChanged);
250
+ private _createVerticalFlicking(): Flicking[] {
251
+ return this.camera.children.map((panel, i) => {
252
+ return new Flicking(panel, {
253
+ ...this.verticalOptions,
254
+ horizontal: false,
255
+ panelsPerView: 1,
256
+ defaultIndex: this._verticalState[i].start,
153
257
  });
154
258
  });
155
259
  }
@@ -94,6 +94,8 @@ export const MOVE_TYPE = {
94
94
  } as const;
95
95
 
96
96
  export const CLASS = {
97
+ VIEWPORT: "flicking-viewport",
98
+ CAMERA: "flicking-camera",
97
99
  VERTICAL: "vertical",
98
100
  HIDDEN: "flicking-hidden",
99
101
  DEFAULT_VIRTUAL: "flicking-panel"
package/src/utils.ts CHANGED
@@ -349,3 +349,24 @@ export const setPrototypeOf = Object.setPrototypeOf || ((obj, proto) => {
349
349
  obj.__proto__ = proto;
350
350
  return obj;
351
351
  });
352
+
353
+ export const camelize = (str: string): string => {
354
+ return str.replace(/[\s-_]([a-z])/g, (all, letter) => letter.toUpperCase());
355
+ };
356
+
357
+ export const getDataAttributes = (element: HTMLElement, attributePrefix: string): Record<string, string> => {
358
+ const dataAttributes: Record<string, string> = {};
359
+ const attributes = element.attributes;
360
+ const length = attributes.length;
361
+
362
+ for (let i = 0; i < length; ++i) {
363
+ const attribute = attributes[i];
364
+ const { name, value } = attribute;
365
+ if (name.indexOf(attributePrefix) === -1) {
366
+ continue;
367
+ }
368
+ dataAttributes[camelize(name.replace(attributePrefix, ""))] = value;
369
+ }
370
+
371
+ return dataAttributes;
372
+ };