1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.dashboard; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.component.manager.ComponentLookupException; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.rendering.block.Block; |
34 |
|
import org.xwiki.rendering.block.GroupBlock; |
35 |
|
import org.xwiki.rendering.block.MacroBlock; |
36 |
|
import org.xwiki.rendering.block.RawBlock; |
37 |
|
import org.xwiki.rendering.block.WordBlock; |
38 |
|
import org.xwiki.rendering.macro.dashboard.Gadget; |
39 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
40 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
41 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
42 |
|
import org.xwiki.rendering.syntax.Syntax; |
43 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("edit") |
54 |
|
@Singleton |
|
|
| 90% |
Uncovered Elements: 5 (50) |
Complexity: 10 |
Complexity Density: 0.28 |
|
55 |
|
public class EditableGadgetRenderer extends DefaultGadgetRenderer |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
protected static final String METADATA = "metadata"; |
62 |
|
|
63 |
|
private static final String ANNOTATED_SYNTAXES_PREFIX = "annotated"; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
@Named("annotatedxhtml/1.0") |
70 |
|
protected BlockRenderer defaultGadgetContentRenderer; |
71 |
|
|
72 |
|
@Inject |
73 |
|
protected ComponentManager componentManager; |
74 |
|
|
75 |
|
@Inject |
76 |
|
protected RenderingContext renderingContext; |
77 |
|
|
78 |
|
@Inject |
79 |
|
protected Logger logger; |
80 |
|
|
81 |
|
|
82 |
|
@return |
83 |
|
|
|
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
84 |
1 |
protected BlockRenderer getGadgetContentRenderer()... |
85 |
|
{ |
86 |
|
|
87 |
1 |
Syntax currentTargetSyntax = renderingContext.getTargetSyntax(); |
88 |
1 |
if (currentTargetSyntax == null) { |
89 |
|
|
90 |
0 |
return defaultGadgetContentRenderer; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
1 |
String annotatedTargetSyntax = currentTargetSyntax.toIdString(); |
95 |
1 |
if (!StringUtils.startsWith(annotatedTargetSyntax, ANNOTATED_SYNTAXES_PREFIX)) { |
96 |
1 |
annotatedTargetSyntax = ANNOTATED_SYNTAXES_PREFIX + annotatedTargetSyntax; |
97 |
|
} |
98 |
|
|
99 |
1 |
try { |
100 |
1 |
return componentManager.getInstance(BlockRenderer.class, annotatedTargetSyntax); |
101 |
|
} catch (ComponentLookupException e) { |
102 |
1 |
logger.warn("Failed to load the syntax [{}].", annotatedTargetSyntax); |
103 |
|
|
104 |
1 |
return defaultGadgetContentRenderer; |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
@param |
110 |
|
@return |
111 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
112 |
1 |
protected Syntax getRawBlockSyntax(BlockRenderer gadgetContentRenderer)... |
113 |
|
{ |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
1 |
return gadgetContentRenderer != defaultGadgetContentRenderer |
121 |
|
? renderingContext.getTargetSyntax() : Syntax.XHTML_1_0; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
@param |
126 |
|
@return |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 2 |
Complexity Density: 0.1 |
|
128 |
2 |
protected Block getGadgetEditMetadata(Gadget gadget)... |
129 |
|
{ |
130 |
2 |
GroupBlock metadataBlock = new GroupBlock(); |
131 |
2 |
metadataBlock.setParameter(CLASS, METADATA); |
132 |
|
|
133 |
|
|
134 |
2 |
boolean isMacro = gadget.getContent().size() == 1 && gadget.getContent().get(0) instanceof MacroBlock; |
135 |
2 |
GroupBlock isMacroBlock = new GroupBlock(); |
136 |
2 |
isMacroBlock.setParameter(CLASS, "isMacro"); |
137 |
2 |
isMacroBlock.addChild(new WordBlock(Boolean.toString(isMacro))); |
138 |
|
|
139 |
2 |
metadataBlock.addChild(isMacroBlock); |
140 |
|
|
141 |
2 |
if (isMacro) { |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
1 |
GroupBlock renderedContentBlock = new GroupBlock(); |
146 |
1 |
renderedContentBlock.setParameter(CLASS, "content"); |
147 |
1 |
WikiPrinter printer = new DefaultWikiPrinter(); |
148 |
1 |
BlockRenderer gadgetContentRenderer = getGadgetContentRenderer(); |
149 |
1 |
gadgetContentRenderer.render(gadget.getContent(), printer); |
150 |
1 |
RawBlock rawBlock = new RawBlock(printer.toString(), getRawBlockSyntax(gadgetContentRenderer)); |
151 |
1 |
renderedContentBlock.addChild(rawBlock); |
152 |
|
|
153 |
|
|
154 |
1 |
GroupBlock gadgetTitleBlock = new GroupBlock(); |
155 |
1 |
gadgetTitleBlock.setParameter(CLASS, "title"); |
156 |
|
|
157 |
1 |
gadgetTitleBlock.addChild(new WordBlock(gadget.getTitleSource())); |
158 |
|
|
159 |
1 |
metadataBlock.addChild(renderedContentBlock); |
160 |
1 |
metadataBlock.addChild(gadgetTitleBlock); |
161 |
|
} |
162 |
|
|
163 |
2 |
return metadataBlock; |
164 |
|
} |
165 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
166 |
2 |
@Override... |
167 |
|
public List<Block> decorateGadget(Gadget gadget) |
168 |
|
{ |
169 |
2 |
List<Block> viewBlock = super.decorateGadget(gadget); |
170 |
|
|
171 |
2 |
if (viewBlock.size() > 0) { |
172 |
2 |
viewBlock.get(0).addChild(getGadgetEditMetadata(gadget)); |
173 |
|
} |
174 |
|
|
175 |
2 |
return viewBlock; |
176 |
|
} |
177 |
|
} |