Skip to content
This repository
tag: v277
Fetching contributors…

Cannot retrieve contributors at this time

file 103 lines (87 sloc) 2.914 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

type Themes.state = stringmap(stringmap(finite_single_thread_lazy(string)))

Themes = {{

  @private
  dir = "stdlib/themes/"

  @private
  default_theme_name = "default"

  @private @server
  themes = Mutable.make(Map.empty : Themes.state)

  @private
  user_theme = UserContext.make(default_theme_name)

  /**
* Used to declare a new theme
*/
  load_theme(theme_name,map_resource) =
    gen_dyn_res(resource)=
      FiniteSingleThreadLazy.make(->
        parameters = {expiration={none}
                      consumption={unlimited}
                      visibility={shared}}
        config = {prefix=some(theme_name) sufix=none onaccess=none}
        DynamicResource.publish_extend(resource, parameters, config)
      )
    map_resource = Map.fold(k,v,map ->
      name = String.replace_first("{dir}{theme_name}/", "", k)
      Map.add(name,gen_dyn_res(v),map)
    ,map_resource,Map.empty)
    map = themes.get()
    themes.set(Map.add(theme_name, map_resource, map) )


  /**
* Get the list of loaded theme
*/
  @server
  get_theme_list() : list(string) =
    Map.To.key_list(themes.get())

  /**
* Change the theme for the current user only
*/
  @server
  set(name) =
    map = themes.get()
    if Map.mem(name, map)
    then UserContext.change(_ -> name, user_theme)
    else Log.info("THEME","theme '{name}' it not loaded yet, cannot be default")

  /**
* Set the default themes
*/
  @server
  set_default(name) =
    do Log.info("THEME", "set_default_theme to {name}" )
    map = themes.get()
    if Map.mem(name, map)
    then UserContext.set_default(name, user_theme)
    else Log.info("THEME","theme '{name}' it not loaded yet, cannot be default")


  /**
* Get the url of a resource with the right theme
*/
  get_resource_url(resource_name)=
    theme_name = UserContext.execute(x->x,user_theme)
    do Log.info("THEME", "current_theme: {theme_name}" )
    rec with_theme(theme_name) =
      map = themes.get()
      match Map.get(theme_name,map) with
        | {some=theme} ->
          match Map.get(resource_name, theme) with
            | {some=lazy} -> FiniteSingleThreadLazy.force(lazy)
            | {none} ->
              if theme_name == default_theme_name
              then bad_resource_url(resource_name,"resource not found for theme {theme_name}")
              else with_theme(default_theme_name)
          end
        | {none} ->
          if theme_name == default_theme_name
          then bad_resource_url(resource_name, "theme not found")
          else with_theme(default_theme_name)
      end
    with_theme(theme_name)

  @private
  dummy_page = Resource.raw_text("")

  @private
  fake_url = DynamicResource.publish(
              dummy_page,
              {consumption={unlimited}; expiration={none}; visibility={shared}})

  @private
  bad_resource_url(resource_name, err) =
    do Log.warning("THEME", "{resource_name} - {err}")
    fake_url
}}
Something went wrong with that request. Please try again.