]>
Commit | Line | Data |
---|---|---|
0bb29d35 DM |
1 | /* |
2 | * The DiffStore is a in-memory store acting as proxy between a real store | |
3 | * instance and a component. | |
4 | * Its purpose is to redisplay the component *only* if the data has been changed | |
5 | * inside the real store, to avoid the annoying visual flickering of using | |
6 | * the real store directly. | |
7 | * | |
8 | * Implementation: | |
9 | * The DiffStore monitors via mon() the 'load' events sent by the real store. | |
10 | * On each 'load' event, the DiffStore compares its own content with the target | |
11 | * store (call to cond_add_item()) and then fires a 'refresh' event. | |
12 | * The 'refresh' event will automatically trigger a view refresh on the component | |
13 | * who binds to this store. | |
14 | */ | |
15 | ||
16 | /* Config properties: | |
17 | * rstore: the realstore which will autorefresh its content from the API | |
18 | * Only works if rstore has a model and use 'idProperty' | |
19 | * sortAfterUpdate: sort the diffstore before rendering the view | |
20 | */ | |
21 | Ext.define('Proxmox.data.DiffStore', { | |
22 | extend: 'Ext.data.Store', | |
23 | alias: 'store.diff', | |
24 | ||
25 | sortAfterUpdate: false, | |
13fc756d | 26 | |
50fea44c TL |
27 | // if true, destroy rstore on destruction. Defaults to true if a rstore |
28 | // config is passed instead of an existing rstore instance | |
65277f6a DC |
29 | autoDestroyRstore: false, |
30 | ||
31 | onDestroy: function() { | |
32 | let me = this; | |
33 | if (me.autoDestroyRstore) { | |
34 | if (Ext.isFunction(me.rstore.destroy)) { | |
35 | me.rstore.destroy(); | |
36 | } | |
37 | delete me.rstore; | |
38 | } | |
39 | me.callParent(); | |
40 | }, | |
41 | ||
0bb29d35 DM |
42 | constructor: function(config) { |
43 | var me = this; | |
44 | ||
45 | config = config || {}; | |
46 | ||
47 | if (!config.rstore) { | |
48 | throw "no rstore specified"; | |
49 | } | |
50 | ||
51 | if (!config.rstore.model) { | |
52 | throw "no rstore model specified"; | |
53 | } | |
54 | ||
eb54b114 DC |
55 | let rstore; |
56 | if (config.rstore.isInstance) { | |
57 | rstore = config.rstore; | |
58 | } else if (config.rstore.type) { | |
50fea44c TL |
59 | Ext.applyIf(config.rstore, { |
60 | autoDestroyRstore: true, | |
61 | }); | |
eb54b114 DC |
62 | rstore = Ext.create(`store.${config.rstore.type}`, config.rstore); |
63 | } else { | |
64 | throw 'rstore is not an instance, and cannot autocreate without "type"'; | |
65 | } | |
0bb29d35 DM |
66 | |
67 | Ext.apply(config, { | |
68 | model: rstore.model, | |
69 | proxy: { type: 'memory' } | |
70 | }); | |
71 | ||
72 | me.callParent([config]); | |
73 | ||
65277f6a DC |
74 | me.rstore = rstore; |
75 | ||
0bb29d35 DM |
76 | var first_load = true; |
77 | ||
78 | var cond_add_item = function(data, id) { | |
79 | var olditem = me.getById(id); | |
80 | if (olditem) { | |
81 | olditem.beginEdit(); | |
82 | Ext.Array.each(me.model.prototype.fields, function(field) { | |
83 | if (olditem.data[field.name] !== data[field.name]) { | |
84 | olditem.set(field.name, data[field.name]); | |
85 | } | |
86 | }); | |
87 | olditem.endEdit(true); | |
13fc756d | 88 | olditem.commit(); |
0bb29d35 DM |
89 | } else { |
90 | var newrec = Ext.create(me.model, data); | |
91 | var pos = (me.appendAtStart && !first_load) ? 0 : me.data.length; | |
92 | me.insert(pos, newrec); | |
93 | } | |
94 | }; | |
95 | ||
96 | var loadFn = function(s, records, success) { | |
97 | ||
98 | if (!success) { | |
99 | return; | |
100 | } | |
101 | ||
102 | me.suspendEvents(); | |
103 | ||
104 | // getSource returns null if data is not filtered | |
105 | // if it is filtered it returns all records | |
106 | var allItems = me.getData().getSource() || me.getData(); | |
107 | ||
108 | // remove vanished items | |
109 | allItems.each(function(olditem) { | |
65277f6a | 110 | var item = me.rstore.getById(olditem.getId()); |
0bb29d35 DM |
111 | if (!item) { |
112 | me.remove(olditem); | |
113 | } | |
114 | }); | |
115 | ||
65277f6a | 116 | me.rstore.each(function(item) { |
0bb29d35 DM |
117 | cond_add_item(item.data, item.getId()); |
118 | }); | |
119 | ||
120 | me.filter(); | |
121 | ||
122 | if (me.sortAfterUpdate) { | |
123 | me.sort(); | |
124 | } | |
125 | ||
126 | first_load = false; | |
127 | ||
128 | me.resumeEvents(); | |
129 | me.fireEvent('refresh', me); | |
130 | me.fireEvent('datachanged', me); | |
131 | }; | |
132 | ||
65277f6a | 133 | if (me.rstore.isLoaded()) { |
0bb29d35 DM |
134 | // if store is already loaded, |
135 | // insert items instantly | |
65277f6a | 136 | loadFn(me.rstore, [], true); |
0bb29d35 DM |
137 | } |
138 | ||
65277f6a | 139 | me.mon(me.rstore, 'load', loadFn); |
0bb29d35 DM |
140 | } |
141 | }); |