@@ -56,6 +56,31 @@ def get_reservables(typ):
5656 ;
5757""" .format (typ ))
5858
59+ _config = None
60+
61+ def get_config (asJson = False ):
62+ global _config
63+
64+ if _config is None :
65+
66+ # noinspection PyBroadException
67+ try :
68+ _config = model .Content ("CalendarSignageConfiguration.json" )
69+ except :
70+ print ("<p>Caution: could not load existing configuration, starting fresh.</p>" )
71+ _config = None
72+
73+ if _config is None or _config .strip () == "" :
74+ _config = "[]"
75+
76+ if asJson :
77+ return _config
78+
79+ import json
80+
81+ return json .loads (_config )
82+
83+
5984if model .HttpMethod == "post" and Data .v == "saveSigns" :
6085 # save signage configuration
6186 import json
@@ -74,19 +99,10 @@ def get_reservables(typ):
7499elif Data .v == "" :
75100 # configuration mode
76101
77- # import knockout from cdn
78102 # language=html
79103 model .Title = "Calendar Signage Configuration"
80104
81- # noinspection PyBroadException
82- try :
83- originalConfig = model .Content ("CalendarSignageConfiguration.json" )
84- except :
85- print ("<p>Caution: could not load existing configuration, starting fresh.</p>" )
86- originalConfig = None
87-
88- if originalConfig is None or originalConfig .strip () == "" :
89- originalConfig = "[]"
105+ originalConfig = get_config ()
90106
91107 ko_form = """
92108 <script>
@@ -118,13 +134,13 @@ def get_reservables(typ):
118134 <td><input type="number" data-bind="value: copies" min="0"/></td>
119135 <td><a data-bind="text: items().length, click: $parent.editSign"></a></td>
120136 <td>
121- <a data-bind="attr: { href: previewLink }"> Preview</a>
122- <a data-bind="click: $parent.removeSign"> Remove</a>
137+ <a data-bind="attr: { href: previewLink }" class="fa fa-eye btn btn-primary" title=" Preview"> </a>
138+ <a data-bind="click: $parent.removeSign" class="fa fa-trash btn btn-primary" title=" Remove"> </a>
123139 </td>
124140 </tr>
125141 </tbody>
126142 </table>
127- <button data-bind="click: addSign"> Add Sign</button >
143+ <a data-bind="click: addSign" class="fa fa-plus btn btn-primary" title=" Add Report"></a >
128144
129145
130146 <div class="modal" id="edit-sign-dialog" data-keyboard="false" data-backdrop="static" style="background: #0009;">
@@ -161,117 +177,118 @@ def get_reservables(typ):
161177 </div>
162178 </div>
163179
164-
165-
166-
167- <script>
168- function Sign(data) {
169- const that = this;
170- this.name = ko.observable(data.name);
171- this.items = ko.observableArray(data.items || []);
172- this.copies = ko.observable(data.copies || 1);
173- this.type = ko.observable(data.type || "CalendarSignageDirectional");
174- this.id = ko.observable(data.id || btoa(Math.random().toString()).substring(0, 6));
175- this.previewLink = ko.computed(function () {
176- return "?v=" + that.id();
177- });
178-
179-
180- this.copies.subscribe((newValue) => {if (newValue < 0) {this.copies(0)}})
181- this.copies.subscribe(() => window.signageViewModel.saveSigns());
182- this.name.subscribe(() => window.signageViewModel.saveSigns());
183- this.type.subscribe(() => window.signageViewModel.saveSigns());
184- this.items.subscribe(() => window.signageViewModel.saveSigns());
185-
186- this.toJson = function () {
187- return {
188- name: that.name(),
189- items: that.items(),
190- copies: that.copies(),
191- type: that.type(),
192- id: that.id()
193- };
194- };
195- }
196-
197- function ViewModel(data) {
198- const self = this;
199- self.signs = ko.observableArray([]);
200-
201- data.forEach(function (signData) {
202- self.signs.push(new Sign(signData));
203- });
204-
205- self.types = ko.observableArray([
206- {name: "Directional", fileName: "CalendarSignageDirectional"},
207- {name: "Reservations", fileName: "CalendarSignageReservations"},
208- {name: "Event Details", fileName: "CalendarSignageEventDetails"}
209- ]);
210-
211- self.currentlyEditingSign = ko.observable(null);
212-
213- self.currentlyEditingSign.subscribe(() => {
214- console.log("here");
215- console.log(self.currentlyEditingSign())
216- if (self.currentlyEditingSign() !== null) {
217- $('#edit-sign-dialog').fadeIn();
218- } else {
219- $('#edit-sign-dialog').fadeOut();
220- }
221- });
222-
223- self.closeModal = function () {
224- self.currentlyEditingSign(null);
225- }
226-
227- self.addSign = function () {
228- const newSign = new Sign({name: "New Sign"});
229- self.signs.push(newSign);
230- return newSign;
231- };
232-
233- self.editSign = function (sign) {
234- self.currentlyEditingSign(sign);
235- };
236-
237- self.removeSign = function (sign) {
238- self.signs.remove(sign);
239- };
240-
241- self.toJson = function () {
242- return ko.toJS(self.signs().map(s => s.toJson()));
243- };
244-
245- self.saveSigns = function () {
246- // Save signs to server
247- const formData = new URLSearchParams();
248- formData.append('data', JSON.stringify(self.toJson()));
249-
250- // post to ?v=saveSigns
251- fetch("/PyScriptForm/CalendarSignage?v=saveSigns", {
252- method: "POST",
253- credentials: 'include',
254- body: formData
255- }).then(response => {
256- if (!response.ok) {
257- throw new Error("Network response was not ok");
258- }
259- return response.json();
260- }).then(() => {
261- console.log("Signs saved successfully!");
262- })
263- };
264-
265- self.signs.subscribe(function (newSigns) {
266- // Save signs to server on change
267- self.saveSigns()
268- });
269- }
270-
271- window.signageViewModel = new ViewModel(originalData);
272- ko.applyBindings(window.signageViewModel);
273- </script>
274- """
180+ <script>
181+ function Sign(data) {
182+ const that = this;
183+ this.name = ko.observable(data.name);
184+ this.items = ko.observableArray(data.items || []);
185+ this.copies = ko.observable(data.copies || 1);
186+ this.type = ko.observable(data.type || "CalendarSignageDirectional");
187+ this.id = ko.observable(data.id || btoa(Math.random().toString()).substring(0, 6));
188+ this.previewLink = ko.computed(function () {
189+ return "?v=" + that.id();
190+ });
191+
192+
193+ this.copies.subscribe((newValue) => {
194+ if (newValue < 0) {
195+ this.copies(0)
196+ }
197+ }) // validate non-negative
198+ this.copies.subscribe(() => window.signageViewModel.saveSigns());
199+ this.name.subscribe(() => window.signageViewModel.saveSigns());
200+ this.type.subscribe(() => window.signageViewModel.saveSigns());
201+ this.items.subscribe(() => window.signageViewModel.saveSigns());
202+
203+ this.toJson = function () {
204+ return {
205+ name: that.name(),
206+ items: that.items(),
207+ copies: that.copies(),
208+ type: that.type(),
209+ id: that.id()
210+ };
211+ };
212+ }
213+
214+ function ViewModel(data) {
215+ const self = this;
216+ self.signs = ko.observableArray([]);
217+
218+ data.forEach(function (signData) {
219+ self.signs.push(new Sign(signData));
220+ });
221+
222+ self.types = ko.observableArray([
223+ {name: "Directional", fileName: "CalendarSignageDirectional"},
224+ {name: "Reservations", fileName: "CalendarSignageReservations"},
225+ {name: "Event Details", fileName: "CalendarSignageEventDetails"}
226+ ]);
227+
228+ self.currentlyEditingSign = ko.observable(null);
229+
230+ self.currentlyEditingSign.subscribe(() => {
231+ console.log("here");
232+ console.log(self.currentlyEditingSign())
233+ if (self.currentlyEditingSign() !== null) {
234+ $('#edit-sign-dialog').fadeIn();
235+ } else {
236+ $('#edit-sign-dialog').fadeOut();
237+ }
238+ });
239+
240+ self.closeModal = function () {
241+ self.currentlyEditingSign(null);
242+ }
243+
244+ self.addSign = function () {
245+ const newSign = new Sign({name: "New Sign"});
246+ self.signs.push(newSign);
247+ return newSign;
248+ };
249+
250+ self.editSign = function (sign) {
251+ self.currentlyEditingSign(sign);
252+ };
253+
254+ self.removeSign = function (sign) {
255+ self.signs.remove(sign);
256+ };
257+
258+ self.toJson = function () {
259+ return ko.toJS(self.signs().map(s => s.toJson()));
260+ };
261+
262+ self.saveSigns = function () {
263+ // Save signs to server
264+ const formData = new URLSearchParams();
265+ formData.append('data', JSON.stringify(self.toJson()));
266+
267+ // post to ?v=saveSigns
268+ fetch("/PyScriptForm/CalendarSignage?v=saveSigns", {
269+ method: "POST",
270+ credentials: 'include',
271+ body: formData
272+ }).then(response => {
273+ if (!response.ok) {
274+ throw new Error("Network response was not ok");
275+ }
276+ return response.json();
277+ }).then(() => {
278+ console.log("Signs saved successfully!");
279+ })
280+ };
281+
282+ self.signs.subscribe(function (newSigns) {
283+ // Save signs to server on change
284+ self.saveSigns()
285+ });
286+ }
287+
288+ window.signageViewModel = new ViewModel(originalData);
289+ ko.applyBindings(window.signageViewModel);
290+ </script>
291+ """
275292
276293 print (ko_form )
277294
@@ -309,4 +326,4 @@ def get_reservables(typ):
309326
310327
311328
312- #
329+ #
0 commit comments