nitweb: introduce web actions
[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 NitAction
24
25 # Model to explore and render.
26 var model: Model
27
28 # View to render.
29 var view = new HtmlHomePage(model) is lazy
30
31 redef fun answer(request, url) do return render_view(view)
32 end
33
34 # Display the list of mentities matching `namespace`.
35 class SearchAction
36 super NitAction
37
38 # Model to explore and render.
39 var model: Model
40
41 # TODO handle more than full namespaces.
42 redef fun answer(request, url) do
43 var namespace = request.param("namespace")
44 if namespace == null or namespace.is_empty then
45 return render_error(400, "Missing :namespace.")
46 end
47 var mentities = model.collect_by_namespace(namespace)
48 if request.is_json_asked then
49 var json = new JsonArray
50 for mentity in mentities do
51 json.add mentity.to_json
52 end
53 return render_json(json)
54 end
55 var view = new HtmlResultPage(namespace, mentities)
56 return render_view(view)
57 end
58 end
59
60 # Display a MEntity source code.
61 class CodeAction
62 super NitAction
63
64 # Model to explore and render.
65 var model: Model
66
67 # Modelbuilder used to access sources.
68 var modelbuilder: ModelBuilder
69
70 # TODO handle more than full namespaces.
71 redef fun answer(request, url) do
72 var namespace = request.param("namespace")
73 if namespace == null or namespace.is_empty then
74 return render_error(400, "Missing :namespace.")
75 end
76 var mentities = model.collect_by_namespace(namespace)
77 if mentities.is_empty then
78 return render_error(404, "No mentity matching this namespace.")
79 end
80 var view = new HtmlSourcePage(modelbuilder, mentities.first)
81 return render_view(view)
82 end
83 end
84
85 # Display the doc of a MEntity.
86 class DocAction
87 super NitAction
88
89 # Model to explore and render.
90 var model: Model
91
92 # Modelbuilder used to access sources.
93 var modelbuilder: ModelBuilder
94
95 # TODO handle more than full namespaces.
96 redef fun answer(request, url) do
97 var namespace = request.param("namespace")
98 if namespace == null or namespace.is_empty then
99 return render_error(400, "Missing :namespace.")
100 end
101 var mentities = model.collect_by_namespace(namespace)
102 if mentities.is_empty then
103 return render_error(404, "No mentity matching this namespace.")
104 end
105 var view = new HtmlDocPage(modelbuilder, mentities.first)
106 return render_view(view)
107 end
108 end
109
110 # Return a random list of MEntities.
111 class RandomAction
112 super NitAction
113
114 # Model to explore and render.
115 var model: Model
116
117 # TODO handle more than full namespaces.
118 redef fun answer(request, url) do
119 var n = request.int_arg("n") or else 10
120 var k = request.string_arg("k") or else "modules"
121 var mentities: Array[MEntity]
122 if k == "modules" then
123 mentities = model.mmodules.to_a
124 else if k == "classdefs" then
125 mentities = new Array[MClassDef]
126 for mclass in model.mclasses do
127 mentities.add_all(mclass.mclassdefs)
128 end
129 else
130 mentities = new Array[MPropDef]
131 for mprop in model.mproperties do
132 mentities.add_all(mprop.mpropdefs)
133 end
134 end
135 mentities.shuffle
136 mentities = mentities.sub(0, n)
137 if request.is_json_asked then
138 var json = new JsonArray
139 for mentity in mentities do
140 json.add mentity.to_json
141 end
142 return render_json(json)
143 end
144 var view = new HtmlResultPage("random", mentities)
145 return render_view(view)
146 end
147 end
148
149 redef class MEntity
150
151 # Return `self` as a JsonObject.
152 fun to_json: JsonObject do
153 var obj = new JsonObject
154 obj["name"] = html_name
155 obj["namespace"] = html_raw_namespace
156 var mdoc = self.mdoc
157 if mdoc != null then
158 obj["synopsis"] = mdoc.content.first.html_escape
159 obj["mdoc"] = mdoc.content.join("\n").html_escape
160 end
161 return obj
162 end
163 end