-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.js
169 lines (156 loc) · 5.33 KB
/
book.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Ext.require([
'BookWindow'
]);
var book = Ext.define('Books', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [{name: 'id', type: 'int'}, 'name', 'desc', 'state'],
});
var statuses = Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
data: [
{id: 0, name: 'new'},
{id: 1, name: 'in work'},
{id: 2, name: 'published'},
{id: 3, name: 'archive'}
]
});
Ext.onReady(function () {
var selRec, bookArr = [];
// create in-memory Data Store
var memStore = Ext.create('Ext.data.Store', {
autoload: true,
id: 'memBookStore',
model: 'Books',
pageSize: 5,
proxy: {
enablePaging: true,
type: 'memory',
},
data: bookArr,
remoteSort: true,
remoteFilter: true
});
var locStorage = localStorage.getItem('book-shelf');
if (locStorage) {
var ls = JSON.parse(locStorage);
ls.forEach(t => {
bookArr.push([...t]);
});
}
if (1 && !bookArr.length) {
for (i = 0; i < 20; i++) {
rec = bookArr.push([i + 1,
'Book' + (i + 1),
'Description' + (i + 1),
statuses.getAt(i % 4).data.name
]);
}
}
memStore.reload();
// create the grid
var grid = Ext.create('Ext.grid.Panel', {
requires: [],
bufferedRenderer: false,
store: memStore,
plugins: {
ptype: 'gridfilters',
},
tbar: {
xtype: 'toolbar',
dock: 'top',
items: [
{
xtype: 'button',
text: 'Add',
handler: function () {
Ext.create('BookWindow', {
title: 'Add book', arrMem: bookArr,
memStore: memStore, editMode: false
}).show();
}
},
{
xtype: 'button',
text: 'Edit',
handler: function () {
if (selRec) {
var id = bookArr.findIndex(el => el[0] === selRec.id);
if (~id) {
Ext.create('BookWindow', {
title: 'Edit book info',
arrMem: bookArr,
memStore: memStore,
editMode: true,
bookId: id
}).show();
}
}
}
},
{
xtype: 'button', text: 'Delete',
handler: function () {
if (selRec) {
var id = bookArr.findIndex(el => el[0] === selRec.id);
if (~id) {
bookArr.splice(id, 1);
memStore.reload();
localStorage.setItem('book-shelf', JSON.stringify(bookArr));
var detailPanel = Ext.getCmp('detailPanel');
detailPanel.update('Please select a book to see additional details.');
}
}
}
}
]
},
columns: [
{text: 'No', xtype: 'rownumberer', width: 30},
{text: 'id', dataIndex: 'id', width: 40, hidden: true},
{text: "Name", width: 150, dataIndex: 'name', sortable: true, filter: 'string'},
// {text: "Description", width: 120, dataIndex: 'desc', sortable: true, filter: 'string'},
{text: "Status", width: 50, dataIndex: 'state', sortable: true, filter: 'string'},
],
bbar: {
xtype: 'pagingtoolbar',
dock: 'bottom',
displayInfo: true,
emptyMsg: 'No pages to display'
},
forceFit: true,
split: true,
region: 'north',
});
// define a template to use for the detail view
var bookTplMarkup = [
'<b>Name:</b> {name}<br/>',
'<b>Description:</b> {desc}<br/>',
'<b>Status:</b> {state}<br/>'
];
var bookTpl = Ext.create('Ext.Template', bookTplMarkup);
Ext.create('Ext.Panel', {
renderTo: 'book-shelf',
frame: true,
title: 'Book List',
layout: 'anchor',
items: [
grid,
{
id: 'detailPanel',
region: 'center',
bodyPadding: 7,
bodyStyle: "background: #ffffff;",
html: 'Please select a book to see additional details.'
},
],
});
grid.getSelectionModel().on('selectionchange', function (sm, selectedRecord) {
if (selectedRecord.length) {
selRec = selectedRecord[0];
var detailPanel = Ext.getCmp('detailPanel');
detailPanel.update(bookTpl.apply(selectedRecord[0].data));
} else
selRec = null;
});
});