Студопедия

Главная страница Случайная страница

КАТЕГОРИИ:

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Responding to Links, Back Buttons, and Errors






The following example is taken from the De Re Basic manual by Paul Laughton. It shows how to respond to all the possible HTML/Javascript UI options:

html.openhtml.load.url " file: ///sdcard/rfo-basic/data/htmlDemo1.html" xnextUserAction: do html.get.datalink data$until data$ < > " " type$ = left$(data$, 4)data$ = mid$(data$, 5)sw.begin type$ sw.case " BAK: " print " BACK key: " + data$ if data$ = " 1" then html.go.back else end sw.break sw.case " LNK: " print " Hyperlink selected: " + data$ html.load.url data$ sw.break sw.case " ERR: " print " Error: " + data$ sw.break sw.case " DAT: " print " User data: " + data$ if left$(data$, 4) = " Exit" print " User ended demo." html.close end endif if data$ = " STT" stt.results thelist list.get thelist, 1, thetext$ thetext$ = " You said: " + thetext$ insert$ = " javascript: text(\" " +thetext$+" \")" print insert$ html.load.url insert$ endif sw.break sw.case " FOR: " print " Form data: " +data$ end sw.break sw.case " DNL: " print " Download: " + data$ array.delete p$[] split p$[], data$, " /" array.length l, p$[] fn$ = p$[l] html.load.string " < html> Starting download of " + fn$ + " < /html> " byte.open r, f, data$ pause 2000 html.go.back byte.copy f, fn$ byte.close sw.break sw.default print " Unexpected data type: ", data$ endsw.end

10.7 Recipe Database #2 - A Little GUI Data Storage and Retrievel App

The program below is a variation of the recipe script provided in the previous section. To improve user input and data display functionality, a long string of HTML is concatenated to form the GUI interface. The replace$ function is used to dynamically place data into the text input and textarea fields, as needed during runtime. Notice that the only changed code from the original program are lines that contain print, input, and text.input functions:

! Here's the default GUI layout. It contains the characters " tttt",! " iiii", and " cccc", which will be replaced with live title, ingredient,! and instruction data as needed: gui$ = " < form action=\" form\" > " + ~ " Title: < br> " + ~ " < input type=\" text\" name=\" title\" value=\" tttt\" > < br> < br> " + ~ " Ingredients: < br> " + ~ " < textarea cols=25 name=\" ingred\" rows=5> iiii< /textarea> < br> < br> " + ~ " Instructions: < br> " + ~ " < textarea cols=25 name=\" instru\" rows=5> cccc< /textarea> < br> < br> " + ~ " < input type=\" submit\" name=\" submit\" value=\" Done\" > " + ~" < /form> "! This function is used to replace formatted characters such as spaces and! newlines in the submitted form text: fn.def pickvalue$(submit$) split submit$[], submit$, " =" value$ = submit$[2] value$ = replace$(value$, " +", " ") value$ = replace$(value$, " %0D%0A", " \n") value$ = replace$(value$, " %27", " '") fn.rtn value$fn.end! The main program starts here: file.exists b, " recipes.txt" if b=0 longstring$ = " Remove entree from " + ~ " packaging\nHeat in microwave\nCool and eat with fork" list.create s, newrecipes list.add newrecipes, " Oatmeal", " Oatmeal\nWater\nMaple Syrup" ~ " Boil water\nAdd oatmeal\nAdd maple syrup\nCool and eat" ~ " Frozen Dinner", " Frozen Entree\nFork", longstring$ list.size newrecipes, size savedata$ = " " for i = 1 to size list.get newrecipes, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endif next i text.open w, myfile, " recipes.txt" text.writeln myfile, savedata$ text.close myfileendifloadSavedRecipes: grabfile serialdata$, " recipes.txt" array.delete recipe$[] split recipe$[], serialdata$, " ###" list.create s, recipes list.add.array recipes, recipe$[] list.size recipes, size list.create s, titles for i = 1 to size step 3 list.get recipes, i, n$ list.add titles, n$ next iviewRecipe: list.search titles, " Create New Recipe...", c if c = 0 list.add titles, " Create New Recipe..." endif select indx, titles, " Select a recipe to view/edit", d if d = 1 then end list.get titles, indx, rcp$ if rcp$ = " Create New Recipe..." goto createNewRecipe endif cls s = indx * 3 - 2 list.get recipes, s, t$ list.get recipes, s+1, i$ list.get recipes, s+2, c$ gui2$ = gui$ gui2$ = replace$(gui2$, " tttt", t$) gui2$ = replace$(gui2$, " iiii", i$) gui2$ = replace$(gui2$, " cccc", c$) html.open html.load.string gui2$ do html.get.datalink data$ until data$ < > " " goto viewRecipecreateNewRecipe: gui2$ = gui$ gui2$ = replace$(gui2$, " tttt", " ") gui2$ = replace$(gui2$, " iiii", " ") gui2$ = replace$(gui2$, " cccc", " ") html.open html.load.string gui2$ do html.get.datalink data$ until data$ < > " " indx = is_in("? ", data$) data$ = mid$(data$, (indx + 1)) array.delete submitted$[] split submitted$[], data$, " & " title$ = pickvalue$(submitted$[1]) ingredients$ = pickvalue$(submitted$[2]) instructions$ = pickvalue$(submitted$[3]) list.add recipes, title$ list.add recipes, ingredients$ list.add recipes, instructions$ list.size recipes, size savedata$ = " " for i = 1 to size list.get recipes, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endif next i text.open w, myfile, " recipes.txt" text.writeln myfile, savedata$ text.close myfilegoto loadSavedRecipes

