OXT Community Stacks

The scripts of Kanban Board

Version 1.0. These are the scripts exactly as they are in the stack that plays here. Each line has its number, and a link to a line keeps it.

21 scripts in this stack
  1. card id 1002 (card 1) Kanban Board, 562 lines
  2. button "View script" (card 1) Kanban Board, 9 lines
  3. field "NewText" (card 1) Kanban Board, 11 lines
  4. field "Hint" (card 1) Kanban Board, 5 lines
  5. button "Tag Design" (card 1) Kanban Board, 5 lines
  6. button "Tag Code" (card 1) Kanban Board, 5 lines
  7. button "Tag Bug" (card 1) Kanban Board, 5 lines
  8. button "Tag Idea" (card 1) Kanban Board, 5 lines
  9. button "Tag Docs" (card 1) Kanban Board, 5 lines
  10. button "Add" (card 1) Kanban Board, 5 lines
  11. button "ClearDone" (card 1) Kanban Board, 5 lines
  12. button "Reset" (card 1) Kanban Board, 5 lines
  13. the stack script Kanban Board Script Viewer, 28 lines
  14. card "Viewer" (card 1) Kanban Board Script Viewer, 369 lines
  15. field "svObjects" (card 1) Kanban Board Script Viewer, 5 lines
  16. button "svOnly" (card 1) Kanban Board Script Viewer, 5 lines
  17. field "svCode" (card 1) Kanban Board Script Viewer, 5 lines
  18. button "svCopy" (card 1) Kanban Board Script Viewer, 4 lines
  19. button "svShowMe" (card 1) Kanban Board Script Viewer, 4 lines
  20. button "svEdit" (card 1) Kanban Board Script Viewer, 5 lines
  21. button "svClose" (card 1) Kanban Board Script Viewer, 4 lines

