2022.09 - 2022.11
협업툴의 대표 기능들을 위젯의 형태로 하나의 화면에서 볼 수 있게 만든 프로젝트
<aside> <img src="/icons/arrow-northeast_gray.svg" alt="/icons/arrow-northeast_gray.svg" width="40px" /> 보러가기
</aside>
export default class Widget {
$target;
$props = {
isInit: true, //페이징 및 렌더링 초기화 여부
isDataDrivenRendering: true, //무조건 다시 데이터를 가져와서 그려야하는지 여부
hasRenderedOnScreen: false, //이미 동일한 페이지가 화면에 그려진적이 이미있는지 여부
};
constructor($target, $props = {}) {
if ($target instanceof jQuery) {
this.$target = $target[0];
if ($target.length > 1) console.warn('Multi Elements!');
} else {
this.$target = $target;
}
if ($props) this.$props = $props;
this.setup();
}
async setup(props) {
if (!this.$target) return;
//1. 옵저빙 체크
this.dashboardObserver();
if (!this.isIntersecting) {
if(!ViewChanger.isPage('dashboard')) await this.deleteIndexedDB();
return;
}
//2. 초기화 여부 체크 (isInit)
if (props?.isInit) await this.deleteIndexedDB();
//3. 페이징 체크
if (!this.$props.pageData || props?.isInit) {
this.$props.pageData = {...constants.pageData()}
this.$props.isInit = false;
} else {
this.$props.pageData = this.$props.pageData || {...constants.pageData()};
}
//4. 렌더링 필요여부 체크
if (props) this.$props = {...this.$props, ...props};
if (props && !props.isDataDrivenRendering && this.$props.hasRenderedOnScreen) {
/**
* 1) 무조건 다시 데이터를 가져와서 그려야하는 경우는 넘어감. (isDataDrivenRendering = true)
* 2) 그려진적이 이미있으면 중복으로 판단하여 리턴한다. (hasRenderedOnScreen = true)
*/
return;
}
//5. 스크롤이 아닌데 setup이 호출되는 케이스 방어
if(this.$props.pageData.PG_NO > 1 && !props?.isScrolling){
/*
* 스크롤을 시도하지 않았는데 페이지가 2 페이지 이상이라면,
* 중복 호출로 판단하여 리턴한다. (isScrolling = true)
* */
return;
}
//5. 데이터 호출
if(this.$props.pageData.NEXT_YN === 'N' //마지막 페이지거나
|| !await this.getData(props) //데이터 조회 대상인데 조회가 제대로 되지 않았거나
|| !await this.setTitle() //타이틀 조회 대상인데 조회가 제대로 되지 않았거나
) return;
//6. 정상 호출이면 렌더
this.render();
this.setEvent();
this.mounted();
};
/**
* @description
* 1) 프로젝트 단위로 생성되는 위젯인 경우 : false 면 권한이 없다 판단하여 렌더링하지 않음.
* 2) 프로젝트 단위가 아닌 위젯의 경우 : 항상 true 로 반환함.
*/
async setTitle() {
if (this.$props.project_binding) {
const dat1 = await DashboardApi.getApi('getProjectInfo', ({
USER_ID: _USER_ID,
PTL_ID: _PTL_ID,
CHNL_ID: _CHNL_ID,
COLABO_SRNO: this.$props.COLABO_SRNO,
WIDGET_USER_SRNO: this.$props.WIDGET_USER_SRNO
}))
return !setProjectTtl($(this.$target), {...this.$props, ...dat1});
} else {
return true;
}
}
/**
* @description 1. Input 데이터를 정의한 뒤 checkDataVersion을 실행시킴.
*/
getData() {
};
/**
* @description 2. indexedDB의 데이터 버전을 체크하여 분기함.
*/
async checkDataVersion(inputData) {
const indexedName = `${this.$props.API_KEY}_${this.$props.WIDGET_USER_SRNO}`;
const localInputValue = Mutil.n2v(Mutil.getLocalValue(indexedName, 'inputData'));
const localVersionValue = Number(Mutil.getLocalValue(indexedName, 'version'));
const isSameInput = localInputValue !== "" && JSON.stringify(localInputValue) === JSON.stringify(inputData);
let data = await readRecords({
DBName: indexedName ,
storeName: indexedName ,
key: this.$props.keySrno
});
if (isSameInput && data.length > 0) {
Mutil.setLocalValue(indexedName, 'version', localVersionValue + 1)
this.$props.data = data;
} else {
data = await this.getDBData(inputData);
data = data.recordData;
if(!data) return true;
Mutil.setLocalJson(indexedName, {'version': 1, inputData})
if(this.$props.pageData.PG_NO === 1){
await deleteStore({
DBName: indexedName ,
storeName: indexedName ,
});
this.$props.data = data;
} else {
this.$props.data = [...this.$props.data, ...data];
}
if(data.length > 0){
await createRecord({
DBName: indexedName ,
storeName: indexedName ,
record: data,
key: this.$props.keySrno,
pageData: {...this.$props.pageData}
});
}
}
return this.$props.data;
}
/**
* @description 3. 실제 데이터베이스에서 값을 가져오는 동작.
*/
getDBData() {
}
template() {
return '';
}
render() {
}
mounted() {
this.$props.hasRenderedOnScreen = true;
}
setEvent() {
}
dashboardObserver() {
if (this.io) return
this.io = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.intersectionRatio > 0) { // 관찰 대상이 viewport 안에 들어온 경우
this.isIntersecting = true;
this.setup();
} else if (!entry.isIntersecting) { // 관찰 대상의 교차 상태가 false 일 (보이지 않는) 경우
this.isIntersecting = false;
}
})
}, {threshold: 0.3})
// 관찰할 대상을 선언하고, 해당 속성을 관찰시킴.
this.io.observe(this.$target);
}
async deleteIndexedDB() {
Mutil.setLocalJson(`${this.$props.API_KEY}_${this.$props.WIDGET_USER_SRNO}`, {})
await deleteStore({
DBName: `${this.$props.API_KEY}_${this.$props.WIDGET_USER_SRNO}`,
storeName: `${this.$props.API_KEY}_${this.$props.WIDGET_USER_SRNO}`,
});
}
}
//기본이 되는 위젯 클래스.
export class ProjectListWidget extends Widget {
async setup(props) {
return super.setup(props);
}
async getData() {
const inputData = {
...this.$props.pageData,
COLABO_FLD_KIND: "1",
COLABO_FLD_SRNO: Number(this.$props.FIL_END_DT_GUBUN) === 2 ? '3' : '11',
MODE: this.$props.FIL_END_DT_GUBUN ? TYPE[this.$props.FIL_END_DT_GUBUN - 1] : TYPE[0],
}
return await super.checkDataVersion(inputData)
}
async getDBData(inputData) {
const data = await DashboardApi.getApi(this.$props.API_KEY, inputData)
this.$props.pageData.NEXT_YN = u.n2v(data.NEXT_YN, "N");
return {recordData: data[this.$props.RECORD]};
}
async render() {
$(this.$target).empty();
$(this.$target).drawListHelper({
pageData: this.$props.pageData,
nextYn: this.$props.pageData.NEXT_YN,
records: this.$props.data,
noDataHtml: getNoData(Number(this.$props.FIL_END_DT_GUBUN) === 2 ? 'favorite' : 'myPrj'),
callback: {
scroll: () => {
this.setup.bind(this)({
isDataDrivenRendering: true,
isScrolling : true,
})
},
item: getProjectsHtmByRecord,
final: this.mounted.bind(this)
}
})
}
mounted() {
super.mounted();
const $t = $(this.$target);
if ($t.find('.js-new-prj').length === 0
&& this.$props.data.length > 0) {
$t.prepend(`<li class="js-new-prj widget-default">
<span class="prj-icon-box"></span>
<span class="prj-title">${i18next.t(common.createProject)}</span>
</li>`)
}
$t.find('.widget-article-list').removeClass('dash-pending')
$t.find('.dash-num-pending').remove()
}
setEvent() {
const $w = $(this.$target).closest('.widget-my-prj');
addFilterEvent.bind(this)({$w, filterGb: "my_prj_filter"})
$w.off('click').on('click', (e) => addProjectListClickEvent(e, this));
}
}