1
- // App Interface (MVC).
2
-
3
- /*
4
- The model class can be imported for a type driven UI, but this is optional.
5
- The datastore does not use model properties for persistence.
6
- All items are added to an item collection.
7
- The item collection is persisted as a JSON array, regardless of the persistence type selected.
8
- */
1
+ // Bookshelf App Interface (MVC).
2
+
3
+ /************************************************************************************
4
+ * The model class can be imported for a type driven UI, but this is optional.
5
+ * The datastore does not use model properties for persistence.
6
+ * All items are added to an item collection.
7
+ * The item collection is persisted as a JSON array, regardless of the persistence type selected.
8
+ ********************************************************************************** */
9
9
import { Book } from "./book.js" ;
10
10
11
- /*
12
- The ItemStore module is required to manage the collection.
13
- The DatabaseSettings module is required to override the default persistence settings.
14
- See the example implementations below.
15
- */
11
+ /************************************************************************************
12
+ * The ItemStore module is required to manage the collection.
13
+ * The DatabaseSettings module is required to override the default persistence settings.
14
+ * See the example implementations below.
15
+ *********************************************************************************** */
16
16
import { ItemStore } from "./itemStore.js" ;
17
17
18
- /*
19
- Persistence types is an enum of the browser supported persistence methods.
20
- This module can be imported for strongly typed declarations but this is optional.
21
- The persistence types can be passed directly as strings.
22
- */
18
+ /*************************************************************************************
19
+ * Persistence types is an enum of the browser supported persistence methods.
20
+ * This module can be imported for strongly typed declarations but this is optional.
21
+ * The persistence types can be passed directly as strings.
22
+ *********************************************************************************** */
23
23
import { PersistenceTypes } from "./services/dataService.js" ;
24
24
25
- /***************************************************************************************
26
- Use this declaration to create a item store to manage the collection using default settings.
27
- The collection will be in memory only and deleted upon page refresh or when the browser window is closed.
28
- ****************************************************************************************/
25
+ /*************************************************************************************
26
+ * The DatabaseSettings class can be used to override the default persistence settings.
27
+ * Properties such as database name, key field, and persistence type can all be
28
+ * customized using valid options.
29
+ *************************************************************************************/
30
+ import { DatabaseSettings } from "./services/databaseSettings.js" ;
29
31
32
+ /*************************************************************************************
33
+ * Use this declaration to create a item store to manage the collection
34
+ * using default settings.
35
+ * The collection will be in memory only and deleted upon page refresh or
36
+ * when the browser window is closed.
37
+ ************************************************************************************/
30
38
//const bookDepot = new ItemStore();
31
39
32
40
/*************************************************************************************
33
- Use this declaration to create a item store to manage the collection without persistence.
34
- When the usePersistenceStore parameter is set to false,
35
- the collection will be in memory only and deleted upon page refresh or when the browser window is closed.
36
- *************************************************************************************/
37
-
41
+ * Use this declaration to create a item store to manage the collection without persistence.
42
+ * When the usePersistenceStore parameter is set to false,
43
+ * the collection will be in memory only and deleted upon page refresh or
44
+ * when the browser window is closed.
45
+ *************************************************************************************/
38
46
//const bookDepot = new ItemStore("MyBookStore", false);
39
47
40
- /*************************************************************************************
41
- Use this declaration to create an item store to manage the collection with default persistence settings.
42
- This implementation will use cookie storage by default.
43
- The collection will be stored with the browser's cookie until the cookie expires or is deleted.
44
- **************************************************************************************/
45
-
46
- const bookDepot = new ItemStore ( "MyBookStore" , true ) ;
47
-
48
48
/**************************************************************************************
49
- An instace of the DatabaseSettings module is required to override the default settings.
50
- Settings such as the database name, database version, table name, as well as key field can be overridden.
51
- This is also where the persistence type can be changed from cookie storage to another option.
52
- This code is ignored when the ItemStore.usePersistence property is set to false.
53
- ***************************************************************************************/
49
+ * Use this declaration to create an item store to manage the collection with
50
+ * default persistence settings.
51
+ * This implementation will use cookie storage by default.
52
+ * The collection will be stored with the browser's cookie until the cookie expires or
53
+ * is deleted.
54
+ **************************************************************************************/
55
+ const bookDepot = new ItemStore ( "MyBookStore" , true ) ;
54
56
55
- //Initialize the persistence store by passing in database settings.
57
+ /***************************************************************************************
58
+ * An instace of the DatabaseSettings module is required to override the default settings.
59
+ * Settings such as the database name, database version, table name, as well as key field
60
+ * can be overridden.
61
+ * This is also where the persistence type can be changed from cookie storage to another option.
62
+ * This code is ignored when the ItemStore.usePersistence property is set to false.
63
+ ***************************************************************************************/
64
+
65
+ //Initialize the data store by passing in a DatabaseSettings object.
56
66
//The persistence types enum can be used for a strongly typed list of supported types.
57
- import { DatabaseSettings } from "./services/databaseSettings.js" ;
58
67
let databaseSettings = new DatabaseSettings (
59
68
"MyBookStore" ,
60
69
1 ,
@@ -64,12 +73,14 @@ let databaseSettings = new DatabaseSettings(
64
73
) ;
65
74
bookDepot . initializeDataStore ( databaseSettings ) ;
66
75
67
- /**************************************************************************************/
68
-
69
- // new page title
76
+ /**************************************************************************************
77
+ * New page title
78
+ **************************************************************************************/
70
79
const pageTitle = "Javascript - Bookshelf" ;
71
80
72
- // HTML markup to display the book collection.
81
+ /***************************************************************************************
82
+ * HTML markup to display the book collection.
83
+ ***************************************************************************************/
73
84
const markUp =
74
85
"<header>" +
75
86
"<h1>Bookshelf</h1>" +
@@ -102,7 +113,9 @@ const markUp =
102
113
"</div>" +
103
114
"</div>" ;
104
115
105
- // initialize the page.
116
+ /**********************************************************************************
117
+ * initialize the page.
118
+ **********************************************************************************/
106
119
( function ( ) {
107
120
// set page title
108
121
document . title = pageTitle ;
@@ -135,9 +148,9 @@ const markUp =
135
148
resetForm ( ) ;
136
149
} ) ( ) ;
137
150
138
- /*
139
- Clear all form values.
140
- */
151
+ /********************************************************************************
152
+ * Clear all form values.
153
+ ******************************************************************************* */
141
154
function resetForm ( ) {
142
155
// get all inputs
143
156
var inputs = document . querySelectorAll (
@@ -158,9 +171,10 @@ function resetForm() {
158
171
// set focus on the first input
159
172
document . getElementById ( "new-book-title" ) . focus ( ) ;
160
173
}
161
- /*
162
- Update book list display
163
- */
174
+
175
+ /***********************************************************************************
176
+ * Update book list display
177
+ ***********************************************************************************/
164
178
function updateListDisplay ( ) {
165
179
// access the book container
166
180
var bookList = document . getElementById ( "book-list" ) ;
@@ -254,9 +268,9 @@ function updateListDisplay() {
254
268
}
255
269
}
256
270
257
- /*
258
- Save a new book from the form values. This could be pushed back to the book depot, but I kind of like it sitting in this "controller" .
259
- */
271
+ /***************************************************************************************
272
+ * Save a new book using form values.
273
+ ************************************************************************************** */
260
274
function saveBook ( ) {
261
275
// get values from the html input elements.
262
276
let title = document . getElementById ( "new-book-title" ) . value ;
@@ -296,9 +310,10 @@ function saveBook() {
296
310
resetForm ( ) ;
297
311
}
298
312
299
- /*
300
- Delete a book from the repository
301
- */
313
+ /******************************************************************************************
314
+ * Delete a book from the repository
315
+ * bookId|int: The id for the item to be deleted.
316
+ ******************************************************************************************/
302
317
function deleteBook ( bookId ) {
303
318
// remove the book by id.
304
319
var b = bookDepot . removeBookById ( bookId ) ;
@@ -310,9 +325,10 @@ function deleteBook(bookId) {
310
325
console . log ( JSON . stringify ( b ) ) ;
311
326
}
312
327
313
- /*
314
- Update a single book
315
- */
328
+ /******************************************************************************************
329
+ * Update book.
330
+ * bookId|int: The id of the book to be updated.
331
+ ******************************************************************************************/
316
332
function updateBook ( bookId ) {
317
333
// find the book to be updated
318
334
var book = bookDepot . getBookById ( bookId ) ;
@@ -324,18 +340,17 @@ function updateBook(bookId) {
324
340
document . getElementById ( "new-book-id" ) . value = book . id ;
325
341
}
326
342
327
- /*
328
- Output all of the contents of the repository object to the console.
329
- */
343
+ /******************************************************************************************
344
+ * Output the contents of the repository object to the console.
345
+ ***************************************************************************************** */
330
346
function displayCollection ( ) {
331
347
// display the repository in the console.
332
348
console . log ( bookDepot ) ;
333
349
}
334
350
335
- /*
336
- Seed the repository with previously stored items if any.
337
- Update the display.
338
- */
351
+ /******************************************************************************************
352
+ * Load any existing items from the repository and update the display.
353
+ ******************************************************************************************/
339
354
async function seedTheRepository ( ) {
340
355
bookDepot . books = await bookDepot . retrieve ( ) ;
341
356
updateListDisplay ( ) ;
0 commit comments