card id 1002 (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 562 lines

-- Kanban Board: cards to drag between three columns.
--
-- Every card is a copy of the hidden group "CardTemplate": a rounded
-- graphic, a tag and the card's words. The mouse moves the card you hold;
-- while you hold it, the cards of the column under it make room, slid
-- by a timer a little at a time. The board is written to a file after
-- every change: in a browser that file is kept in the page's own storage.

constant kFile = "kanban-board.txt"
constant kTop = 184
constant kBottom = 548
constant kWidth = 236
constant kGap = 10

local sText, sTag, sCol, sH, sOrder, sNext, sNewTag
local sDrag, sOffX, sOffY, sDownX, sDownY, sMoved, sOverCol, sOverIdx, sOverBin
local sTargetX, sTargetY, sTimer, sFading

on preOpenCard
   kbSetTag "Idea"
   kbLoad
   kbBuildAll
   kbHint
end preOpenCard

on closeCard
   if sTimer is not empty then cancel sTimer
   put empty into sTimer
end closeCard

-- The tags, and the colours of their labels
function kbTagInk pTag
   switch pTag
      case "Design"
         return "190,24,93"
      case "Code"
         return "29,78,216"
      case "Bug"
         return "185,28,28"
      case "Docs"
         return "15,118,110"
      default
         return "180,83,9"
   end switch
end kbTagInk

function kbTagTint pTag
   switch pTag
      case "Design"
         return "252,231,243"
      case "Code"
         return "219,234,254"
      case "Bug"
         return "254,226,226"
      case "Docs"
         return "204,251,241"
      default
         return "254,243,199"
   end switch
end kbTagTint

command kbSetTag pTag
   local tTag
   put pTag into sNewTag
   repeat for each item tTag in "Design,Code,Bug,Idea,Docs"
      set the hilite of button ("Tag" && tTag) to (tTag is pTag)
   end repeat
end kbSetTag

command kbHint
   set the visible of field "Hint" to (the text of field "NewText" is empty)
end kbHint

-- Where the board is kept. In a browser, the documents folder is the
-- page's own storage, kept between visits.
function kbPath
   local tFolder
   if the platform is "HTML5" then
      put specialFolderPath("documents") into tFolder
   else
      put specialFolderPath("temporary") & "/oxt-kanban" into tFolder
   end if
   if there is not a folder tFolder then create folder tFolder
   return tFolder & "/" & kFile
end kbPath

function kbFirstBoard
   return "1" & tab & "Idea" & tab & "Teach the robot a new joke" & return & \
         "1" & tab & "Design" & tab & "Pick a colour for the new logo" & return & \
         "1" & tab & "Docs" & tab & "Write the getting started page" & return & \
         "1" & tab & "Bug" & tab & "The snake is too fast on level 9" & return & \
         "2" & tab & "Code" & tab & "Drag cards from one column to the next" & return & \
         "2" & tab & "Design" & tab & "A dark look for every sample" & return & \
         "3" & tab & "Code" & tab & "Run stacks in a web browser" & return & \
         "3" & tab & "Docs" & tab & "Explain what the Stack Player does"
end kbFirstBoard

command kbLoad
   local tData, tLine, c
   put url ("file:" & kbPath()) into tData
   if tData is empty then put kbFirstBoard() into tData
   put empty into sText
   put empty into sTag
   put empty into sCol
   put empty into sH
   repeat with c = 1 to 3
      put empty into sOrder[c]
   end repeat
   put 0 into sNext
   set the itemDelimiter to tab
   repeat for each line tLine in tData
      if item 1 of tLine is not among the items of ("1" & tab & "2" & tab & "3") then next repeat
      if item 3 of tLine is empty then next repeat
      add 1 to sNext
      put item 1 of tLine into sCol[sNext]
      put item 2 of tLine into sTag[sNext]
      put item 3 of tLine into sText[sNext]
   end repeat
   set the itemDelimiter to comma
   repeat with c = 1 to sNext
      kbAppend sCol[c], c
   end repeat
end kbLoad

command kbAppend pCol, pId
   if sOrder[pCol] is empty then
      put pId into sOrder[pCol]
   else
      put comma & pId after sOrder[pCol]
   end if
end kbAppend

command kbSave
   local tData, c, tId
   repeat with c = 1 to 3
      repeat for each item tId in sOrder[c]
         put c & tab & sTag[tId] & tab & sText[tId] & return after tData
      end repeat
   end repeat
   put tData into url ("file:" & kbPath())
end kbSave

-- Makes every card again from the data
command kbBuildAll
   local tGroups, c, tId
   lock screen
   repeat with c = the number of groups of this card down to 1
      if the short name of group c begins with "kbCard" then delete group c
   end repeat
   repeat with c = 1 to 3
      repeat for each item tId in sOrder[c]
         kbMakeCard tId
      end repeat
   end repeat
   put empty into sDrag
   kbLayout
   repeat with c = 1 to 3
      repeat for each item tId in sOrder[c]
         set the topLeft of group ("kbCard" & tId) to sTargetX[tId], sTargetY[tId]
      end repeat
   end repeat
   kbCounts
   unlock screen
end kbBuildAll

command kbMakeCard pId
   copy group "CardTemplate" to this card
   set the name of it to "kbCard" & pId
   set the visible of group ("kbCard" & pId) to true
   kbFill pId
end kbMakeCard

-- Puts a card's words and tag on it, and sizes it to fit them
command kbFill pId
   local tG, tL, tT, tFH, tW, tH, tDone
   put "kbCard" & pId into tG
   put the left of group tG into tL
   put the top of group tG into tT
   put sCol[pId] is 3 into tDone
   set the rect of field "cText" of group tG to tL + 12, tT + 32, tL + 224, tT + 52
   set the text of field "cText" of group tG to sText[pId]
   put the formattedHeight of field "cText" of group tG into tFH
   set the rect of field "cText" of group tG to tL + 12, tT + 32, tL + 224, tT + 36 + tFH
   set the text of field "cTag" of group tG to sTag[pId]
   put the formattedWidth of field "cTag" of group tG + 16 into tW
   -- the tag's letters sit in the middle of its pill (the field's text
   -- starts at the top of its rect)
   set the rect of field "cTag" of group tG to tL + 12, tT + 15, tL + 12 + tW, tT + 31
   set the rect of graphic "cPill" of group tG to tL + 12, tT + 10, tL + 12 + tW, tT + 28
   set the foregroundColor of field "cTag" of group tG to kbTagInk(sTag[pId])
   set the backgroundColor of graphic "cPill" of group tG to kbTagTint(sTag[pId])
   put 36 + tFH + 10 into tH
   set the rect of graphic "cBack" of group tG to tL, tT, tL + kWidth, tT + tH
   put tH into sH[pId]
   if tDone then
      set the backgroundColor of graphic "cBack" of group tG to "203,213,225"
      set the foregroundColor of field "cText" of group tG to "71,85,105"
      set the textStyle of field "cText" of group tG to "strikeout"
      kbUnstrikeWraps tG
   else
      set the backgroundColor of graphic "cBack" of group tG to "248,250,252"
      set the foregroundColor of field "cText" of group tG to "30,41,59"
      set the textStyle of field "cText" of group tG to "plain"
   end if
end kbFill

-- A line wraps after a space, and a struck-out space at the end of a
-- line looks like a hyphen ("Player-"): the spaces where a line breaks
-- are left plain.
command kbUnstrikeWraps pG
   local tLen, i
   put the length of field "cText" of group pG into tLen
   repeat with i = 1 to tLen - 1
      if char i of field "cText" of group pG is space then
         if the formattedTop of char i + 1 of field "cText" of group pG > the formattedTop of char i of field "cText" of group pG then
            set the textStyle of char i of field "cText" of group pG to "plain"
         end if
      end if
   end repeat
end kbUnstrikeWraps

