cmscore
Attribute
Core functionalities of all cms
Attributes.
CDN Import
Important: Do not manually import this package into your project. All cms
packages dynamically import it only when required, you don't need to do anything.
https://cdn.jsdelivr.net/npm/@finsweet/attributes-cmscore@1/cmscore.js
JavaScript API
Each cms
attribute uses cmscore
under the hood, creating an instance of CMSList
.
All list instances can be accessed from window.fsAttributes.cms.listInstances
, but the safest way to access an attribute's instance is by using the window.fsAttributes.push()
method (see each Attribute's docs for more info).
The CMSList
instance has a few properties and methods that can be used to extend its functionalities. You can also subscribe to specific events.
Each individual Collection Item is stored as a CMSItem
instance, which also has some methods and properties.
CMSList
instance
interface CMSList {
wrapper: HTMLDivElement;
list?: HTMLDivElement | null;
paginationWrapper?: HTMLDivElement | null;
paginationCount?: HTMLDivElement | null;
paginationPrevious?: HTMLAnchorElement | null;
paginationNext?: HTMLAnchorElement | null;
scrollAnchor?: HTMLElement;
itemsCount?: HTMLElement;
visibleCount?: HTMLElement;
initialElement?: HTMLElement | null;
emptyElement?: HTMLElement | null;
emptyState: boolean;
loader?: HTMLElement;
totalPages: number;
paginationActive: boolean;
currentPage?: number;
pagesQuery?: string;
showPaginationQuery: boolean;
items: CMSItem[];
originalItemsOrder: CMSItem[];
itemsPerPage: number;
originalItemsPerPage: number;
staticItems: CMSItem[];
validItems: CMSItem[];
restartWebflow: boolean;
restartIx: boolean;
restartCommerce: boolean;
restartLightbox: boolean;
restartSliders: boolean;
restartTabs: boolean;
addItems(itemElements: HTMLElement[], method?: 'unshift' | 'push'): Promise<void>;
addStaticItems(
itemsData: Array<{ itemElement: HTMLDivElement; targetIndex: number; interactive: boolean }>
): Promise<void>;
restoreItemsOrder(): void;
clearItems(removeElements?: boolean): Promise<void>;
renderItems(animateItems?: boolean, animateList?: boolean): Promise<CMSItem[]>;
displayElement(
elementKey:
| 'wrapper'
| 'list'
| 'emptyElement'
| 'initialElement'
| 'paginationNext'
| 'paginationPrevious'
| 'loader',
show?: boolean,
animate?: boolean
): Promise<void>;
addEmptyElement(element: HTMLElement): void;
addLoader(element: HTMLElement): void;
addItemsCount(element: HTMLElement): void;
addVisibleCount(
totalElement?: HTMLElement | null,
fromElement?: HTMLElement | null,
toElement?: HTMLElement | null
): void;
switchPage(targetPage: number, renderItems?: boolean): Promise<void>;
scrollToAnchor(): void;
getAttribute(attributeKey: string): string | null | undefined;
getInstanceIndex(key: string): number | undefined;
public destroy(): void;
}
CMSList
events
Each CMSList
instance provides some events that the user can subscribe to by using the CMSList.on(eventName, callback)
method.
renderitems
This event is fired every time that any Collection Items are rendered on the page. This includes:
- The user has applied a filter and the list has changed the currently rendered items.
- The user has switched a page and the list has rendered new items.
- The user has sorted the items and the list has rendered the new items order.
- The list has loaded more items and rendered them.
The callback function provides an array of the CMSItem
instances that have been rendered.
Example:
listInstance.on('renderitems', (renderedItems) => {
console.log('The following items have been rendered on the Collection List: ', renderedItems);
});
additems
This event is fired whenever cmsload
adds more items to the CMSList
instance, or the user programatically does it with CMSList.addItems()
.
Adding items to the list doesn't mean that those items have been rendered (thus they are present in the DOM).
cmscore
keeps all items in memory and only renders the ones that are required to. If you want to access the items that are currently present in the DOM, you should use the renderitems
event instead.
The callback function provides an array of the CMSItem
instances that have been added.
Example:
listInstance.on('additems', (addedItems) => {
console.log('The following items have been added to the CMSList memory: ', addedItems);
});
switchpage
This event is fired whenever the user navigates to another page when using cmsload
with pagination
mode.
The callback function provides the new page index. This index is 0-based.
listInstance.on('switchpage', (targetPage) => {
console.log('The user has navigated to the page number ', targetPage);
});
CMSItem
instance
interface CMSItem {
element: HTMLDivElement;
list: HTMLDivElement;
currentIndex?: number;
staticIndex?: number;
href?: string;
props: CMSItemProps;
valid: boolean;
rendering?: Promise<void>;
animating?: Promise<void>;
needsWebflowRestart: boolean;
collectProps({ fieldKey, typeKey, rangeKey }: { fieldKey: string; typeKey?: string; rangeKey?: string }): void;
}
interface CMSItemProps {
[key: string]: {
values: Set<string>;
elements: Map<
string,
{
element: HTMLElement;
originalHTML: string;
}
>;
highlightData?: Map<string, { filterValue?: string; highlightCSSClass: string }>;
type?: string | null;
range?: string | null;
};
}