Skip to content

Commit

Permalink
don't normalize widget keys (close #123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Nov 18, 2014
1 parent e1c9564 commit a7a7f55
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait ComponentWidget
.getOrElse(throw XWException("Widget " + key + " is not on any tab."))
.key
def setTab(tabKey: WidgetKey): Unit =
ws.xwTabs.find(_.key == normalizeString(tabKey)) match {
ws.xwTabs.find(_.key == tabKey) match {
case None throw XWException("Tab " + tabKey + " does not exist.")
case Some(newTab)
tab match {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/uk/ac/surrey/xw/state/Reader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class Reader(
def widgetKeyVector: Vector[WidgetKey] = Vector() ++ widgetMap.keys

private def mutablePropertyMap(widgetKey: WidgetKey): Either[XWException, MutablePropertyMap] =
widgetMap.get(normalizeString(widgetKey)).orException(
widgetMap.get(widgetKey).orException(
"Widget " + widgetKey + " does not exist.")

def propertyMap(widgetKey: WidgetKey): Either[XWException, PropertyMap] =
mutablePropertyMap(widgetKey).right.map(_.toMap)

def contains(widgetKey: WidgetKey) = widgetMap.contains(normalizeString(widgetKey))
def contains(widgetKey: WidgetKey) = widgetMap.contains(widgetKey)

def propertyKeyVector(widgetKey: WidgetKey): Either[XWException, Vector[PropertyKey]] =
mutablePropertyMap(widgetKey).right.map(Vector() ++ _.keysIterator)
Expand Down
27 changes: 12 additions & 15 deletions core/src/main/scala/uk/ac/surrey/xw/state/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,19 @@ class Writer(
}

def add(widgetKey: WidgetKey, propertyMap: PropertyMap): Unit = {
val wKey = normalizeString(widgetKey)
val properties = propertyMap.normalizeKeys
reader.validateNonEmpty("widget key", wKey).rightOrThrow
reader.validateUnique("widget key", wKey).rightOrThrow
reader.validateNonEmpty("widget key", widgetKey).rightOrThrow
reader.validateUnique("widget key", widgetKey).rightOrThrow
val kind = getKind(widgetKey, properties)
val tabProperty: PropertyMap = kind match {
case _: TabKind[_] Map.empty
case _ if properties.isDefinedAt(tabPropertyKey) Map.empty
case _ Map(tabPropertyKey -> getLastTabKey)
}
val keyProperty = Map(keyPropertyKey -> wKey)
val keyProperty = Map(keyPropertyKey -> widgetKey)
val allProperties = kind.defaultValues ++ properties ++ tabProperty ++ keyProperty
widgetMap += wKey -> allProperties.asMutablePropertyMap
publish(AddWidget(wKey, allProperties))
widgetMap += widgetKey -> allProperties.asMutablePropertyMap
publish(AddWidget(widgetKey, allProperties))
}

def getKind(widgetKey: WidgetKey, propertyMap: PropertyMap): WidgetKind[_] = {
Expand Down Expand Up @@ -86,16 +85,15 @@ class Writer(
.getOrElse(throw new XWException("No widget tab has been created yet."))

def remove(widgetKey: WidgetKey): Unit = {
val wKey = normalizeString(widgetKey)
// Special case: if we're removing a tab, also
// remove the widgets on that tab from the widget map
if (reader.get(kindPropertyKey, wKey) == tabKindName) {
if (reader.get(kindPropertyKey, widgetKey) == tabKindName) {
widgetMap --= widgetMap.collect {
case (k, ps) if ps.get(tabPropertyKey) == Some(wKey) k
case (k, ps) if ps.get(tabPropertyKey) == Some(widgetKey) k
}
}
widgetMap -= wKey
publish(RemoveWidget(wKey))
widgetMap -= widgetKey
publish(RemoveWidget(widgetKey))
}

def set(
Expand All @@ -110,14 +108,13 @@ class Writer(
propertyValue: PropertyValue,
publishEvent: Boolean): Unit = {

val wKey = normalizeString(widgetKey)
val propertyMap = widgetMap.getOrElse(wKey,
throw XWException("Widget " + wKey + " does not exist."))
val propertyMap = widgetMap.getOrElse(widgetKey,
throw XWException("Widget " + widgetKey + " does not exist."))
val pKey = normalizeString(propertyKey)
val oldValue = propertyMap.get(pKey)
if (Some(propertyValue) != oldValue) {
propertyMap += pKey -> propertyValue
if (publishEvent) publish(SetProperty(wKey, pKey, propertyValue))
if (publishEvent) publish(SetProperty(widgetKey, pKey, propertyValue))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package uk.ac.surrey.xw.extension

import uk.ac.surrey.xw.api.WidgetKey
import uk.ac.surrey.xw.api.XWException
import uk.ac.surrey.xw.api.normalizeString

class WidgetContextManager {
var stack: List[WidgetKey] = List.empty
Expand All @@ -14,7 +13,7 @@ class WidgetContextManager {
| block passed to xw:create-<kind>, xw:ask or xw:of.
|""".stripMargin))
def withContext[A](key: WidgetKey)(f: () A): A = {
stack = normalizeString(key) :: stack // push
stack = key :: stack // push
val result = f()
stack = stack.tail // pop
result
Expand Down

0 comments on commit a7a7f55

Please sign in to comment.