Select2.icl 2.97 KB
Newer Older
1 2
implementation module BasicAPIExamples.Extensions.Select2

3 4
import Text

5
import iTasks
6
import iTasks.UI.Editor.Common
7 8 9
import iTasks.Extensions.Editors.Select2

wf :: String -> Workflow
10
wf a = workflow a "Try out the Select2 extension for searchable select boxes" main
11 12

main :: Task ()
13 14 15 16 17
main = ArrangeWithTabs False @>>
	allTasks
		[ select2AndNormal "Enter expression (withSelect2)" enterExpression
		, select2AndNormal "Dropdown" enterWithDropdown
		, select2AndNormal "Dropdown with groups" enterWithDropdownWithGroups
18 19 20 21 22
		, withShared fruits \shared ->
			Title "Dropdown with dynamic options" @>> (
				(Hint "Enter options:" @>> updateSharedInformation [] shared) ||-
				select2AndNormal "" (dropdownWithDynamicOptions shared)
			)
23 24 25 26 27 28 29 30 31 32
		]
	@! ()
where
	select2AndNormal title task = Title title @>> ArrangeHorizontal @>>
		(
			forever (task True "with Select2 input fields")
		-&&-
			forever (task False "with standard HTML select boxes")
		)
		@! ()
33 34 35 36 37 38 39 40

:: Expr
	= Literal Int
	| Add Expr Expr
	| Sum [Expr]

derive class iTask Expr

41 42 43 44 45 46 47
enterExpression :: !Bool !String -> Task Expr
enterExpression select2 hint =
	Hint (concat ["Enter an expression ",hint,":"]) @>>
	enterInformation [EnterUsing id editor] >>! \expr ->
	Hint "You entered:" @>>
	viewInformation [] expr >>! return
where
48
	editor = if select2 (withSelect2 True gEditor{|*|}) gEditor{|*|}
49

50 51 52
fruits =: ["Apple", "Banana", "Cherry"]
veggies =: ["Arugula", "Broccoli", "Chickpea"]

53 54 55
enterWithDropdown :: !Bool !String -> Task String
enterWithDropdown select2 hint =
	Hint (concat ["Enter a value ",hint,":"]) @>>
56
	enterInformation [EnterUsing id (choose fruits)] @ ((!!) fruits) >>! \choice ->
57 58 59 60 61 62 63 64 65
	Hint "You entered:" @>>
	viewInformation [] choice >>! return
where
	choose = if select2 chooseWithSelect2Dropdown chooseWithDropdown

enterWithDropdownWithGroups :: !Bool !String -> Task String
enterWithDropdownWithGroups select2 hint =
	Hint (concat ["Enter a value ",hint,":"]) @>>
	enterInformation [EnterUsing id editor] @ (fst o (!!) options) >>! \choice ->
66
	Hint "You entered:" @>>
67 68 69 70 71 72 73 74 75 76 77 78
	viewInformation [] choice >>! return
where
	editor = bijectEditorValue
		(\i -> [i])
		(\sel -> case sel of [x] -> x; _ -> 0)
		(withConstantChoices numberedOptions
			(if select2 select2DropdownWithGroups dropdownWithGroups))
	numberedOptions =
		[({ChoiceText | id=i, text=t}, group) \\ (t,group) <- options & i <- [0..]]
	options =
		[(opt, Just "Fruits") \\ opt <- fruits] ++
		[(opt, Just "Veggies") \\ opt <- veggies]
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

derive class iTask ChoiceText

dropdownWithDynamicOptions :: !(SimpleSDSLens [String]) !Bool !String -> Task [String]
dropdownWithDynamicOptions options select2 hint =
	ArrangeVertical @>>
	Hint (concat ["Make a choice ",hint,":"]) @>>
	editSelectionWithShared
		[ SelectUsing
			(\opts -> [{ChoiceText | id=i,text=o} \\ o <- opts & i <- [0..]])
			(\opts is -> [opts !! i \\ i <- is])
			editor
		]
		options
		(const []) >>! \choices ->
	Hint "You entered:" @>>
	viewInformation [] choices >>! return
where
	editor = if select2 select2Dropdown dropdown <<@ multipleAttr True