1
1
using Microsoft . AspNetCore . Razor . TagHelpers ;
2
- using Our . Umbraco . TagHelpers . Enums ;
2
+ using Our . Umbraco . TagHelpers . Extensions ;
3
3
using Our . Umbraco . TagHelpers . Services ;
4
- using System . Linq ;
5
4
using System . Text ;
6
5
7
6
namespace Our . Umbraco . TagHelpers
@@ -11,7 +10,7 @@ namespace Our.Umbraco.TagHelpers
11
10
/// then an edit link will display on the front end of the site. This will
12
11
/// take you to the umbraco backoffice to edit the current page.
13
12
/// </summary>
14
- [ HtmlTargetElement ( "our-editlink " ) ]
13
+ [ HtmlTargetElement ( "our-edit-link " ) ]
15
14
public class EditLinkTagHelper : TagHelper
16
15
{
17
16
private readonly IBackofficeUserAccessor _backofficeUserAccessor ;
@@ -28,150 +27,99 @@ public EditLinkTagHelper(IBackofficeUserAccessor backofficeUserAccessor)
28
27
public int ContentId { get ; set ; } = 0 ;
29
28
30
29
/// <summary>
31
- /// An enum to say which corner of the screen you would like
32
- /// the edit link to show. Defaults to bottom left.
33
- /// </summary>
34
- [ HtmlAttributeName ( "position" ) ]
35
- public EditLinkPosition Position { get ; set ; } = EditLinkPosition . BottomLeft ;
36
-
37
- /// <summary>
38
- /// A bool to say whether or not you would like to apply the inline link styles. Defaults to true.
39
- /// </summary>
40
- [ HtmlAttributeName ( "apply-inline-link-styles" ) ]
41
- public bool ApplyInlineLinkStyles { get ; set ; } = true ;
42
-
43
- /// <summary>
44
- /// The 'Edit' text in the link. Defaults to "Edit"
45
- /// </summary>
46
- [ HtmlAttributeName ( "edit-message" ) ]
47
- public string EditMessage { get ; set ; } = "Edit" ;
48
-
49
- /// <summary>
50
- /// The CSS colour of the link text. Defaults to #fff
51
- /// </summary>
52
- [ HtmlAttributeName ( "link-colour" ) ]
53
- public string LinkColour { get ; set ; } = "#fff" ;
54
-
55
- /// <summary>
56
- /// The CSS colour of the link background. Defaults to "#1b264f"
57
- /// </summary>
58
- [ HtmlAttributeName ( "link-background-colour" ) ]
59
- public string LinkBackgroundColour { get ; set ; } = "#1b264f" ;
60
-
61
- /// <summary>
62
- /// The font size of the link text in pixels. Defaults to 16
63
- /// </summary>
64
- [ HtmlAttributeName ( "font-size" ) ]
65
- public int FontSize { get ; set ; } = 16 ;
66
-
67
- /// <summary>
68
- /// The padding around the link in pixels. Defaults to 10
69
- /// </summary>
70
- [ HtmlAttributeName ( "link-padding" ) ]
71
- public int LinkPadding { get ; set ; } = 10 ;
72
-
73
- /// <summary>
74
- /// The border radius of the link in pixels. Defaults to 6
75
- /// </summary>
76
- [ HtmlAttributeName ( "border-radius" ) ]
77
- public int BorderRadius { get ; set ; } = 6 ;
78
-
79
- /// <summary>
80
- /// The class you would like to add to the link. Defaults to "edit-link-inner"
30
+ /// Override the umbraco edit content url if yours is different
81
31
/// </summary>
82
- [ HtmlAttributeName ( "link-class-name " ) ]
83
- public string LinkClassName { get ; set ; } = "edit-link-inner " ;
32
+ [ HtmlAttributeName ( "edit-url " ) ]
33
+ public string EditUrl { get ; set ; } = "/umbraco#/content/content/ edit/ " ;
84
34
85
35
/// <summary>
86
- /// Whether or not you would like to apply the inline styles for the outer element. Defaults to true
36
+ /// Override the text of the link
87
37
/// </summary>
88
- [ HtmlAttributeName ( "apply-inline-outer-element-styles " ) ]
89
- public bool ApplyInlineOuterElementStyles { get ; set ; } = true ;
38
+ [ HtmlAttributeName ( "text " ) ]
39
+ public string Text { get ; set ; } = "Edit" ;
90
40
91
41
/// <summary>
92
- /// The margin around the link. Defaults to 10
42
+ /// A boolean to say whether or not you would like to use the default styling.
93
43
/// </summary>
94
- [ HtmlAttributeName ( "margin " ) ]
95
- public int Margin { get ; set ; } = 10 ;
44
+ [ HtmlAttributeName ( "use-default-styles " ) ]
45
+ public bool UseDefaultStyles { get ; set ; } = false ;
96
46
97
47
/// <summary>
98
- /// The zindex of this link block. Defaults to 10000
48
+ /// Set the id attribute if you want
99
49
/// </summary>
100
- [ HtmlAttributeName ( "zindex " ) ]
101
- public int Zindex { get ; set ; } = 10000 ;
50
+ [ HtmlAttributeName ( "id " ) ]
51
+ public string Id { get ; set ; } = "" ;
102
52
103
53
/// <summary>
104
- /// Override the umbraco edit content url if yours is different
54
+ /// The class attribute for the link
105
55
/// </summary>
106
- [ HtmlAttributeName ( "umbraco-edit-content-url " ) ]
107
- public string UmbracoEditContentUrl { get ; set ; } = "/umbraco#/content/content/edit/ " ;
56
+ [ HtmlAttributeName ( "class " ) ]
57
+ public string Class { get ; set ; } = "" ;
108
58
109
59
/// <summary>
110
- /// The class name for the outer element . Defaults to "edit-link-outer"
60
+ /// Set the target attribute if you want . Defaults to _blank
111
61
/// </summary>
112
- [ HtmlAttributeName ( "outer-class-name " ) ]
113
- public string OuterClassName { get ; set ; } = "edit-link-outer " ;
62
+ [ HtmlAttributeName ( "target " ) ]
63
+ public string Target { get ; set ; } = "_blank " ;
114
64
115
65
/// <summary>
116
- /// The CSS position for the outer element. Defaults to "fixed"
66
+ /// Add some inline styles to the link if you want
117
67
/// </summary>
118
- [ HtmlAttributeName ( "outer-position " ) ]
119
- public string OuterPosition { get ; set ; } = "fixed " ;
68
+ [ HtmlAttributeName ( "style " ) ]
69
+ public string Style { get ; set ; } = "" ;
120
70
121
71
/// <summary>
122
- /// The CSS position for the link. Defaults to "absolute"
72
+ /// Set the title attribute if you want
123
73
/// </summary>
124
- [ HtmlAttributeName ( "link-position" ) ]
125
- public string LinkPosition { get ; set ; } = "absolute" ;
126
-
74
+ [ HtmlAttributeName ( "title" ) ]
75
+ public string Title { get ; set ; } = "" ;
127
76
128
77
public override void Process ( TagHelperContext context , TagHelperOutput output )
129
78
{
79
+ //don't output the tag name as it's not valid HTML
130
80
output . TagName = "" ;
131
81
132
- if ( _backofficeUserAccessor . BackofficeUser != null
133
- && _backofficeUserAccessor . BackofficeUser . AuthenticationType
134
- == global ::Umbraco . Cms . Core . Constants . Security . BackOfficeAuthenticationType
135
- && _backofficeUserAccessor . BackofficeUser . Claims != null
136
- && _backofficeUserAccessor . BackofficeUser . Claims . Any ( x =>
137
- x . Type == global ::Umbraco . Cms . Core . Constants . Security . AllowedApplicationsClaimType
138
- && x . Value == global ::Umbraco . Cms . Core . Constants . Conventions . PermissionCategories . ContentCategory ) )
82
+ //check if the user is logged in to the backoffice
83
+ //and they have access to the content section
84
+ if ( _backofficeUserAccessor . BackofficeUser . IsAllowedToSeeEditLink ( ) )
139
85
{
140
- var editLink = $ "/umbraco#/content/content/edit/ { ContentId } ";
86
+ var editLinkUrl = $ "{ EditUrl } { ContentId } ";
141
87
88
+ StringBuilder editLinkCode = new StringBuilder ( ) ;
89
+ SetAttributeValue ( "style" , Style , editLinkCode ) ;
142
90
91
+ if ( UseDefaultStyles )
92
+ {
93
+ //Render the outer div with some inline styles
94
+ editLinkCode . Append ( $ "<div") ;
95
+ SetAttributeValue ( "style" , GetOuterElementStyles ( ) , editLinkCode ) ;
96
+ editLinkCode . Append ( $ ">") ;
97
+ }
143
98
144
- StringBuilder editLinkCode = new StringBuilder ( ) ;
99
+ //Add the opening tag of the link
100
+ editLinkCode . Append ( $ "<a") ;
145
101
146
- //Render the starting outer div
147
- editLinkCode . Append ( $ "<div") ;
148
- editLinkCode . Append ( $ " class=\" { OuterClassName } \" ") ;
102
+ SetAttributeValue ( "href" , editLinkUrl , editLinkCode ) ;
103
+ SetAttributeValue ( "id" , Id , editLinkCode ) ;
104
+ SetAttributeValue ( "class" , Class , editLinkCode ) ;
105
+ SetAttributeValue ( "target" , Target , editLinkCode ) ;
106
+ SetAttributeValue ( "title" , Title , editLinkCode ) ;
149
107
150
- //Render the inline styles for the outer div
151
- if ( ApplyInlineOuterElementStyles )
108
+ if ( UseDefaultStyles )
152
109
{
153
- string outerStyles = GetOuterElementStyles ( OuterPosition , Position , Margin , Zindex , LinkPadding ) ;
154
- editLinkCode . Append ( $ " style=\" { outerStyles } \" ") ;
110
+ SetAttributeValue ( "style" , GetLinkStyles ( ) , editLinkCode ) ;
155
111
}
156
- editLinkCode . Append ( $ ">") ;
157
112
158
- //Render the link
159
- editLinkCode . Append ( $ "<a href=\" { UmbracoEditContentUrl } { ContentId } \" ") ;
160
- editLinkCode . Append ( $ " target=\" _blank\" ") ;
161
- editLinkCode . Append ( $ " class=\" { LinkClassName } \" ") ;
113
+ //Add the link text and close the link tag
114
+ editLinkCode . Append ( $ ">{ Text } </a>") ;
162
115
163
- //Render the inline styles for the link
164
- if ( ApplyInlineLinkStyles )
116
+ if ( UseDefaultStyles )
165
117
{
166
- string linkStyles = GetLinkStyles ( LinkColour , LinkBackgroundColour , LinkPadding , FontSize , BorderRadius ) ;
167
- editLinkCode . Append ( $ "style= \" { linkStyles } \" ") ;
118
+ //Add the closing outer div
119
+ editLinkCode . Append ( $ "</div> ") ;
168
120
}
169
121
170
- //Render the link text and closing tag
171
- editLinkCode . Append ( $ ">{ EditMessage } </a>") ;
172
-
173
- //Render the closing outer div
174
- editLinkCode . Append ( $ "</div>") ;
122
+ //Set the content of the tag helper
175
123
output . Content . SetHtmlContent ( editLinkCode . ToString ( ) ) ;
176
124
return ;
177
125
}
@@ -182,6 +130,14 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
182
130
}
183
131
}
184
132
133
+ private static void SetAttributeValue ( string attributeName , string attributeValue , StringBuilder editLinkCode )
134
+ {
135
+ if ( ! string . IsNullOrWhiteSpace ( attributeValue ) )
136
+ {
137
+ editLinkCode . Append ( $ " { attributeName } =\" { attributeValue } \" ") ;
138
+ }
139
+ }
140
+
185
141
/// <summary>
186
142
/// Helper method to get the link styles
187
143
/// </summary>
@@ -191,8 +147,12 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
191
147
/// <param name="fontSize">The font size of the link text</param>
192
148
/// <param name="borderRadius">The border radius of the link</param>
193
149
/// <returns></returns>
194
- private static string GetLinkStyles ( string linkColour , string linkBackgroundColour ,
195
- int linkPadding , int fontSize , int borderRadius )
150
+ private static string GetLinkStyles (
151
+ string linkColour = "#ffffff" ,
152
+ string linkBackgroundColour = "#1b264f" ,
153
+ int linkPadding = 10 ,
154
+ int fontSize = 16 ,
155
+ int borderRadius = 6 )
196
156
{
197
157
StringBuilder linkStyles = new StringBuilder ( ) ;
198
158
linkStyles . Append ( $ "color:{ linkColour } ;") ;
@@ -207,40 +167,25 @@ private static string GetLinkStyles(string linkColour, string linkBackgroundColo
207
167
/// Helper method to get the outer element styles
208
168
/// </summary>
209
169
/// <param name="outerPosition">The CSS position of the outer element</param>
210
- /// <param name="position">The CSS position of the link element</param>
211
170
/// <param name="margin">The margin around the outer element</param>
212
- /// <param name="zindex">The zindex of the outer element</param>
171
+ /// <param name="zindex">The z-index of the outer element</param>
213
172
/// <param name="linkPadding">The padding around the link</param>
214
173
/// <returns></returns>
215
- private static string GetOuterElementStyles ( string outerPosition , EditLinkPosition position ,
216
- int margin , int zindex , int linkPadding )
174
+ private static string GetOuterElementStyles (
175
+ string outerPosition = "fixed" ,
176
+ int margin = 10 ,
177
+ int zindex = 10000 ,
178
+ int linkPadding = 10 )
217
179
{
218
- linkPadding = linkPadding / 2 ;
180
+ linkPadding /= 2 ;
219
181
220
- StringBuilder outerStyles = new StringBuilder ( ) ;
182
+ var outerStyles = new StringBuilder ( ) ;
221
183
222
184
outerStyles . Append ( "display:block;" ) ;
223
185
if ( outerPosition == "fixed" )
224
186
{
225
- switch ( position )
226
- {
227
- case EditLinkPosition . TopLeft :
228
- outerStyles . Append ( $ "top:{ margin + linkPadding } px;") ;
229
- outerStyles . Append ( $ "left:{ margin } px;") ;
230
- break ;
231
- case EditLinkPosition . TopRight :
232
- outerStyles . Append ( $ "top:{ margin + linkPadding } px;") ;
233
- outerStyles . Append ( $ "right:{ margin } px;") ;
234
- break ;
235
- case EditLinkPosition . BottomRight :
236
- outerStyles . Append ( $ "bottom:{ margin + linkPadding } px;") ;
237
- outerStyles . Append ( $ "right:{ margin } px;") ;
238
- break ;
239
- case EditLinkPosition . BottomLeft :
240
- outerStyles . Append ( $ "bottom:{ margin + linkPadding } px;") ;
241
- outerStyles . Append ( $ "left:{ margin } px;") ;
242
- break ;
243
- }
187
+ outerStyles . Append ( $ "bottom:{ margin + linkPadding } px;") ;
188
+ outerStyles . Append ( $ "left:{ margin } px;") ;
244
189
}
245
190
246
191
outerStyles . Append ( $ "z-index:{ zindex } ;") ;
0 commit comments