function kbColLeft pCol
   return 40 + (pCol - 1) * 280 + 12
end kbColLeft

-- Where every card should be. While a card is held over a column, that
-- column leaves a gap where it would land.
command kbLayout
   local c, tIds, tId, tY, tN, k, tStep, tTotal, tGapH
   repeat with c = 1 to 3
      put empty into tIds
      repeat for each item tId in sOrder[c]
         if tId is not sDrag then put tId & comma after tIds
      end repeat
      delete the last char of tIds
      put the number of items of tIds into tN
      put 0 into tGapH
      if sDrag is not empty and sOverCol is c then put sH[sDrag] + kGap into tGapH
      -- when the column is too full, the cards overlap
      put tGapH into tTotal
      repeat for each item tId in tIds
         add sH[tId] + kGap to tTotal
      end repeat
      put kGap into tStep
      if tTotal > kBottom - kTop and tN > 1 then
         put kGap - (tTotal - (kBottom - kTop)) / (tN - 1) into tStep
      end if
      put kTop into tY
      put 0 into k
      repeat for each item tId in tIds
         add 1 to k
         if tGapH > 0 and k is sOverIdx then add tGapH to tY
         put kbColLeft(c) into sTargetX[tId]
         put round(tY) into sTargetY[tId]
         add sH[tId] + tStep to tY
      end repeat
   end repeat
   kbAnimate
end kbLayout

command kbAnimate
   if sTimer is empty then
      send "kbStep" to me in 16 milliseconds
      put the result into sTimer
   end if
end kbAnimate

-- One frame: every card goes a third of the way to where it should be
command kbStep
   local c, tId, tG, tX, tY, tDX, tDY, tMoving
   put empty into sTimer
   put false into tMoving
   lock screen
   repeat with c = 1 to 3
      repeat for each item tId in sOrder[c]
         if tId is sDrag then next repeat
         put "kbCard" & tId into tG
         if there is not a group tG then next repeat
         put the left of group tG into tX
         put the top of group tG into tY
         put sTargetX[tId] - tX into tDX
         put sTargetY[tId] - tY into tDY
         if abs(tDX) < 2 and abs(tDY) < 2 then
            if tDX is not 0 or tDY is not 0 then
               set the topLeft of group tG to sTargetX[tId], sTargetY[tId]
            end if
         else
            put true into tMoving
            set the topLeft of group tG to tX + round(tDX * 0.35), tY + round(tDY * 0.35)
         end if
      end repeat
   end repeat
   unlock screen
   if tMoving then
      send "kbStep" to me in 16 milliseconds
      put the result into sTimer
   end if
end kbStep

command kbCounts
   local c
   repeat with c = 1 to 3
      set the text of field ("Count" & c) to the number of items of sOrder[c]
   end repeat
   set the visible of button "ClearDone" to (sOrder[3] is not empty)
end kbCounts

-- The card under the mouse, if any
function kbCardOf pTarget
   local tObj
   put pTarget into tObj
   if word 1 of tObj is "card" or word 1 of tObj is "stack" then return empty
   if the short name of tObj begins with "kbCard" then return char 7 to -1 of the short name of tObj
   put the long owner of tObj into tObj
   if word 1 of tObj is "group" and the short name of tObj begins with "kbCard" then return char 7 to -1 of the short name of tObj
   return empty
end kbCardOf

on mouseDown pButton
   local tId
   if pButton is not 1 then pass mouseDown
   put kbCardOf(the long id of the target) into tId
   if tId is empty or tId is among the items of sFading then pass mouseDown
   put tId into sDrag
   put false into sMoved
   put the mouseH into sDownX
   put the mouseV into sDownY
   put sDownX - the left of group ("kbCard" & tId) into sOffX
   put sDownY - the top of group ("kbCard" & tId) into sOffY
end mouseDown

on mouseMove pX, pY
   local tCol
   if sDrag is empty then pass mouseMove
   if not sMoved then
      if abs(pX - sDownX) + abs(pY - sDownY) < 5 then exit mouseMove
      kbStartDrag
   end if
   set the topLeft of group ("kbCard" & sDrag) to pX - sOffX, pY - sOffY
   kbOver pX, pY
end mouseMove

command kbStartDrag
   put true into sMoved
   put sCol[sDrag] into sOverCol
   put itemOffset(sDrag, sOrder[sOverCol]) into sOverIdx
   relayer group ("kbCard" & sDrag) to front of this card
   set the dropShadow["color"] of graphic "cBack" of group ("kbCard" & sDrag) to "0,0,0"
   set the dropShadow["opacity"] of graphic "cBack" of group ("kbCard" & sDrag) to 110
   set the dropShadow["size"] of graphic "cBack" of group ("kbCard" & sDrag) to 18
   set the dropShadow["distance"] of graphic "cBack" of group ("kbCard" & sDrag) to 8
   set the visible of group "BinZone" to true
   relayer group "BinZone" to front of this card
   relayer group ("kbCard" & sDrag) to front of this card
