src/web: replace adhoc MEntity::to_json by the model_json one
[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 uml
20
21 # Display the tree of all loaded mentities.
22 class TreeAction
23 super ModelAction
24
25 redef fun answer(request, url) do
26 var model = init_model_view(request)
27 var view = new HtmlHomePage(model.to_tree)
28 return render_view(view)
29 end
30 end
31
32 # Display the list of mentities matching `namespace`.
33 class SearchAction
34 super ModelAction
35
36 # TODO handle more than full namespaces.
37 redef fun answer(request, url) do
38 var namespace = request.param("namespace")
39 var model = init_model_view(request)
40 var mentity = find_mentity(model, namespace)
41 if mentity == null then
42 return render_error(404, "No mentity found")
43 end
44 if request.is_json_asked then
45 return render_json(mentity.to_json)
46 end
47 var view = new HtmlResultPage(namespace or else "null", [mentity])
48 return render_view(view)
49 end
50 end
51
52 # Display a MEntity source code.
53 class CodeAction
54 super ModelAction
55
56 # Modelbuilder used to access sources.
57 var modelbuilder: ModelBuilder
58
59 redef fun answer(request, url) do
60 var namespace = request.param("namespace")
61 var model = init_model_view(request)
62 var mentity = find_mentity(model, namespace)
63 if mentity == null then
64 return render_error(404, "No mentity found")
65 end
66 var view = new HtmlSourcePage(modelbuilder, mentity)
67 return render_view(view)
68 end
69 end
70
71 # Display the doc of a MEntity.
72 class DocAction
73 super ModelAction
74
75 # Modelbuilder used to access sources.
76 var modelbuilder: ModelBuilder
77
78 # TODO handle more than full namespaces.
79 redef fun answer(request, url) do
80 var namespace = request.param("namespace")
81 var model = init_model_view(request)
82 var mentity = find_mentity(model, namespace)
83 if mentity == null then
84 return render_error(404, "No mentity found")
85 end
86 var view = new HtmlDocPage(modelbuilder, mentity)
87 return render_view(view)
88 end
89 end
90
91 # Return an UML diagram for `namespace`.
92 class UMLDiagramAction
93 super ModelAction
94
95 # Mainmodule used for hierarchy flattening.
96 var mainmodule: MModule
97
98 redef fun answer(request, url) do
99 var namespace = request.param("namespace")
100 var model = init_model_view(request)
101 var mentity = find_mentity(model, namespace)
102 if mentity == null then
103 return render_error(404, "No mentity found")
104 end
105
106 var dot
107 if mentity isa MClassDef then mentity = mentity.mclass
108 if mentity isa MClass then
109 var uml = new UMLModel(model, mainmodule)
110 dot = uml.generate_class_uml.write_to_string
111 else if mentity isa MModule then
112 var uml = new UMLModel(model, mentity)
113 dot = uml.generate_package_uml.write_to_string
114 else
115 return render_error(404, "No diagram matching this namespace.")
116 end
117 var view = new HtmlDotPage(dot, mentity.as(not null).html_name)
118 return render_view(view)
119 end
120 end
121
122 # Return a random list of MEntities.
123 class RandomAction
124 super ModelAction
125
126 # TODO handle more than full namespaces.
127 redef fun answer(request, url) do
128 var n = request.int_arg("n") or else 10
129 var k = request.string_arg("k") or else "modules"
130 var model = init_model_view(request)
131 var mentities: Array[MEntity]
132 if k == "modules" then
133 mentities = model.mmodules.to_a
134 else if k == "classdefs" then
135 mentities = model.mclassdefs.to_a
136 else
137 mentities = model.mpropdefs.to_a
138 end
139 mentities.shuffle
140 mentities = mentities.sub(0, n)
141 if request.is_json_asked then
142 var json = new JsonArray
143 for mentity in mentities do
144 json.add mentity.to_json
145 end
146 return render_json(json)
147 end
148 var view = new HtmlResultPage("random", mentities)
149 return render_view(view)
150 end
151 end