nitweb: introduce ModelAction
[nit.git] / src / web / web_actions.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Nitcorn actions used by the nitweb server.
16 module web_actions
17
18 import web_views
19 import model::model_collect
20
21 # Display the tree of all loaded mentities.
22 class TreeAction
23 super ModelAction
24
25 # View to render.
26 var view = new HtmlHomePage(model) is lazy
27
28 redef fun answer(request, url) do return render_view(view)
29 end
30
31 # Display the list of mentities matching `namespace`.
32 class SearchAction
33 super ModelAction
34
35 # TODO handle more than full namespaces.
36 redef fun answer(request, url) do
37 var namespace = request.param("namespace")
38 if namespace == null or namespace.is_empty then
39 return render_error(400, "Missing :namespace.")
40 end
41 var mentities = model.collect_by_namespace(namespace)
42 if request.is_json_asked then
43 var json = new JsonArray
44 for mentity in mentities do
45 json.add mentity.to_json
46 end
47 return render_json(json)
48 end
49 var view = new HtmlResultPage(namespace, mentities)
50 return render_view(view)
51 end
52 end
53
54 # Display a MEntity source code.
55 class CodeAction
56 super ModelAction
57
58 # Modelbuilder used to access sources.
59 var modelbuilder: ModelBuilder
60
61 # TODO handle more than full namespaces.
62 redef fun answer(request, url) do
63 var namespace = request.param("namespace")
64 if namespace == null or namespace.is_empty then
65 return render_error(400, "Missing :namespace.")
66 end
67 var mentities = model.collect_by_namespace(namespace)
68 if mentities.is_empty then
69 return render_error(404, "No mentity matching this namespace.")
70 end
71 var view = new HtmlSourcePage(modelbuilder, mentities.first)
72 return render_view(view)
73 end
74 end
75
76 # Display the doc of a MEntity.
77 class DocAction
78 super ModelAction
79
80 # Modelbuilder used to access sources.
81 var modelbuilder: ModelBuilder
82
83 # TODO handle more than full namespaces.
84 redef fun answer(request, url) do
85 var namespace = request.param("namespace")
86 if namespace == null or namespace.is_empty then
87 return render_error(400, "Missing :namespace.")
88 end
89 var mentities = model.collect_by_namespace(namespace)
90 if mentities.is_empty then
91 return render_error(404, "No mentity matching this namespace.")
92 end
93 var view = new HtmlDocPage(modelbuilder, mentities.first)
94 return render_view(view)
95 end
96 end
97
98 # Return a random list of MEntities.
99 class RandomAction
100 super ModelAction
101
102 # TODO handle more than full namespaces.
103 redef fun answer(request, url) do
104 var n = request.int_arg("n") or else 10
105 var k = request.string_arg("k") or else "modules"
106 var mentities: Array[MEntity]
107 if k == "modules" then
108 mentities = model.mmodules.to_a
109 else if k == "classdefs" then
110 mentities = new Array[MClassDef]
111 for mclass in model.mclasses do
112 mentities.add_all(mclass.mclassdefs)
113 end
114 else
115 mentities = new Array[MPropDef]
116 for mprop in model.mproperties do
117 mentities.add_all(mprop.mpropdefs)
118 end
119 end
120 mentities.shuffle
121 mentities = mentities.sub(0, n)
122 if request.is_json_asked then
123 var json = new JsonArray
124 for mentity in mentities do
125 json.add mentity.to_json
126 end
127 return render_json(json)
128 end
129 var view = new HtmlResultPage("random", mentities)
130 return render_view(view)
131 end
132 end
133
134 redef class MEntity
135
136 # Return `self` as a JsonObject.
137 fun to_json: JsonObject do
138 var obj = new JsonObject
139 obj["name"] = html_name
140 obj["namespace"] = html_raw_namespace
141 var mdoc = self.mdoc
142 if mdoc != null then
143 obj["synopsis"] = mdoc.content.first.html_escape
144 obj["mdoc"] = mdoc.content.join("\n").html_escape
145 end
146 return obj
147 end
148 end