end kbStartDrag

-- What the held card is over: a column (and where in it), or the bin
command kbOver pX, pY
   local tCol, c, tIdx, tId, tMid
   put false into sOverBin
   put empty into tCol
   if pY >= 76 and pY <= 128 then
      put true into sOverBin
   else
      repeat with c = 1 to 3
         if pX >= 40 + (c - 1) * 280 - 10 and pX < 40 + (c - 1) * 280 + 270 then put c into tCol
      end repeat
   end if
   if tCol is empty then put sOverCol into tCol
   if sOverBin then
      set the backgroundColor of graphic "Bin" to "185,28,28"
   else
      set the backgroundColor of graphic "Bin" to "127,29,29"
   end if
   -- the place in the column: after every card whose middle is above the mouse
   put 1 into tIdx
   repeat for each item tId in sOrder[tCol]
      if tId is sDrag then next repeat
      put sTargetY[tId] + sH[tId] / 2 into tMid
      if pY - sOffY + sH[sDrag] / 2 > tMid then add 1 to tIdx
   end repeat
   if sOverBin then put 0 into tIdx
   repeat with c = 1 to 3
      if c is tCol and not sOverBin then
         set the lineSize of graphic ("Col" & c) to 2
         set the backgroundColor of graphic ("Col" & c) to "30,41,72"
      else
         set the lineSize of graphic ("Col" & c) to 0
         set the backgroundColor of graphic ("Col" & c) to "24,32,56"
      end if
   end repeat
   if tCol is not sOverCol or tIdx is not sOverIdx then
      put tCol into sOverCol
      put tIdx into sOverIdx
      kbLayout
   end if
end kbOver

on mouseUp pButton
   kbDrop
   pass mouseUp
end mouseUp

on mouseRelease pButton
   kbDrop
   pass mouseRelease
end mouseRelease

command kbDrop
   local tId, tFrom, tList, c, tNew, k, tItem
   if sDrag is empty then exit kbDrop
   put sDrag into tId
   if not sMoved then
      put empty into sDrag
      exit kbDrop
   end if
   set the visible of group "BinZone" to false
   repeat with c = 1 to 3
      set the lineSize of graphic ("Col" & c) to 0
      set the backgroundColor of graphic ("Col" & c) to "24,32,56"
   end repeat
   set the dropShadow of graphic "cBack" of group ("kbCard" & tId) to empty
   put sCol[tId] into tFrom
   kbRemove tFrom, tId
   if sOverBin then
      put empty into sDrag
      kbDelete tId
      exit kbDrop
   end if
   -- into its new place
   put empty into tNew
   put 0 into k
   repeat for each item tItem in sOrder[sOverCol]
      add 1 to k
      if k is sOverIdx then put tId & comma after tNew
      put tItem & comma after tNew
   end repeat
   if sOverIdx > k then put tId & comma after tNew
   delete the last char of tNew
   put tNew into sOrder[sOverCol]
   put sOverCol into sCol[tId]
   put empty into sDrag
   put empty into sOverCol
   put empty into sOverIdx
   if tFrom is 3 or sCol[tId] is 3 then kbFill tId
   kbCounts
   kbLayout
   kbSave
end kbDrop

command kbRemove pCol, pId
   local tNew, tItem
   repeat for each item tItem in sOrder[pCol]
      if tItem is not pId then put tItem & comma after tNew
   end repeat
   delete the last char of tNew
   put tNew into sOrder[pCol]
end kbRemove

-- A deleted card fades away
command kbDelete pId
   put empty into sCol[pId]
   if sFading is empty then
      put pId into sFading
   else
      put comma & pId after sFading
   end if
   kbCounts
   kbLayout
   kbSave
   send "kbFade" && pId & ",0" to me in 16 milliseconds
end kbDelete

command kbFade pId, pLevel
   local tNew, tItem
   if there is not a group ("kbCard" & pId) then exit kbFade
   if pLevel >= 100 then
      delete group ("kbCard" & pId)
      repeat for each item tItem in sFading
         if tItem is not pId then put tItem & comma after tNew
      end repeat
      delete the last char of tNew
      put tNew into sFading
      exit kbFade
   end if
   set the blendLevel of group ("kbCard" & pId) to pLevel
   send "kbFade" && pId & comma & (pLevel + 20) to me in 16 milliseconds
end kbFade

on mouseDoubleUp pButton
   local tId, tWords
   put kbCardOf(the long id of the target) into tId
   if tId is empty or tId is among the items of sFading then pass mouseDoubleUp
   put empty into sDrag
   ask "Change the card:" with sText[tId]
   if the result is "Cancel" then exit mouseDoubleUp
   put kbClean(it) into tWords
   if tWords is empty then exit mouseDoubleUp
   put tWords into sText[tId]
   kbFill tId
   kbLayout
   kbSave