Here's the same program again with some useful parts of the code broken out into functions and subroutines. Isolating repeated sections of useful code such as this can help make your code shorter, more readable, and more reusable, as your programs become increasingly complex. You can use variations of the pickvalue$(), displayGUI$() and serialize$() functions in any program that makes use of HTML GUI forms:

gui$ = " < form action=\" form\" > " + ~ " Title: < br> " + ~ " < input type=\" text\" name=\" title\" value=\" tttt\" > < br> < br> " + ~ " Ingredients: < br> " + ~ " < textarea cols=25 name=\" ingred\" rows=5> iiii< /textarea> < br> < br> " + ~ " Instructions: < br> " + ~ " < textarea cols=25 name=\" instru\" rows=5> cccc< /textarea> < br> < br> " + ~ " < input type=\" submit\" name=\" submit\" value=\" Done\" > " + ~" < /form> " fn.def pickvalue$(submit$) split submit$[], submit$, " =" value$ = submit$[2] value$ = replace$(value$, " +", " ") value$ = replace$(value$, " %0D%0A", " \n") value$ = replace$(value$, " %27", " '") fn.rtn value$fn.endfn.def displayGUI$(tt$, ii$, cc$, g$) gui2$ = g$ gui2$ = replace$(gui2$, " tttt", tt$) gui2$ = replace$(gui2$, " iiii", ii$) gui2$ = replace$(gui2$, " cccc", cc$) html.open html.load.string gui2$ do html.get.datalink data$ until data$ < > " " fn.rtn data$fn.endfn.def serialize$(recipelist) list.size recipelist, size savedata$ = " " for i = 1 to size list.get recipelist, i, m$ if i = size savedata$ = savedata$ + m$ else savedata$ = savedata$ + m$ + " ###" endif next i text.open w, myfile, " recipes.txt" text.writeln myfile, savedata$ text.close myfilefn.endfile.exists b, " recipes.txt" if b=0 longstring$ = " Remove entree from " + ~ " packaging\nHeat in microwave\nCool and eat with fork" list.create s, newrecipes list.add newrecipes, " Oatmeal", " Oatmeal\nWater\nMaple Syrup" ~ " Boil water\nAdd oatmeal\nAdd maple syrup\nCool and eat" ~ " Frozen Dinner", " Frozen Entree\nFork", longstring$ sss$ = serialize$(newrecipes)endifloadSavedRecipes: grabfile serialdata$, " recipes.txt" array.delete recipe$[] split recipe$[], serialdata$, " ###" list.create s, recipes list.add.array recipes, recipe$[] list.size recipes, size list.create s, titles for i = 1 to size step 3 list.get recipes, i, n$ list.add titles, n$ next iviewRecipe: list.search titles, " Create New Recipe...", c if c = 0 then list.add titles, " Create New Recipe..." select indx, titles, " Select a recipe to view/edit", d if d = 1 then end list.get titles, indx, rcp$ if rcp$ = " Create New Recipe..." then goto createNewRecipe cls s = indx * 3 - 2 list.get recipes, s, t$ list.get recipes, s+1, i$ list.get recipes, s+2, c$ data$ = displayGUI$(t$, i$, c$, gui$)goto viewRecipecreateNewRecipe: data$ = displayGUI$(" ", " ", " ", gui$) indx = is_in("? ", data$) data$ = mid$(data$, (indx + 1)) array.delete submitted$[] split submitted$[], data$, " & " title$ = pickvalue$(submitted$[1]) ingredients$ = pickvalue$(submitted$[2]) instructions$ = pickvalue$(submitted$[3]) list.add recipes, title$ list.add recipes, ingredients$ list.add recipes, instructions$ sss$ = serialize$(recipes)goto loadSavedRecipes

Поделиться с друзьями:

mylektsii.su - Мои Лекции - 2015-2024 год. (0.009 сек.)Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав Пожаловаться на материал