nitweb: use ModelView instead of model collect
[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
20 # Display the tree of all loaded mentities.
21 class TreeAction
22 super ModelAction
23
24 redef fun answer(request, url) do
25 var model = init_model_view(request)
26 var view = new HtmlHomePage(model.model)
27 return render_view(view)
28 end
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 model = init_model_view(request)
42 var mentities = model.mentities_by_namespace(namespace)
43 if request.is_json_asked then
44 var json = new JsonArray
45 for mentity in mentities do
46 json.add mentity.to_json
47 end
48 return render_json(json)
49 end
50 var view = new HtmlResultPage(namespace, mentities)
51 return render_view(view)
52 end
53 end
54
55 # Display a MEntity source code.
56 class CodeAction
57 super ModelAction
58
59 # Modelbuilder used to access sources.
60 var modelbuilder: ModelBuilder
61
62 # TODO handle more than full namespaces.
63 redef fun answer(request, url) do
64 var namespace = request.param("namespace")
65 if namespace == null or namespace.is_empty then
66 return render_error(400, "Missing :namespace.")
67 end
68 var model = init_model_view(request)
69 var mentities = model.mentities_by_namespace(namespace)
70 if mentities.is_empty then
71 return render_error(404, "No mentity matching this namespace.")
72 end
73 var view = new HtmlSourcePage(modelbuilder, mentities.first)
74 return render_view(view)
75 end
76 end
77
78 # Display the doc of a MEntity.
79 class DocAction
80 super ModelAction
81
82 # Modelbuilder used to access sources.
83 var modelbuilder: ModelBuilder
84
85 # TODO handle more than full namespaces.
86 redef fun answer(request, url) do
87 var namespace = request.param("namespace")
88 if namespace == null or namespace.is_empty then
89 return render_error(400, "Missing :namespace.")
90 end
91 var model = init_model_view(request)
92 var mentities = model.mentities_by_namespace(namespace)
93 if mentities.is_empty then
94 return render_error(404, "No mentity matching this namespace.")
95 end
96 var view = new HtmlDocPage(modelbuilder, mentities.first)
97 return render_view(view)
98 end
99 end
100
101 # Return a random list of MEntities.
102 class RandomAction
103 super ModelAction
104
105 # TODO handle more than full namespaces.
106 redef fun answer(request, url) do
107 var n = request.int_arg("n") or else 10
108 var k = request.string_arg("k") or else "modules"
109 var model = init_model_view(request)
110 var mentities: Array[MEntity]
111 if k == "modules" then
112 mentities = model.mmodules.to_a
113 else if k == "classdefs" then
114 mentities = model.mclassdefs.to_a
115 else
116 mentities = model.mpropdefs.to_a
117 end
118 mentities.shuffle
119 mentities = mentities.sub(0, n)
120 if request.is_json_asked then
121 var json = new JsonArray
122 for mentity in mentities do
123 json.add mentity.to_json
124 end
125 return render_json(json)
126 end
127 var view = new HtmlResultPage("random", mentities)
128 return render_view(view)
129 end
130 end
131
132 redef class MEntity
133
134 # Return `self` as a JsonObject.
135 fun to_json: JsonObject do
136 var obj = new JsonObject
137 obj["name"] = html_name
138 obj["namespace"] = html_raw_namespace
139 var mdoc = self.mdoc
140 if mdoc != null then
141 obj["synopsis"] = mdoc.content.first.html_escape
142 obj["mdoc"] = mdoc.content.join("\n").html_escape
143 end
144 return obj
145 end
146 end