end mouseDoubleUp

-- One line, with no tabs: the file keeps a card on each line
function kbClean pText
   replace tab with space in pText
   replace return with space in pText
   replace linefeed with space in pText
   return word 1 to -1 of pText
end kbClean

command kbAdd
   local tWords
   put kbClean(the text of field "NewText") into tWords
   if tWords is empty then
      focus on field "NewText"
      exit kbAdd
   end if
   add 1 to sNext
   put tWords into sText[sNext]
   put sNewTag into sTag[sNext]
   put 1 into sCol[sNext]
   if sOrder[1] is empty then
      put sNext into sOrder[1]
   else
      put sNext & comma before sOrder[1]
   end if
   kbMakeCard sNext
   -- it drops in from the bar
   set the topLeft of group ("kbCard" & sNext) to 52, 88
   put empty into field "NewText"
   kbHint
   kbCounts
   kbLayout
   kbSave
end kbAdd

command kbClearDone
   local tId
   repeat for each item tId in sOrder[3]
      put empty into sCol[tId]
      send "kbFade" && tId & ",0" to me in 16 milliseconds
      if sFading is empty then
         put tId into sFading
      else
         put comma & tId after sFading
      end if
   end repeat
   put empty into sOrder[3]
   kbCounts
   kbSave
end kbClearDone

command kbStartOver
   answer "Put the first board back? Your cards will be deleted." with "Cancel" or "Start over"
   if it is not "Start over" then exit kbStartOver
   delete file kbPath()
   kbLoad
   kbBuildAll
end kbStartOver

button "View script" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 9 lines

-- Opens the Script Viewer: the scripts of this stack, in colour.
on mouseUp
   if the mode of stack "Kanban Board Script Viewer" is not 0 then
      close stack "Kanban Board Script Viewer"
   else
      dispatch "svOpen" to card "Viewer" of stack "Kanban Board Script Viewer" with the short name of this stack
   end if
end mouseUp

field "NewText" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 11 lines

-- Return adds the card.
on returnInField
   kbAdd
end returnInField
on enterInField
   kbAdd
end enterInField
on textChanged
   kbHint
end textChanged

field "Hint" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- A click on the hint goes to the field under it.
on mouseUp
   focus on field "NewText"
end mouseUp

button "Tag Design" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- The tag of the next card.
on mouseUp
   kbSetTag the label of me
end mouseUp

button "Tag Code" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- The tag of the next card.
on mouseUp
   kbSetTag the label of me
end mouseUp

button "Tag Bug" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- The tag of the next card.
on mouseUp
   kbSetTag the label of me
end mouseUp

button "Tag Idea" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- The tag of the next card.
on mouseUp
   kbSetTag the label of me
end mouseUp

button "Tag Docs" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- The tag of the next card.
on mouseUp
   kbSetTag the label of me
end mouseUp

button "Add" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- Adds the card to To do.
on mouseUp
   kbAdd
end mouseUp

button "ClearDone" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- Deletes every card in Done.
on mouseUp
   kbClearDone
end mouseUp

button "Reset" (card 1)

Kanban Board.oxtstack, stack "Kanban Board", 5 lines

-- Puts the first board back.
on mouseUp
   kbStartOver
end mouseUp

the stack script

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 28 lines

-- The Script Viewer's own messages stop here, so they never reach the
-- stack it belongs to: that stack's openCard, mouseUp and arrowKey
-- handlers are for its own cards.
on preOpenStack
end preOpenStack
on openStack
end openStack
on preOpenCard
end preOpenCard
on openCard
end openCard
on closeCard
end closeCard
on closeStack
end closeStack
on mouseUp
end mouseUp
on mouseDown
end mouseDown
on arrowKey pKey
   -- up and down move through the list
   if pKey is "up" then svMove -1
   if pKey is "down" then svMove 1
end arrowKey
on escapeKey
   close this stack
end escapeKey

card "Viewer" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 369 lines

-- ===== The Script Viewer =====
-- Lists the scripts of the card in front of the stack that opened it,
-- and shows the one chosen in colour: comments, strings, numbers,
-- keywords, commands and the names of handlers each in their own
-- colour, with line numbers. Any stack opens it with svOpen:
--
--    dispatch "svOpen" to card 1 of stack "Kanban Board Script Viewer" with "My Stack"
--
-- Controls are named "of me" (this card), so these handlers work even
-- while another stack is the default stack.

-- The colours of the code, on its dark background.
constant kInk = "#E0DEF4"
constant kComment = "#7F8FA6"
constant kString = "#F6C177"
constant kNumber = "#F8A978"
constant kKeyword = "#C4A7E7"
constant kCommand = "#9CCFD8"
constant kDeclare = "#EB6F92"
constant kName = "#82AAFF"
constant kSoft = "#A6A3C4"

