Skip to content

Commit 6609894

Browse files
committed
refactor(frontend): remove examples from wrong place
1 parent c00b458 commit 6609894

File tree

11 files changed

+82
-434
lines changed

11 files changed

+82
-434
lines changed

frontend/src/main/resources/config/examples.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

frontend/src/main/scala/it/unibo/scafi/js/Index.scala

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package it.unibo.scafi.js
2-
3-
import it.unibo.scafi.js.code._
2+
import upickle.default._
3+
import it.unibo.scafi.js.code.{ExampleGroup, _}
44
import it.unibo.scafi.js.controller.local
55
import it.unibo.scafi.js.controller.local._
66
import it.unibo.scafi.js.dsl.semantics._
@@ -76,32 +76,6 @@ object Index {
7676
300
7777
)
7878

79-
def buildTour(controls: SimulationControlsSection): PopoverProgression.Builder = SkeletonPage.popoverTourBuilder
80-
.addNextPopover(
81-
attachTo = controls.loadButton.id,
82-
title = "Load code",
83-
text = "Every time you edit your code and want to load it onto the network, click here ...",
84-
direction = Popover.Bottom
85-
)
86-
.addNextPopover(
87-
attachTo = controls.startButton.id,
88-
title = "Start the simulation",
89-
text = "... and then start the simulation here"
90-
)
91-
.addNextPopover(
92-
attachTo = controls.stopButton.id,
93-
title = "Stop the simulation",
94-
text = "You can stop the simulation with this button to restart it later."
95-
)
96-
.addNextPopover(
97-
attachTo = controls.tick.id,
98-
title = "Tick-by-tick progression",
99-
text = "You can also progress in the simulation tick-by-tick using this button."
100-
)
101-
// TODO add batch description
102-
// TODO add period description
103-
.andFinally(() => Cookie.store("visited", "true"))
104-
10579
def scafiInitialization(mode: EditorSection.Mode): Unit = {
10680
implicit val context: Scheduler = Execution.timeoutBasedScheduler
10781
// dynamic part configuration
@@ -129,7 +103,7 @@ object Index {
129103
// force repaint
130104
support.invalidate()
131105
SkeletonPage.visualizationSection.focus()
132-
val tour = buildTour(controls).start()
106+
val tour = Tour(controls).start()
133107
PopoverProgression.ResetButton.render(tour, SkeletonPage.navRightSide)
134108
if (!Cookie.get("visited").exists(_.toBoolean)) {
135109
val modal = welcomeModal
@@ -140,15 +114,14 @@ object Index {
140114
}
141115
modal.show()
142116
}
143-
144117
PageBus.publish(configuration) // tell to all component the new configuration installed on the frontend
145-
val example = Seq(BasicExamples(), LibraryExamples(), MatrixLedExample(), MovementExamples(), HighLevelExamples())
146-
// PageStructure.static()
147118
PageStructure.resizable()
148119
if (mode == EditorSection.JavascriptMode) {
149120
editor.setCode("", mode)
150121
} else {
151-
val exampleChooser = new ExampleChooser(SkeletonPage.selectionProgram, example, configurationSection, editor)
122+
ExampleProvider
123+
.race(ExampleProvider.fromGlobal(), ExampleProvider.fromRemote())
124+
.foreach(examples => new ExampleChooser(SkeletonPage.selectionProgram, examples, configurationSection, editor))
152125
}
153126
}
154127
@JSExportTopLevel("ScafiBackend")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package it.unibo.scafi.js
2+
3+
import it.unibo.scafi.js.utils.Cookie
4+
import it.unibo.scafi.js.view.dynamic.{Popover, PopoverProgression, SimulationControlsSection}
5+
import it.unibo.scafi.js.view.static.SkeletonPage
6+
7+
object Tour {
8+
def apply(controls: SimulationControlsSection): PopoverProgression.Builder = SkeletonPage.popoverTourBuilder
9+
.addNextPopover(
10+
attachTo = controls.loadButton.id,
11+
title = "Load code",
12+
text = "Every time you edit your code and want to load it onto the network, click here ...",
13+
direction = Popover.Bottom
14+
)
15+
.addNextPopover(
16+
attachTo = controls.startButton.id,
17+
title = "Start the simulation",
18+
text = "... and then start the simulation here"
19+
)
20+
.addNextPopover(
21+
attachTo = controls.stopButton.id,
22+
title = "Stop the simulation",
23+
text = "You can stop the simulation with this button to restart it later."
24+
)
25+
.addNextPopover(
26+
attachTo = controls.tick.id,
27+
title = "Tick-by-tick progression",
28+
text = "You can also progress in the simulation tick-by-tick using this button."
29+
)
30+
// TODO add batch description
31+
// TODO add period description
32+
.andFinally(() => Cookie.store("visited", "true"))
33+
}

frontend/src/main/scala/it/unibo/scafi/js/code/BasicExamples.scala

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
package it.unibo.scafi.js.code
22

3+
import upickle.default._
4+
35
case class ExampleGroup(groupName: String, examples: Seq[Example])
6+
object ExampleGroup {
7+
implicit def exampleGroupRW: ReadWriter[ExampleGroup] = macroRW[ExampleGroup]
8+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package it.unibo.scafi.js.code
2+
3+
import it.unibo.scafi.js.utils.GlobalStore
4+
import org.scalajs.dom.ext.Ajax
5+
6+
import scala.concurrent.{ExecutionContext, Future}
7+
import upickle.default._
8+
9+
import scala.util.Success
10+
11+
trait ExampleProvider {
12+
def getExamples: Future[Seq[ExampleGroup]]
13+
}
14+
15+
object ExampleProvider {
16+
private val storeKey = "examples"
17+
/** Combine provider by requesting the example in order. the first provider that returns a sequence of example
18+
* provider successfully complete the future
19+
* @param providers
20+
* the sequence of provider that tries to load examples
21+
*/
22+
def race(providers: ExampleProvider*)(implicit context: ExecutionContext): Future[Seq[ExampleGroup]] =
23+
providers.map(_.getExamples).reduce((resultingFuture, current) => resultingFuture.recoverWith { case _ => current })
24+
25+
def fromRemote()(implicit context: ExecutionContext): ExampleProvider = new ExampleProvider {
26+
override def getExamples: Future[Seq[ExampleGroup]] = Ajax
27+
.get("/config/examples.json")
28+
.map(_.responseText)
29+
.andThen { case Success(value) => GlobalStore.put(storeKey, value) }
30+
.map(result => read[Seq[ExampleGroup]](result))
31+
}
32+
33+
def fromGlobal()(implicit context: ExecutionContext): ExampleProvider = new ExampleProvider {
34+
override def getExamples: Future[Seq[ExampleGroup]] =
35+
Future.fromTry(GlobalStore.get[String](storeKey).map(data => read[Seq[ExampleGroup]](data)))
36+
}
37+
}

frontend/src/main/scala/it/unibo/scafi/js/code/HighLevelExamples.scala

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)