@@ -15,7 +15,7 @@ script "stylesLib"
1515--
1616-- copyright 2021 revIgniter.com
1717--
18- -- Version 1.0.0
18+ -- Version 1.0.1
1919--
2020-- License: Apache License, Version 2.0, see http://www.apache.org/licenses/LICENSE-2.0
2121--
@@ -93,7 +93,7 @@ local sStylesA, sOverwriteStylesheet
9393
9494command syLoadStyles pStylesFilePath
9595 local tStylesFilePath
96-
96+
9797 if pStylesFilePath is empty then
9898 answer file "Select stylesheet:"
9999 put it into tStylesFilePath
@@ -103,7 +103,7 @@ command syLoadStyles pStylesFilePath
103103
104104 put url ("file:" & tStylesFilePath ) into tStylesData
105105 put textEncode (tStylesData ,"UTF-8" ) into tStylesData
106-
106+
107107 try
108108 put JSONToArray(tStylesData ) into sStylesA
109109 catch e
@@ -124,9 +124,9 @@ end syLoadStyles
124124--| COMMAND syApplyStyles
125125--|
126126--| Author: rabit
127- --| Version: 1.0
127+ --| Version: 1.1
128128--| Created: 2021-03-19
129- --| Last Mod: 2021-03 -19
129+ --| Last Mod: 2021-05 -19
130130--| Requires: syResizeElement
131131--|
132132--| Summary: Loop through the list of selectors of a control and apply the appropriate property values.
@@ -140,31 +140,32 @@ end syLoadStyles
140140
141141command syApplyStyles pControlID
142142 local tSelectors , tSelector , tProp , tAlignment , tCurrentPropVal
143-
143+ local tControlPropsA , tPropSynos
144+
144145 put the uStyleSelectors of control id pControlID into tSelectors
145-
146+
146147 if tSelectors <> empty then
147-
148+
148149 # GET ALIGNMENT (LEFT, RIGHT OR CENTER)
149150 put empty into tAlignment
150151 repeat for each item tSelector in tSelectors
151152 if sStylesA [tSelector ] is an array then
152153 if sStylesA [tSelector ]["align" ] <> empty then
153154 if sStylesA [tSelector ]["align" ] is not among the items of "left,right,center" then
154155 syErrorHandling "Wrong format, custom style property" && quote & "align" & quote && "could not be set!"
155-
156+
156157 put empty into sStylesA [tSelector ]["align" ]
157158 # SET FLAG TO RESAVE JSON FILE USING CURRENT VALUE
158159 put TRUE into sOverwriteStylesheet
159-
160+
160161 else
161162 put sStylesA [tSelector ]["align" ] into tAlignment
162163 end if -- if sStylesA[tSelector]["align"] is not among the items of "left,right,center"
163164 end if -- if sStylesA[tSelector]["align"] <> empty
164165 end if -- if sStylesA[tSelector] is an array
165166 end repeat
166167 # #
167-
168+
168169 repeat for each item tSelector in tSelectors
169170 if sStylesA [tSelector ] is an array then
170171 repeat for each key tProp in sStylesA [tSelector ]
@@ -177,26 +178,42 @@ command syApplyStyles pControlID
177178 default
178179 # SET STANDARD LIVECODE PROPERTIES
179180 if tProp <> "align" then
180-
181+
182+ # CHECK IF THE PROPERTY NAME IS VALID
183+ put the properties of control id pControlID into tControlPropsA
184+ if tProp is not among the lines of the keys of tControlPropsA then
185+ put "foregroundColor,firstcolor,textcolor,thumbcolor,fillfore,penfore,forecolor,backgroundColor,secondcolor,backcolor" into tPropSynos
186+
187+ if tProp is not among the items of tPropSynos then
188+ delete variable sStylesA [tSelector ][tProp ]
189+ answer warning "Invalid property name:" && quote & tProp & quote & "! Property name removed."
190+
191+ # SET FLAG TO RESAVE JSON FILE
192+ put TRUE into sOverwriteStylesheet
193+
194+ next repeat
195+ end if
196+ end if -- if tProp is not among the lines of the keys of tControlPropsA
197+
181198 put the tProp of control id pControlID into tCurrentPropVal
182-
199+
183200 try
184201 set the tProp of control id pControlID to sStylesA [tSelector ][tProp ]
185202 catch e
186203 # NOTE debugging
187204 syErrorHandling "Wrong format, property could not be set!"
188-
205+
189206 set the tProp of control id pControlID to tCurrentPropVal
190207 put tCurrentPropVal into sStylesA [tSelector ][tProp ]
191-
208+
192209 # SET FLAG TO RESAVE JSON FILE USING CURRENT VALUE
193210 put TRUE into sOverwriteStylesheet
194211 end try
195-
212+
196213 end if -- if tProp <> "align"
197214 end switch
198215 end repeat
199- end if -- if sStylesA[tSelector] is an array
216+ end if -- if sStylesA[tSelector] is an array
200217 end repeat
201218 end if
202219end syApplyStyles
@@ -211,7 +228,7 @@ end syApplyStyles
211228--| Author: LiveCode
212229--|
213230--| Summary: Parse JSON to a LiveCode array.
214- --|
231+ --|
215232--| Format: JSONToArray(param1)
216233--|
217234--| Parameters: string <pJSON>
@@ -259,49 +276,49 @@ command syNewLook pStylesA
259276 -- put the keys of sStylesA[".default"] into msg ------ properties
260277 -- put displayArrayData(sStylesA) into msg
261278 -- put displayArrayData(sStylesA[".default"]["padding"]) into msg
262-
279+
263280 if pStylesA <> empty then
264281 put pStylesA into sStylesA
265282 end if
266-
267- put the uStyleSelectors of this stack into tStackSelectors
283+
284+ put the uStyleSelectors of this stack into tStackSelectors
268285 put the id of this cd into tCurrentCdID
269-
286+
270287 lock screen
271288 lock messages
272-
289+
273290 # STACK
274291 if tStackSelectors <> empty then
275292 put the long id of this stack into tObject
276293 _syApplyStylesToStackOrCard tObject , tStackSelectors
277294 end if
278-
279-
295+
296+
280297 repeat with i = 1 to the number of lines of the cardIDs of this stack
281298 go cd id (line i of the cardIDs of this stack)
282-
299+
283300 put the uStyleSelectors of this cd into tCardSelectors
284301 put the number of controls of this cd into tNumCtrls
285-
302+
286303 # CARD
287304 if tCardSelectors <> empty then
288305 put the long id of this cd into tObject
289306 _syApplyStylesToStackOrCard tObject , tCardSelectors
290307 end if
291-
308+
292309 # CONTROLS
293310 repeat with tCtrl = 1 to tNumCtrls
294311 put the id of control tCtrl into tControlID
295312 syApplyStyles tControlID
296313 end repeat
297314
298315 end repeat
299-
316+
300317 go cd id tCurrentCdID
301-
318+
302319 unlock messages
303320 unlock screen
304-
321+
305322 # SAVE CORRECTED JSON FILE, IN CASE THE PROPERTY FORMAT WAS INCORRECT
306323 if sOverwriteStylesheet is TRUE then
307324 put FALSE into sOverwriteStylesheet
@@ -337,7 +354,7 @@ end syNewLook
337354
338355private command _syApplyStylesToStackOrCard pObject pSelectors
339356 local tSelector , tProp , tCurrentPropVal
340-
357+
341358 repeat for each item tSelector in pSelectors
342359 if sStylesA [tSelector ] is an array then
343360 repeat for each key tProp in sStylesA [tSelector ]
@@ -347,10 +364,10 @@ private command _syApplyStylesToStackOrCard pObject pSelectors
347364 set the tProp of pObject to sStylesA [tSelector ][tProp ]
348365 catch e
349366 syErrorHandling "Wrong format, property could not be set!"
350-
367+
351368 set the tProp of pObject to tCurrentPropVal
352369 put tCurrentPropVal into sStylesA [tSelector ][tProp ]
353-
370+
354371 # SET FLAG TO RESAVE JSON FILE USING CURRENT VALUE
355372 put TRUE into sOverwriteStylesheet
356373 end try
@@ -387,72 +404,72 @@ end _syApplyStylesToStackOrCard
387404command syResizeElement pControlID, pPadding, pAlignment
388405 local tTextSize , tMargins , tTextWidth , tRight , tLeft
389406 local tHorizontalPadding , tLabelShortName
390-
407+
391408 if pAlignment is empty then
392409 put "center" into pAlignment
393410 end if
394-
411+
395412 # GET TEXT SIZE OF CONTROL
396413 switch word 1 of the long name of control id pControlID
397414 case "field"
398415 if the dontWrap of control id pControlID is true then
399416 put measureText(the text of control id pControlID , control id pControlID , "size" ) into tTextSize
400417 else
401-
418+
402419 end if
403-
420+
404421 break
405422 case "button"
406423 put measureText(the label of control id pControlID , control id pControlID , "size" ) into tTextSize
407-
424+
408425 break
409426 default
410427 # OTHER?
411428 -- default code
412429 end switch
413430 # #
414-
431+
415432 # HORIZONTAL PADDING
416433 put the margins of control id pControlID into tMargins
417434 if tMargins <> empty then
418435 if (item 1 of pPadding ) < tMargins then
419436 put tMargins into item 1 of pPadding
420437 end if
421438 end if
422-
439+
423440 put item 1 of tTextSize into tTextWidth
424441 put 2 * (item 1 of pPadding ) into tHorizontalPadding
425442 # #
426-
443+
427444 # TODO vertical padding
428445 # #
429-
446+
430447 # SET RECT OF CONTROL
431448 switch pAlignment
432449 case "right"
433450 put the right of control id pControlID into tRight
434451 put (tRight - tTextWidth ) - tHorizontalPadding into tLeft
435-
452+
436453 break
437454 case "left"
438455 put the left of control id pControlID into tLeft
439456 put (tLeft + tTextWidth ) + tHorizontalPadding into tRight
440-
457+
441458 break
442459 case "center"
443460 set the width of control id pControlID to (tTextWidth + tHorizontalPadding )
444461 put the right of control id pControlID into tRight
445462 put left of control id pControlID into tLeft
446-
463+
447464 break
448465 default
449466 # OTHER?
450467 syErrorHandling "Value of property" && quote & "align" & quote && "has wrong format:" && pAlignment & ". Use left, right or center."
451468 exit syResizeElement
452469 end switch
453-
470+
454471 set the rect of control id pControlID to tLeft , the top of control id pControlID , tRight , the bottom of control id pControlID
455-
472+
456473 # ALIGN LINKED LABEL IF ANY
457474 if (word 1 of the long name of control id pControlID is "button" ) and (the uLabel of btn id pControlID <> empty ) then
458475 put the uLabel of btn id pControlID into tLabelShortName
@@ -496,4 +513,4 @@ end syErrorHandling
496513
497514
498515-- | END OF stylesLib.livecodescript
499- -- --------------------------------------------------------------------
516+ -- --------------------------------------------------------------------
0 commit comments