-- Words of each kind. A word may be in only one list.
constant kKeywords = "if,then,else,end,repeat,for,each,while,until,forever,times,switch,case,default,break,exit,next,pass,return,try,catch,finally,throw,do,send,dispatch,call,with,in"
constant kCommands = "put,set,get,go,answer,ask,create,delete,show,hide,lock,unlock,wait,add,subtract,multiply,divide,sort,replace,open,close,palette,move,play,beep,select,copy,paste,start,stop,save,write,read,launch,click,type,focus,choose,cancel,import,export,print,filter,split,combine,load,post"
constant kSoftWords = "the,of,into,after,before,and,or,not,is,a,an,to,me,it,empty,true,false,quote,space,tab,comma,cr,return,line,lines,word,words,char,chars,item,items,field,button,card,stack,graphic,group,this,target,among,contains,there"
constant kDeclares = "on,command,function,private,getprop,setprop,local,global,constant"

local sTarget    -- the name of the stack whose scripts are listed
local sIDs       -- the long id of the object on each line of the list
local sInBlock   -- true inside a /* ... */ comment while colouring

-- Open the viewer for a stack, beside its window, listing the scripts
-- of its card in front. pChoose "stack" chooses the stack script.
command svOpen pStack, pChoose
   local tR, tScreen, tHeight, tLeft, tTop
   put pStack into sTarget
   put the rect of stack pStack into tR
   put the working screenRect into tScreen
   -- As tall as the screen allows, keeping room above for the title bar,
   -- so that the window can always be dragged.
   put min(640, item 4 of tScreen - item 2 of tScreen - 44) into tHeight
   put max(440, tHeight) into tHeight
   if the height of this stack is not tHeight then
      set the height of this stack to tHeight
   end if
   svLayout
   -- To the right of the stack's window, or to its left, or over it.
   put item 3 of tR + 12 into tLeft
   if tLeft + the width of this stack > item 3 of tScreen then
      put item 1 of tR - 12 - the width of this stack into tLeft
   end if
   if tLeft < item 1 of tScreen then
      put item 3 of tR - the width of this stack - 12 into tLeft
   end if
   put max(item 1 of tScreen, tLeft) into tLeft
   -- Level with the stack, but never above the screen or off its bottom.
   put max(item 2 of tR, item 2 of tScreen + 32) into tTop
   if tTop + tHeight > item 4 of tScreen - 8 then
      put item 4 of tScreen - 8 - tHeight into tTop
   end if
   put max(tTop, item 2 of tScreen + 32) into tTop
   set the topLeft of this stack to tLeft, tTop
   -- Open first: a list field keeps its chosen line only while open.
   if the mode of this stack is 0 then
      palette this stack
   end if
   svRefresh pChoose
end svOpen

-- The window can be made shorter or taller: the list and the code share
-- the height, and the buttons stay at the bottom.
on resizeStack
   svLayout
end resizeStack

command svLayout
   local tH, tList, tY, tButton
   put the height of this stack into tH
   put max(70, 140 - max(0, 600 - tH)) into tList
   lock screen
   set the rect of field "svObjects" of me to 16, 78, 524, 78 + tList
   put 78 + tList + 4 into tY
   set the rect of button "svOnly" of me to 16, tY, 330, tY + 22
   add 28 to tY
   set the rect of field "svAbout" of me to 16, tY, 524, tY + 54
   add 62 to tY
   set the rect of field "svGutter" of me to 16, tY, 58, tH - 52
   set the rect of field "svCode" of me to 58, tY, 524, tH - 52
   repeat for each item tButton in "svCopy,svShowMe,svEdit,svClose"
      set the bottom of button tButton of me to tH - 12
   end repeat
   unlock screen
end svLayout

-- Rebuild the list for the target's card in front.
command svRefresh pChoose
   local tCard, tMain, tList, tObj, i, tOnly, tPrevious, tLine
   if sTarget is empty or there is not a stack sTarget then exit svRefresh
   -- Remember which object was chosen, to choose it again.
   put line (the hilitedLine of field "svObjects" of me) of sIDs into tPrevious
   put the long id of stack sTarget into tMain
   put the long id of this card of stack sTarget into tCard
   put sTarget & ": the card" && quote & the short name of tCard & quote into field "svSub" of me
   put the hilite of button "svOnly" of me into tOnly
   -- The stack and the card come first: they are always listed.
   put "Stack script" & tab & svCount(tMain) into tList
   put tMain into sIDs
   put return & "Card script" & tab & svCount(tCard) after tList
   put return & tCard after sIDs
   -- Then each control of the card, from the back to the front.
   repeat with i = 1 to the number of controls of tCard
      put the long id of control i of tCard into tObj
      if tOnly and the script of tObj is empty then next repeat
      put return after tList
      -- Controls in a group are indented under it.
      if word 1 of the owner of tObj is "group" then put "    " after tList
      put the name of tObj & tab & svCount(tObj) after tList
      put return & tObj after sIDs
   end repeat
   put tList into field "svObjects" of me
   -- Choose as asked, or the same object again, or else the card script.
   put 2 into tLine
   if pChoose is "stack" then
      put 1 into tLine
   else if tPrevious is not empty then
      set the wholeMatches to true
      if lineOffset(tPrevious, sIDs) > 0 then put lineOffset(tPrevious, sIDs) into tLine
   end if
   set the hilitedLine of field "svObjects" of me to tLine
   svShow
