UNPKG

2.83 kBJavaScriptView Raw
1/*
2Copyright 2013-2015 ASIAL CORPORATION
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16*/
17
18import onsElements from '../ons/elements.js';
19import util from '../ons/util.js';
20import autoStyle from '../ons/autostyle.js';
21import ModifierUtil from '../ons/internal/modifier-util.js';
22import BaseElement from './base/base-element.js';
23import contentReady from '../ons/content-ready.js';
24
25const defaultClassName = 'card';
26const scheme = {
27 '': 'card--*',
28 '.card__title': 'card--*__title',
29 '.card__content': 'card--*__content'
30};
31
32/**
33 * @element ons-card
34 * @category visual
35 * @modifier material
36 * [en]A card with material design.[/en]
37 * [ja]リストの上下のボーダーが無いリストを表示します。[/ja]
38 * @description
39 * [en]
40 * Component to create a card that displays some information.
41 *
42 * The card may be composed by divs with specially prepared classes `title` and/or `content`. You can also add your own content as you please.[/en]
43 * [ja][/ja]
44 * @tutorial vanilla/Reference/card
45 * @example
46 * <ons-card>
47 * <p>Some content</p>
48 * </ons-card>
49 */
50export default class CardElement extends BaseElement {
51
52 /**
53 * @attribute modifier
54 * @type {String}
55 * @description
56 * [en]The appearance of the card.[/en]
57 * [ja]リストの表現を指定します。[/ja]
58 */
59
60 constructor() {
61 super();
62
63 contentReady(this, () => {
64 this._compile();
65 });
66 }
67
68 _compile() {
69 let title, content;
70
71 for (let i = 0; i < this.children.length; i++) {
72 const el = this.children[i];
73
74 if (el.classList.contains('title')) {
75 el.classList.add('card__title');
76 title = el;
77 }
78 else if (el.classList.contains('content')) {
79 el.classList.add('card__content');
80 content = el;
81 }
82 }
83
84 autoStyle.prepare(this);
85 this.classList.add(defaultClassName);
86 ModifierUtil.initModifier(this, scheme);
87 }
88
89 static get observedAttributes() {
90 return ['modifier', 'class'];
91 }
92
93 attributeChangedCallback(name, last, current) {
94 switch (name) {
95 case 'class':
96 util.restoreClass(this, defaultClassName, scheme);
97 break;
98 case 'modifier':
99 ModifierUtil.onModifierChanged(last, current, this, scheme);
100 break;
101 }
102 }
103}
104
105onsElements.Card = CardElement;
106customElements.define('ons-card', CardElement);