(function () {
const TAG = "spz-custom-discount-placement";
class SpzCustomDiscountPlacement extends SPZ.BaseElement {
constructor(element) {
super(element);
this.placementTypeMap = {
PTT_POPUP: "PTT_POPUP",
PTT_BANNER: "PTT_BANNER"
};
this.placementTemplateMap = {
PT_POPUP_BUY_X_GET_Y: "PT_POPUP_BUY_X_GET_Y",
PT_BANNER_BUY_X_GET_Y: "PT_BANNER_BUY_X_GET_Y"
};
this.progressMap = {
PROGRESS_ONGOING: "PROGRESS_ONGOING",
PROGRESS_NOT_STARTED: "PROGRESS_NOT_STARTED",
PROGRESS_FINISHED: "PROGRESS_FINISHED",
PROGRESS_PAUSED: "PROGRESS_PAUSED"
};
}
static deferredMount() {
return false;
}
buildCallback = () => {
this.action_ = SPZServices.actionServiceForDoc(this.element);
this.templates_ = SPZServices.templatesForDoc(this.element);
this.xhr_ = SPZServices.xhrFor(this.win);
this.setupAction_();
this.viewport_ = this.getViewport();
}
mountCallback = () => {
this.handlePlacement();
}
// 上报数据
reportData = async(data) => {
const reqBody = {
placement_id: data.placement_id,
event: data.event
}
const url = `/api/storefront/promotion/placement/data/report`;
this.xhr_.fetchJson(url, {
method: "post",
body: reqBody
}).then((res)=>{
// todo ...
})
}
// 资源位数据获取
getplacementList = async() => {
const reqBody = {
page_id: window.SHOPLAZZA.meta.page.template_type
}
const url = `/api/storefront/promotion/placement/list`
const data = await this.xhr_.fetchJson(url, {
method: "post",
body: reqBody
})
return data;
}
handlePlacement = async() => {
const res = await this.getplacementList();
const BXGYPopupList = res.list.filter(item => (item.placement_type == this.placementTypeMap.PTT_POPUP && item.placement_template == this.placementTemplateMap.PT_POPUP_BUY_X_GET_Y));
if(BXGYPopupList.length) {
const $spz_custom_popup_bxgy = document.querySelector('#spz_custom_popup_bxgy');
SPZ.whenApiDefined($spz_custom_popup_bxgy).then((api)=> {
api.doRender_({popupList: BXGYPopupList});
})
}
}
doRender_ = (data) => {
const renderData = data || {};
return this.templates_
.findAndRenderTemplate(this.element, renderData)
.then((el) => {
const children = this.element.querySelector('*:not(template)');
children && SPZCore.Dom.removeElement(children);
this.element.appendChild(el);
})
}
setupAction_ = () => {
this.registerAction('handleTrack', (invocation) => {
const data = invocation.args;
this.reportData(data)
});
}
triggerEvent_(name, data) {
const event = SPZUtils.Event.create(this.win, `${ TAG }.${ name }`, data || {});
this.action_.trigger(this.element, name, event);
}
isLayoutSupported(layout) {
return layout == SPZCore.Layout.CONTAINER;
}
}
SPZ.defineElement(TAG, SpzCustomDiscountPlacement)
})()