Skip to content
This repository
tag: v1869
Fetching contributors…

Cannot retrieve contributors at this time

file 112 lines (94 sloc) 3.395 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 104 105 106 107 108 109 110 111 112
/*
Copyright © 2011, 2012 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/

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 randomize=true }
        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
  bad_resource_url(resource_name, err) =
    do Log.warning("THEME", "{resource_name} - {err}")
    "/_internal_/null"
}}
Something went wrong with that request. Please try again.