end svRefresh

-- "12 lines", or "no script".
function svCount pObj
   local tCount
   put the number of lines of the script of pObj into tCount
   if tCount is 0 then return "no script"
   if tCount is 1 then return "1 line"
   return tCount && "lines"
end svCount

-- The long id of the object chosen in the list (empty when none).
function svCurrent
   local tLine
   put the hilitedLine of field "svObjects" of me into tLine
   if tLine is empty then return empty
   return line tLine of sIDs
end svCurrent

-- Show the chosen object's script, and a word on what it is for.
command svShow
   local tObj, tScript, tNumbers, i
   put svCurrent() into tObj
   if tObj is empty then exit svShow
   if not exists(tObj) then
      -- It was deleted since the list was made.
      svRefresh
      exit svShow
   end if
   lock screen
   put svAbout(tObj) into field "svAbout" of me
   put the script of tObj into tScript
   if tScript is empty then put "-- This object has no script." into tScript
   -- htmlText sets the text and its colours in one go.
   set the htmlText of field "svCode" of me to svColoured(tScript)
   repeat with i = 1 to the number of lines of tScript
      put i & return after tNumbers
   end repeat
   put char 1 to -2 of tNumbers into field "svGutter" of me
   set the vScroll of field "svCode" of me to 0
   set the hScroll of field "svCode" of me to 0
   set the vScroll of field "svGutter" of me to 0
   unlock screen
end svShow

-- The script as htmlText, one coloured paragraph per line.
function svColoured pScript
   local tLine, tHtml
   put false into sInBlock
   repeat for each line tLine in pScript
      put "<p>" & svColourLine(tLine) & "</p>" after tHtml
   end repeat
   return tHtml
end svColoured

-- One line, coloured word by word. Walks the line a character at a
-- time: a quote starts a string, -- or # a comment to the end of the
-- line, /* a comment to */, a letter a word.
function svColourLine pLine
   local tOut, tLen, i, tChar, tStart, tWord, tPrev, tEnd
   put the number of chars of pLine into tLen
   put 1 into i
   if sInBlock then
      put offset("*/", pLine) into tEnd
      if tEnd is 0 then return svSpan(pLine, kComment)
      put svSpan(char 1 to tEnd + 1 of pLine, kComment) into tOut
      put false into sInBlock
      put tEnd + 2 into i
   end if
   repeat while i <= tLen
      put char i of pLine into tChar
      if tChar is quote then
         put i into tStart
         add 1 to i
         repeat while i <= tLen and char i of pLine is not quote
            add 1 to i
         end repeat
         put svSpan(char tStart to i of pLine, kString) after tOut
         add 1 to i
      else if char i to i + 1 of pLine is "--" or tChar is "#" then
         put svSpan(char i to tLen of pLine, kComment) after tOut
         exit repeat
      else if char i to i + 1 of pLine is "/*" then
         put offset("*/", char i + 2 to tLen of pLine) into tEnd
         if tEnd is 0 then
            put svSpan(char i to tLen of pLine, kComment) after tOut
            put true into sInBlock
            exit repeat
         end if
         put svSpan(char i to i + tEnd + 2 of pLine, kComment) after tOut
         add tEnd + 3 to i
      else if svIsWordChar(tChar) then
         put i into tStart
         repeat while i <= tLen and svIsWordChar(char i of pLine)
            add 1 to i
         end repeat
         put char tStart to i - 1 of pLine into tWord
         put svSpan(tWord, svWordColour(tWord, tPrev)) after tOut
         put tWord into tPrev
      else
         put svEscape(tChar) after tOut
         add 1 to i
      end if
   end repeat
   return tOut
end svColourLine

-- The colour of a word, which can depend on the word before it: the
-- word after on, command or function is the name of a handler.
function svWordColour pWord, pPrevious
   if pPrevious is among the items of "on,command,function,getProp,setProp" then return kName
   if pWord is a number then return kNumber
   if pWord is among the items of kDeclares then return kDeclare
   if pWord is among the items of kKeywords then return kKeyword
   if pWord is among the items of kCommands then return kCommand
   if pWord is among the items of kSoftWords then return kSoft
   return kInk
end svWordColour

function svIsWordChar pChar
   return pChar is in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
end svIsWordChar

