nitweb: rename ModelAction intro ModelHandler
[nit.git] / src / web / web_base.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 # Base classes used by `nitweb`.
16 module web_base
17
18 import model::model_views
19 import model::model_json
20 import doc_down
21 import popcorn
22
23 # Specific nitcorn Action that uses a Model
24 class ModelHandler
25 super Handler
26
27 # Model to use.
28 var model: Model
29
30 # MModule used to flatten model.
31 var mainmodule: MModule
32
33 # Find the MEntity ` with `full_name`.
34 fun find_mentity(model: ModelView, full_name: nullable String): nullable MEntity do
35 if full_name == null then return null
36 return model.mentity_by_full_name(full_name.from_percent_encoding)
37 end
38
39 # Init the model view from the `req` uri parameters.
40 fun init_model_view(req: HttpRequest): ModelView do
41 var view = new ModelView(model)
42 var show_private = req.bool_arg("private") or else false
43 if not show_private then view.min_visibility = protected_visibility
44
45 view.include_fictive = req.bool_arg("fictive") or else false
46 view.include_empty_doc = req.bool_arg("empty-doc") or else true
47 view.include_test_suite = req.bool_arg("test-suite") or else false
48 view.include_attribute = req.bool_arg("attributes") or else true
49
50 return view
51 end
52 end
53
54 # A NitView is rendered by an action.
55 interface NitView
56 # Renders this view and returns something that can be written to a HTTP response.
57 fun render: Writable is abstract
58 end
59
60 redef class HttpResponse
61 # Render a NitView as response.
62 fun send_view(view: NitView, status: nullable Int) do send(view.render, status)
63 end
64
65 redef class HttpRequest
66 # Does the client asked for a json formatted response?
67 #
68 # Checks the URL get parameter `?json=true`.
69 fun is_json_asked: Bool do return bool_arg("json") or else false
70 end
71
72 redef class MEntity
73
74 # URL to `self` within the web interface.
75 fun web_url: String is abstract
76
77 # URL to `self` within the JSON api.
78 fun api_url: String do return "/api/entity/" / full_name
79
80 redef fun json do
81 var obj = super
82 obj["web_url"] = web_url
83 obj["api_url"] = api_url
84 return obj
85 end
86
87 # Get the full json repesentation of `self` with MEntityRefs resolved.
88 fun api_json(handler: ModelHandler): JsonObject do return json
89 end
90
91 redef class MEntityRef
92 redef fun json do
93 var obj = super
94 obj["web_url"] = mentity.web_url
95 obj["api_url"] = mentity.api_url
96 obj["name"] = mentity.name
97 obj["mdoc"] = mentity.mdoc_or_fallback
98 obj["visibility"] = mentity.visibility
99 obj["location"] = mentity.location
100 var modifiers = new JsonArray
101 for modifier in mentity.collect_modifiers do
102 modifiers.add modifier
103 end
104 obj["modifiers"] = modifiers
105 return obj
106 end
107 end
108
109 redef class MDoc
110
111 # Add doc down processing
112 redef fun json do
113 var obj = super
114 obj["synopsis"] = synopsis
115 obj["documentation"] = documentation
116 obj["comment"] = comment
117 obj["html_synopsis"] = html_synopsis.write_to_string
118 obj["html_documentation"] = html_documentation.write_to_string
119 obj["html_comment"] = html_comment.write_to_string
120 return obj
121 end
122 end
123
124 redef class MPackage
125 redef var web_url = "/package/{full_name}" is lazy
126 end
127
128 redef class MGroup
129 redef var web_url = "/group/{full_name}" is lazy
130 end
131
132 redef class MModule
133 redef var web_url = "/module/{full_name}" is lazy
134
135 redef fun api_json(handler) do
136 var obj = super
137 obj["intro_mclassdefs"] = to_mentity_refs(collect_intro_mclassdefs(private_view))
138 obj["redef_mclassdefs"] = to_mentity_refs(collect_redef_mclassdefs(private_view))
139 obj["imports"] = to_mentity_refs(in_importation.direct_greaters)
140 return obj
141 end
142 end
143
144 redef class MClass
145 redef var web_url = "/class/{full_name}" is lazy
146
147 redef fun api_json(handler) do
148 var obj = super
149 obj["all_mproperties"] = to_mentity_refs(collect_accessible_mproperties(private_view))
150 obj["intro_mproperties"] = to_mentity_refs(collect_intro_mproperties(private_view))
151 obj["redef_mproperties"] = to_mentity_refs(collect_redef_mproperties(private_view))
152 var poset = hierarchy_poset(handler.mainmodule, private_view)
153 obj["parents"] = to_mentity_refs(poset[self].direct_greaters)
154 return obj
155 end
156 end
157
158 redef class MClassDef
159 redef var web_url = "/classdef/{full_name}" is lazy
160
161 redef fun json do
162 var obj = super
163 obj["intro"] = to_mentity_ref(mclass.intro)
164 obj["mpackage"] = to_mentity_ref(mmodule.mpackage)
165 return obj
166 end
167
168 redef fun api_json(handler) do
169 var obj = super
170 obj["intro_mpropdefs"] = to_mentity_refs(collect_intro_mpropdefs(private_view))
171 obj["redef_mpropdefs"] = to_mentity_refs(collect_redef_mpropdefs(private_view))
172 return obj
173 end
174 end
175
176 redef class MProperty
177 redef var web_url = "/property/{full_name}" is lazy
178
179 redef fun json do
180 var obj = super
181 obj["intro_mclass"] = to_mentity_ref(intro_mclassdef.mclass)
182 obj["mpackage"] = to_mentity_ref(intro_mclassdef.mmodule.mpackage)
183 return obj
184 end
185 end
186
187 redef class MPropDef
188 redef var web_url = "/propdef/{full_name}" is lazy
189
190 redef fun json do
191 var obj = super
192 obj["intro"] = to_mentity_ref(mproperty.intro)
193 obj["intro_mclassdef"] = to_mentity_ref(mproperty.intro.mclassdef)
194 obj["mmodule"] = to_mentity_ref(mclassdef.mmodule)
195 obj["mgroup"] = to_mentity_ref(mclassdef.mmodule.mgroup)
196 obj["mpackage"] = to_mentity_ref(mclassdef.mmodule.mpackage)
197 return obj
198 end
199 end
200
201 redef class MClassType
202 redef var web_url = mclass.web_url is lazy
203 end
204
205 redef class MNullableType
206 redef var web_url = mtype.web_url is lazy
207 end
208
209 redef class MParameterType
210 redef var web_url = mclass.web_url is lazy
211 end
212
213 redef class MVirtualType
214 redef var web_url = mproperty.web_url is lazy
215 end