-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage1.js
84 lines (70 loc) · 2.36 KB
/
page1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {PolymerElement, html} from '@polymer/polymer/polymer-element.js';
import '@polymer/paper-input/paper-input.js';
import '@polymer/paper-button/paper-button.js';
import '@polymer/paper-radio-button/paper-radio-button.js';
import '@polymer/paper-radio-group/paper-radio-group.js';
import './shared-styles.js';
import './list.js';
class Page1 extends PolymerElement {
static get template() {
return html`
<style include="shared-styles">
:host { display: block; padding: 10px; }
</style>
<div class="box">
<img src="[[imageUrl]]" />
<paper-input always-float-label label="Nome" name="name" value="{{name}}"></paper-input>
<paper-input always-float-label label="Imagem" name="image-url" value="{{imageUrl}}"></paper-input>
<paper-button raised on-click="_save">Atualizar</paper-button>
<paper-button raised on-click="_create">Criar</paper-button>
</div>
<nc-list selected="{{selected}}" db="[[db]]" name="{{name}}" image-url="{{imageUrl}}" selected-id="{{selectedId}}"></nc-list>
`;
}
static get properties() {
return {
name: String,
imageUrl: String,
selectedId: { type:String, notify: true },
selected: { type:Object, notify: true, observer:'_onSelected' },
db: { type: Array, notify: true },
};
}
ready() {
if( !this.selected ) {
this.selected = this.db[this.db.length - 1];
}
this.name = this.selected.name;
this.imageUrl = this.selected.imageUrl;
this.selectedId = this.selected.id;
super.ready();
}
_onSelected(){
this.name = this.selected.name;
this.imageUrl = this.selected.imageUrl;
}
_fixPersonal(){
if(!this.name ) {
this.name = "Sem nome";
}
if( !this.imageUrl ) {
this.imageUrl = 'https://place-hold.it/400x200/ccc';
}
}
_save(){
this._fixPersonal();
let i = this.db.indexOf( this.selected );
this.set(`selected.name`, this.name );
this.set(`selected.imageUrl` , this.imageUrl );
this.notifyPath(`db.${i}.name` , this.name );
this.notifyPath(`db.${i}.imageUrl` , this.imageUrl );
}
_create(){
this._fixPersonal();
const obj = { id: this.db.length, name: this.name, imageUrl: this.imageUrl };
this.unshift('db', obj);
this.selected = obj;
this.selectedId = obj.id;
}
}
window.customElements.define('nc-page1', Page1);