function svSpan pText, pColour
   return "<font color=" & quote & pColour & quote & ">" & svEscape(pText) & "</font>"
end svSpan

-- < > and & mean something in HTML: write them as entities.
function svEscape pText
   replace "&" with "&amp;" in pText
   replace "<" with "&lt;" in pText
   replace ">" with "&gt;" in pText
   return pText
end svEscape

-- A sentence or two on what this kind of script does, and its handlers.
function svAbout pObj
   local tKind, tText, tHandlers
   put word 1 of the name of pObj into tKind
   switch tKind
      case "stack"
         put "The stack script is the last stop on the message path for every card, so its handlers can be called from anywhere in the stack." into tText
         break
      case "card"
         put "A card script gets the card's own messages (openCard, closeCard) and the messages of its controls that they do not handle." into tText
         break
      case "group"
         put "A group script gets the messages of the controls inside the group that they do not handle themselves." into tText
         break
      default
         if the script of pObj is not empty then
            put "This" && tKind & "'s handlers run when it gets the messages they are named after: mouseUp for a click." into tText
         else if the cGoTo of pObj is not empty then
            put "No script: its click goes up the message path to the stack script, which reads its cGoTo property (" & quote & the cGoTo of pObj & quote & ")." into tText
         else if word 1 of the owner of pObj is "group" then
            put "No script: its messages go on to its group," && the short name of the owner of pObj & "." into tText
         else
            put "No script: this" && tKind && "only shows something, and other scripts change it." into tText
         end if
   end switch
   put svHandlers(the script of pObj) into tHandlers
   if tHandlers is not empty then put return & "Handlers: " & tHandlers after tText
   return tText
end svAbout

-- The names of the handlers in a script, joined with commas.
function svHandlers pScript
   local tLine, tDecl, tList
   repeat for each line tLine in pScript
      put tLine into tDecl
      if word 1 of tDecl is "private" then delete word 1 of tDecl
      if word 1 of tDecl is among the items of "on,command,function,getProp,setProp" then
         put word 2 of tDecl & ", " after tList
      end if
   end repeat
   if tList is not empty then delete char -2 to -1 of tList
   return tList
end svHandlers

-- Copy the chosen script, and say so on the button for a moment.
command svCopy
   local tObj
   put svCurrent() into tObj
   if tObj is empty then exit svCopy
   set the clipboardData["text"] to the script of tObj
   set the label of button "svCopy" of me to "Copied!"
   send "svCopyDone" to me in 1200 milliseconds
end svCopy

on svCopyDone
   set the label of button "svCopy" of me to "Copy"
end svCopyDone

-- Open the chosen script in the Script Editor (only in the IDE).
command svEdit
   local tObj
   put svCurrent() into tObj
   if tObj is empty then exit svEdit
   edit the script of tObj
end svEdit

-- Make the chosen control blink, to show where it is.
command svShowMe
   local tObj
   put svCurrent() into tObj
   if tObj is empty then exit svShowMe
   if word 1 of the name of tObj is among the items of "stack,card" then
      put "Show me works on a control: choose one in the list." into field "svAbout" of me
      exit svShowMe
   end if
   if not the visible of tObj then
      put "This control is hidden right now, so it cannot blink." into field "svAbout" of me
      exit svShowMe
   end if
   repeat 3 times
      set the visible of tObj to false
      wait 150 milliseconds with messages
      set the visible of tObj to true
      wait 150 milliseconds with messages
   end repeat
end svShowMe

-- The arrow keys move the choice in the list.
command svMove pStep
   local tLine
   put the hilitedLine of field "svObjects" of me into tLine
   if tLine is empty then put 0 into tLine
   put max(1, min(the number of lines of field "svObjects" of me, tLine + pStep)) into tLine
   set the hilitedLine of field "svObjects" of me to tLine
   svShow
end svMove


-- Edit script is shown only in the IDE, where the Script Editor is.
on preOpenCard
   set the visible of button "svEdit" of me to (the environment is "development")
end preOpenCard

field "svObjects" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 5 lines

-- Show the script of the object clicked. svShow is in the card script.
on mouseUp
   svShow
end mouseUp

button "svOnly" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 5 lines

-- List every control, or only those with a script.
on mouseUp
   svRefresh
end mouseUp

field "svCode" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 5 lines

-- The line numbers scroll with the code.
on scrollbarDrag
   set the vScroll of field "svGutter" of me to the vScroll of me
end scrollbarDrag

button "svCopy" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 4 lines

on mouseUp
   svCopy
end mouseUp

button "svShowMe" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 4 lines

on mouseUp
   svShowMe
end mouseUp

button "svEdit" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 5 lines

-- edit opens the Script Editor, which only the IDE has.
on mouseUp
   svEdit
end mouseUp

button "svClose" (card 1)

Kanban Board.oxtstack, stack "Kanban Board Script Viewer", 4 lines

on mouseUp
   close this stack
end mouseUp