private List <dynamic> GetFileObjectsFromStringList(List <string> filePaths) { var resultFiles = new List <dynamic>(); foreach (var filePath in filePaths) { var iconClass = "fa " + WvHelpers.GetPathTypeIcon(filePath); var fileName = WvHelpers.GetFileNameFromPath(filePath); dynamic file = new { path = filePath, size = 0, name = fileName, icon = iconClass, timestamp = (DateTime?)null, author = "" }; resultFiles.Add(file); } return(resultFiles); }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (!isVisible) { output.SuppressOutput(); return(Task.CompletedTask); } #region << Init >> var initSuccess = InitField(context, output); if (!initSuccess) { return(Task.CompletedTask); } if (Value == null) { Value = new List <dynamic>(); } else if (Value is string) { var stringValue = (string)Value; if (String.IsNullOrWhiteSpace(stringValue)) { Value = new List <dynamic>(); } else if (stringValue.Contains(",")) { var filePaths = stringValue.Split(",", StringSplitOptions.RemoveEmptyEntries).ToList(); Value = GetFileObjectsFromStringList(filePaths); } else { var filePaths = new List <string> { stringValue }; Value = GetFileObjectsFromStringList(filePaths); } } else if (Value is List <string> ) { var filePaths = (List <string>)Value; Value = GetFileObjectsFromStringList(filePaths); } else if (Value is List <object> ) { var valueObjects = (List <object>)Value; var resultFiles = new List <dynamic>(); foreach (var fileObject in valueObjects) { var type = fileObject.GetType(); var filePath = ""; var fileSize = 0; var fileIcon = ""; var fileName = ""; DateTime?fileTimestamp = null; var fileAuthor = ""; if (!String.IsNullOrWhiteSpace(PathPropName) && type.GetProperty(PathPropName) != null && type.GetProperty(PathPropName).GetValue(fileObject) != null) { var propValue = type.GetProperty(PathPropName).GetValue(fileObject); if (propValue is string) { filePath = propValue.ToString(); } } if (!String.IsNullOrWhiteSpace(SizePropName) && type.GetProperty(SizePropName) != null && type.GetProperty(SizePropName).GetValue(fileObject) != null) { var propValue = type.GetProperty(SizePropName).GetValue(fileObject); if (propValue is int || propValue is decimal || propValue is double) { fileSize = (int)propValue; } } if (!String.IsNullOrWhiteSpace(IconPropName) && type.GetProperty(IconPropName) != null && type.GetProperty(IconPropName).GetValue(fileObject) != null) { var propValue = type.GetProperty(IconPropName).GetValue(fileObject); if (propValue is string) { fileIcon = propValue.ToString(); } } if (!String.IsNullOrWhiteSpace(filePath) && String.IsNullOrWhiteSpace(fileIcon)) { fileIcon = "fa " + WvHelpers.GetPathTypeIcon(filePath); } if (!String.IsNullOrWhiteSpace(NamePropName) && type.GetProperty(NamePropName) != null && type.GetProperty(NamePropName).GetValue(fileObject) != null) { var propValue = type.GetProperty(NamePropName).GetValue(fileObject); if (propValue is string) { fileName = propValue.ToString(); } } if (!String.IsNullOrWhiteSpace(filePath) && String.IsNullOrWhiteSpace(fileName)) { fileName = WvHelpers.GetFileNameFromPath(filePath); } if (!String.IsNullOrWhiteSpace(TimestampPropName) && type.GetProperty(TimestampPropName) != null && type.GetProperty(TimestampPropName).GetValue(fileObject) != null) { var propValue = type.GetProperty(TimestampPropName).GetValue(fileObject); if (propValue is DateTime) { fileTimestamp = (DateTime)propValue; } } if (!String.IsNullOrWhiteSpace(AuthorPropName) && type.GetProperty(AuthorPropName) != null && type.GetProperty(AuthorPropName).GetValue(fileObject) != null) { var propValue = type.GetProperty(AuthorPropName).GetValue(fileObject); if (propValue is string) { fileAuthor = propValue.ToString(); } } dynamic file = new { path = filePath, size = fileSize, name = fileName, icon = fileIcon, timestamp = fileTimestamp, author = fileAuthor }; resultFiles.Add(file); } Value = resultFiles; } else { output.Content.AppendHtml("<div class='go-red'>'value' property type is not supported</div>"); return(Task.CompletedTask); } #endregion #region << Render >> var fileObjects = (List <dynamic>)Value; if (Mode == WvFieldRenderMode.Form) { if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate) { #region << Hidden input for posting >> output.Content.AppendHtml($"<input type='hidden' id='input-{FieldId}' name='{Name}' value='{String.Join(',', fileObjects.Select(x => x.path).ToList())}'/>"); #endregion #region << fake upload >> var inputGroupEl = new TagBuilder("div"); inputGroupEl.AddCssClass("input-group erp-file-multiple-input"); var fakeInputEl = new TagBuilder("div"); var inputElCssClassList = new List <string>(); inputElCssClassList.Add("form-control erp-file with-progress"); fakeInputEl.Attributes.Add("id", $"fake-{FieldId}"); fakeInputEl.Attributes.Add("style", "cursor:pointer"); inputElCssClassList.Add("go-gray"); fakeInputEl.InnerHtml.AppendHtml("Activate 'Paste Image' from clipboard"); if (ValidationErrors.Count > 0) { inputElCssClassList.Add("is-invalid"); } fakeInputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList)); var fakeInputProgress = new TagBuilder("div"); fakeInputProgress.AddCssClass("form-control-progress"); fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress); fakeInputEl.Attributes.Add("data-handler-prefix", GetHandlerPrefix); fakeInputEl.Attributes.Add("data-file-upload-api", FileUploadApi); fakeInputEl.Attributes.Add("data-path-name", PathPropName); fakeInputEl.Attributes.Add("data-size-name", SizePropName); fakeInputEl.Attributes.Add("data-name-name", NamePropName); fakeInputEl.Attributes.Add("data-icon-name", IconPropName); fakeInputEl.Attributes.Add("data-timestamp-name", TimestampPropName); fakeInputEl.Attributes.Add("data-author-name", AuthorPropName); fakeInputEl.Attributes.Add("data-field-id", (FieldId != null ? FieldId.Value.ToString() : "")); inputGroupEl.InnerHtml.AppendHtml(fakeInputEl); var appendEl = new TagBuilder("span"); appendEl.AddCssClass("input-group-append action erp-file"); var selectFileLink = new TagBuilder("button"); selectFileLink.Attributes.Add("type", $"button"); selectFileLink.AddCssClass("btn btn-white"); selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();"); selectFileLink.InnerHtml.AppendHtml("browse"); appendEl.InnerHtml.AppendHtml(selectFileLink); inputGroupEl.InnerHtml.AppendHtml(appendEl); output.Content.AppendHtml(inputGroupEl); var realHiddenFileInput = new TagBuilder("input"); realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}"); realHiddenFileInput.Attributes.Add("type", $"file"); realHiddenFileInput.Attributes.Add("multiple", $"true"); realHiddenFileInput.AddCssClass("d-none"); realHiddenFileInput.Attributes.Add("value", $""); if (!String.IsNullOrWhiteSpace(Accept)) { realHiddenFileInput.Attributes.Add("accept", $"{Accept}"); } output.Content.AppendHtml(realHiddenFileInput); #endregion #region << Files list element >> var filesListEl = new TagBuilder("div"); filesListEl.AddCssClass("form-control erp-file-multiple-list form"); filesListEl.Attributes.Add("id", $"fake-list-{FieldId}"); if (fileObjects.Count == 0) { filesListEl.AddCssClass("d-none"); } //Generate the files list foreach (var fileObject in fileObjects) { var fileRow = new TagBuilder("div"); fileRow.AddCssClass("filerow"); fileRow.Attributes.Add("data-file-path", fileObject.path); fileRow.Attributes.Add("data-field-id", (FieldId != null ? FieldId.Value.ToString() : "")); //Append icon fileRow.InnerHtml.AppendHtml($"<div class='icon'><i class='fa {fileObject.icon}'></i></div>"); //Append meta var rowMeta = new TagBuilder("div"); rowMeta.AddCssClass("meta"); //Append file rowMeta.InnerHtml.AppendHtml($"<a class='link' href='{GetHandlerPrefix}{fileObject.path}' target='_blank' title='{GetHandlerPrefix}{fileObject.path}'>{fileObject.name}<em></em></a>"); if (((int)fileObject.size) > 0 || ((DateTime?)fileObject.timestamp) != null || String.IsNullOrWhiteSpace((string)fileObject.author)) { var fileRowMetaDetailsEl = new TagBuilder("div"); fileRowMetaDetailsEl.AddCssClass("details"); if (((int)fileObject.size) > 0) { var sizeString = WvHelpers.GetSizeStringFromSize((int)fileObject.size); fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='size'>{sizeString}</span>"); } if (((DateTime?)fileObject.timestamp) != null) { var timestampString = ((DateTime)fileObject.timestamp).ToString("dd MMM yyyy HH:mm", Culture); fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='timestamp'>{timestampString}</span>"); } if (!String.IsNullOrWhiteSpace((string)fileObject.author)) { fileRowMetaDetailsEl.InnerHtml.AppendHtml($"<span class='author'>{(string)fileObject.author}</span>"); } rowMeta.InnerHtml.AppendHtml(fileRowMetaDetailsEl); } fileRow.InnerHtml.AppendHtml(rowMeta); //Action var rowAction = new TagBuilder("div"); rowAction.AddCssClass("action remove"); rowAction.InnerHtml.AppendHtml($"<a class='link' href='#'><i class='fa fa-times-circle'></i></a>"); fileRow.InnerHtml.AppendHtml(rowAction); filesListEl.InnerHtml.AppendHtml(fileRow); } output.Content.AppendHtml(filesListEl); #endregion var jsCompressor = new JavaScriptCompressor(); #region << Init Scripts >> { var tagHelperInitialized = false; var fileName = "form"; if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldFileMultiple) + fileName)) { var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldFileMultiple) + fileName]; tagHelperInitialized = tagHelperContext.Initialized; } if (!tagHelperInitialized) { var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldFileMultiple", "WebVella.TagHelpers"); var scriptEl = new TagBuilder("script"); scriptEl.Attributes.Add("type", "text/javascript"); //scriptEl.InnerHtml.AppendHtml(scriptContent); scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent)); output.PostContent.AppendHtml(scriptEl); ViewContext.HttpContext.Items[typeof(WvFieldFileMultiple) + fileName] = new WvTagHelperContext() { Initialized = true }; } } #endregion #region << Add Inline Init Script for this instance >> var initScript = new TagBuilder("script"); initScript.Attributes.Add("type", "text/javascript"); var scriptTemplate = @" $(function(){ FieldMultiFileFormInit(""{{FieldId}}"",{{ConfigJson}}); });" ; scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId != null ? FieldId.Value.ToString() : "")); var fieldConfig = new WvFieldFileConfig() { ApiUrl = ApiUrl, CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false, Accept = Accept, FileUploadApi = FileUploadApi }; scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig)); initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate)); output.PostContent.AppendHtml(initScript); #endregion } else if (Access == WvFieldAccess.ReadOnly) { output.Content.AppendHtml(GenerateDisplayHtml(fileObjects, GetHandlerPrefix)); } } else if (Mode == WvFieldRenderMode.Display) { output.Content.AppendHtml(GenerateDisplayHtml(fileObjects, GetHandlerPrefix)); } else if (Mode == WvFieldRenderMode.Simple) { output.SuppressOutput(); foreach (var fileObject in fileObjects) { var fileSpan = new TagBuilder("span"); fileSpan.AddCssClass("mr-2"); if (!String.IsNullOrWhiteSpace(fileObject.icon)) { fileSpan.InnerHtml.AppendHtml($"<i class='{fileObject.icon}'></i> "); } var fileLink = new TagBuilder("a"); fileLink.Attributes.Add("href", GetHandlerPrefix + fileObject.path); fileLink.Attributes.Add("title", GetHandlerPrefix + fileObject.path); fileLink.Attributes.Add("target", "_blank"); fileLink.InnerHtml.Append(fileObject.name); fileSpan.InnerHtml.AppendHtml(fileLink); output.Content.AppendHtml(fileSpan); } } else if (Mode == WvFieldRenderMode.InlineEdit) { output.Content.AppendHtml("Not implemented yet"); } #endregion //Finally if (SubInputEl != null) { output.PostContent.AppendHtml(SubInputEl); } return(Task.CompletedTask); }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (!isVisible) { output.SuppressOutput(); return(Task.CompletedTask); } #region << Init >> var initSuccess = InitField(context, output); if (!initSuccess) { return(Task.CompletedTask); } if (Value != null && !String.IsNullOrWhiteSpace((Value ?? "").ToString())) { PathTypeIcon = WvHelpers.GetPathTypeIcon(Value); FileName = Path.GetFileName(Value); } #endregion #region << Render >> if (Mode == WvFieldRenderMode.Form) { if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate) { var inputGroupEl = new TagBuilder("div"); inputGroupEl.AddCssClass("input-group"); var prependEl = new TagBuilder("span"); prependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); prependEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); var prependText = new TagBuilder("span"); prependText.AddCssClass("input-group-text"); var prependIcon = new TagBuilder("span"); prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); prependText.InnerHtml.AppendHtml(prependIcon); prependEl.InnerHtml.AppendHtml(prependText); inputGroupEl.InnerHtml.AppendHtml(prependEl); var fakeInputEl = new TagBuilder("div"); var inputElCssClassList = new List <string>(); inputElCssClassList.Add("form-control erp-file with-progress "); fakeInputEl.Attributes.Add("id", $"fake-{FieldId}"); if (ValidationErrors.Count > 0) { inputElCssClassList.Add("is-invalid"); } fakeInputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList)); var fakeInputFileLinkEl = new TagBuilder("a"); fakeInputFileLinkEl.Attributes.Add("href", $"{SrcPrefix}{Value}"); fakeInputFileLinkEl.Attributes.Add("target", "_blank"); fakeInputFileLinkEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); fakeInputFileLinkEl.InnerHtml.Append(FileName); fakeInputEl.InnerHtml.AppendHtml(fakeInputFileLinkEl); var fakeInputProgress = new TagBuilder("div"); fakeInputProgress.AddCssClass("form-control-progress"); fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress); inputGroupEl.InnerHtml.AppendHtml(fakeInputEl); var appendEl = new TagBuilder("span"); appendEl.AddCssClass("input-group-append action erp-file"); if (!Required) { var appendDeleteLink = new TagBuilder("button"); appendDeleteLink.Attributes.Add("type", $"button"); appendDeleteLink.Attributes.Add("id", $"remove-{FieldId}"); appendDeleteLink.AddCssClass($"btn btn-white remove {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); appendDeleteLink.Attributes.Add("title", "select as undefined"); var appendDeleteLinkIcon = new TagBuilder("span"); appendDeleteLinkIcon.AddCssClass("fa fa-fw fa-trash go-red"); appendDeleteLink.InnerHtml.AppendHtml(appendDeleteLinkIcon); appendEl.InnerHtml.AppendHtml(appendDeleteLink); } var selectFileLink = new TagBuilder("button"); selectFileLink.Attributes.Add("type", $"button"); selectFileLink.AddCssClass("btn btn-white"); selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();"); selectFileLink.InnerHtml.AppendHtml("browse"); appendEl.InnerHtml.AppendHtml(selectFileLink); inputGroupEl.InnerHtml.AppendHtml(appendEl); output.Content.AppendHtml(inputGroupEl); var realHiddenFileInput = new TagBuilder("input"); realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}"); realHiddenFileInput.Attributes.Add("type", $"file"); realHiddenFileInput.AddCssClass("d-none"); realHiddenFileInput.Attributes.Add("value", $""); if (!String.IsNullOrWhiteSpace(Accept)) { realHiddenFileInput.Attributes.Add("accept", $"{Accept}"); } output.Content.AppendHtml(realHiddenFileInput); var realSubmitInput = new TagBuilder("input"); realSubmitInput.Attributes.Add("id", $"input-{FieldId}"); realSubmitInput.Attributes.Add("type", $"hidden"); realSubmitInput.Attributes.Add("name", $"{Name}"); realSubmitInput.Attributes.Add("value", $"{Value}"); output.Content.AppendHtml(realSubmitInput); var jsCompressor = new JavaScriptCompressor(); #region << Init Scripts >> var tagHelperInitialized = false; if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldFile) + "-form")) { var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldFile) + "-form"]; tagHelperInitialized = tagHelperContext.Initialized; } if (!tagHelperInitialized) { var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldFile", "WebVella.TagHelpers"); var scriptEl = new TagBuilder("script"); scriptEl.Attributes.Add("type", "text/javascript"); scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent)); output.PostContent.AppendHtml(scriptEl); ViewContext.HttpContext.Items[typeof(WvFieldFile) + "-form"] = new WvTagHelperContext() { Initialized = true }; } #endregion #region << Add Inline Init Script for this instance >> var initScript = new TagBuilder("script"); initScript.Attributes.Add("type", "text/javascript"); var scriptTemplate = @" $(function(){ FileFormInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}}); });" ; scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString()); scriptTemplate = scriptTemplate.Replace("{{Name}}", Name); var fieldConfig = new WvFieldFileConfig() { ApiUrl = ApiUrl, CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false, Accept = Accept, FileUploadApi = FileUploadApi, SrcPrefix = SrcPrefix }; scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig)); initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate)); output.PostContent.AppendHtml(initScript); #endregion } else if (Access == WvFieldAccess.ReadOnly) { if (!String.IsNullOrWhiteSpace(Value)) { var inputGroupEl = new TagBuilder("div"); inputGroupEl.AddCssClass("input-group"); var prependEl = new TagBuilder("span"); prependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); prependEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); var prependText = new TagBuilder("span"); prependText.AddCssClass("input-group-text"); var prependIcon = new TagBuilder("span"); prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); prependText.InnerHtml.AppendHtml(prependIcon); prependEl.InnerHtml.AppendHtml(prependText); inputGroupEl.InnerHtml.AppendHtml(prependEl); var inputEl = new TagBuilder("div"); inputEl.AddCssClass("form-control erp-file disabled"); var inputElLink = new TagBuilder("a"); inputElLink.Attributes.Add("href", $"{SrcPrefix}{Value}"); inputElLink.Attributes.Add("target", "_blank"); inputElLink.Attributes.Add("title", $"{SrcPrefix}{Value}"); inputElLink.InnerHtml.Append(FileName); inputEl.InnerHtml.AppendHtml(inputElLink); inputGroupEl.InnerHtml.AppendHtml(inputEl); output.Content.AppendHtml(inputGroupEl); //Hidden input with the value var hiddenInput = new TagBuilder("input"); hiddenInput.Attributes.Add("type", "hidden"); hiddenInput.Attributes.Add("id", $"input-{FieldId}"); hiddenInput.Attributes.Add("name", Name); hiddenInput.Attributes.Add("value", (Value ?? "").ToString()); output.Content.AppendHtml(hiddenInput); } else { var inputEl = new TagBuilder("input"); inputEl.Attributes.Add("readonly", null); inputEl.AddCssClass("form-control erp-file"); inputEl.Attributes.Add("value", ""); inputEl.Attributes.Add("name", Name); output.Content.AppendHtml(inputEl); } } } else if (Mode == WvFieldRenderMode.Display) { if (!String.IsNullOrWhiteSpace(Value)) { var divEl = new TagBuilder("div"); divEl.Attributes.Add("id", $"input-{FieldId}"); divEl.AddCssClass("form-control-plaintext erp-file"); var iconEl = new TagBuilder("span"); iconEl.AddCssClass($"fa fa-fw {PathTypeIcon}"); divEl.InnerHtml.AppendHtml(iconEl); var linkEl = new TagBuilder("a"); linkEl.Attributes.Add("href", $"{SrcPrefix}{Value}"); linkEl.Attributes.Add("target", $"_blank"); linkEl.InnerHtml.Append(FileName); divEl.InnerHtml.AppendHtml(linkEl); output.Content.AppendHtml(divEl); } else { output.Content.AppendHtml(EmptyValEl); } } else if (Mode == WvFieldRenderMode.Simple) { output.SuppressOutput(); var linkEl = new TagBuilder("a"); linkEl.Attributes.Add("href", $"{SrcPrefix}{Value}"); linkEl.Attributes.Add("target", $"_blank"); linkEl.InnerHtml.Append(FileName); output.Content.AppendHtml(linkEl); return(Task.CompletedTask); } else if (Mode == WvFieldRenderMode.InlineEdit) { if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate) { #region << View Wrapper >> { var viewWrapperEl = new TagBuilder("div"); viewWrapperEl.AddCssClass("input-group view-wrapper"); viewWrapperEl.Attributes.Add("title", "double click to edit"); viewWrapperEl.Attributes.Add("id", $"view-{FieldId}"); var viewInputPrepend = new TagBuilder("span"); viewInputPrepend.AddCssClass($"input-group-prepend icon-addon"); viewInputPrepend.Attributes.Add("title", $"{SrcPrefix}{Value}"); var viewInputPrependText = new TagBuilder("span"); viewInputPrependText.AddCssClass("input-group-text"); var prependIcon = new TagBuilder("span"); prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); viewInputPrependText.InnerHtml.AppendHtml(prependIcon); viewInputPrepend.InnerHtml.AppendHtml(viewInputPrependText); viewWrapperEl.InnerHtml.AppendHtml(viewInputPrepend); var viewFormControlEl = new TagBuilder("div"); viewFormControlEl.AddCssClass("form-control erp-file"); var viewFormControlLinkEl = new TagBuilder("a"); viewFormControlLinkEl.Attributes.Add("href", $"{SrcPrefix}{Value}"); viewFormControlLinkEl.Attributes.Add("target", "_blank"); viewFormControlLinkEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); viewFormControlLinkEl.InnerHtml.Append(FileName); viewFormControlEl.InnerHtml.AppendHtml(viewFormControlLinkEl); viewWrapperEl.InnerHtml.AppendHtml(viewFormControlEl); var viewInputActionEl = new TagBuilder("span"); viewInputActionEl.AddCssClass("input-group-append action"); viewInputActionEl.Attributes.Add("title", "edit"); var viewInputActionLinkEl = new TagBuilder("button"); viewInputActionLinkEl.Attributes.Add("type", "button"); viewInputActionLinkEl.AddCssClass("btn btn-white"); var viewInputActionIconEl = new TagBuilder("span"); viewInputActionIconEl.AddCssClass("fa fa-fw fa-pencil-alt"); viewInputActionLinkEl.InnerHtml.AppendHtml(viewInputActionIconEl); viewInputActionEl.InnerHtml.AppendHtml(viewInputActionLinkEl); viewWrapperEl.InnerHtml.AppendHtml(viewInputActionEl); output.Content.AppendHtml(viewWrapperEl); } #endregion #region << Edit Wrapper>> { var editWrapperEl = new TagBuilder("div"); editWrapperEl.Attributes.Add("id", $"edit-{FieldId}"); editWrapperEl.Attributes.Add("style", $"display:none;"); editWrapperEl.AddCssClass("edit-wrapper"); var editInputGroupEl = new TagBuilder("div"); editInputGroupEl.AddCssClass("input-group"); var editWrapperPrependEl = new TagBuilder("span"); editWrapperPrependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); editWrapperPrependEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); var editWrapperPrependText = new TagBuilder("span"); editWrapperPrependText.AddCssClass("input-group-text"); var editWrapperPrependIcon = new TagBuilder("span"); editWrapperPrependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); editWrapperPrependText.InnerHtml.AppendHtml(editWrapperPrependIcon); editWrapperPrependEl.InnerHtml.AppendHtml(editWrapperPrependText); editInputGroupEl.InnerHtml.AppendHtml(editWrapperPrependEl); var fakeInputEl = new TagBuilder("div"); var inputElCssClassList = new List <string>(); inputElCssClassList.Add("form-control erp-file with-progress "); fakeInputEl.Attributes.Add("id", $"fake-{FieldId}"); if (ValidationErrors.Count > 0) { inputElCssClassList.Add("is-invalid"); } fakeInputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList)); var fakeInputFileLinkEl = new TagBuilder("a"); fakeInputFileLinkEl.Attributes.Add("href", $"{SrcPrefix}{Value}"); fakeInputFileLinkEl.Attributes.Add("target", "_blank"); fakeInputFileLinkEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); fakeInputFileLinkEl.InnerHtml.Append(FileName); fakeInputEl.InnerHtml.AppendHtml(fakeInputFileLinkEl); var fakeInputProgress = new TagBuilder("div"); fakeInputProgress.AddCssClass("form-control-progress"); fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress); editInputGroupEl.InnerHtml.AppendHtml(fakeInputEl); var editInputGroupAppendEl = new TagBuilder("span"); editInputGroupAppendEl.AddCssClass("input-group-append"); if (!Required) { var appendDeleteLink = new TagBuilder("button"); appendDeleteLink.Attributes.Add("type", $"button"); appendDeleteLink.Attributes.Add("id", $"remove-{FieldId}"); appendDeleteLink.AddCssClass($"btn btn-white remove {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); appendDeleteLink.Attributes.Add("title", "select as undefined"); var appendDeleteLinkIcon = new TagBuilder("span"); appendDeleteLinkIcon.AddCssClass("fa fa-fw fa-trash go-red"); appendDeleteLink.InnerHtml.AppendHtml(appendDeleteLinkIcon); editInputGroupAppendEl.InnerHtml.AppendHtml(appendDeleteLink); } var selectFileLink = new TagBuilder("button"); selectFileLink.Attributes.Add("type", $"button"); selectFileLink.AddCssClass("btn btn-white"); selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();"); selectFileLink.InnerHtml.AppendHtml("select"); editInputGroupAppendEl.InnerHtml.AppendHtml(selectFileLink); var editSaveBtnEl = new TagBuilder("button"); editSaveBtnEl.Attributes.Add("type", "submit"); editSaveBtnEl.AddCssClass("btn btn-white save"); editSaveBtnEl.Attributes.Add("title", "save"); var editSaveIconEl = new TagBuilder("span"); editSaveIconEl.AddCssClass("fa fa-fw fa-check go-green"); editSaveBtnEl.InnerHtml.AppendHtml(editSaveIconEl); editInputGroupAppendEl.InnerHtml.AppendHtml(editSaveBtnEl); var editCancelBtnEl = new TagBuilder("button"); editCancelBtnEl.Attributes.Add("type", "submit"); editCancelBtnEl.AddCssClass("btn btn-white cancel"); editCancelBtnEl.Attributes.Add("title", "cancel"); var editCancelIconEl = new TagBuilder("span"); editCancelIconEl.AddCssClass("fa fa-fw fa-times go-gray"); editCancelBtnEl.InnerHtml.AppendHtml(editCancelIconEl); editInputGroupAppendEl.InnerHtml.AppendHtml(editCancelBtnEl); editInputGroupEl.InnerHtml.AppendHtml(editInputGroupAppendEl); editWrapperEl.InnerHtml.AppendHtml(editInputGroupEl); output.Content.AppendHtml(editWrapperEl); var realHiddenFileInput = new TagBuilder("input"); realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}"); realHiddenFileInput.Attributes.Add("type", $"file"); realHiddenFileInput.AddCssClass("d-none"); realHiddenFileInput.Attributes.Add("value", $""); if (!String.IsNullOrWhiteSpace(Accept)) { realHiddenFileInput.Attributes.Add("accept", $"{Accept}"); } output.Content.AppendHtml(realHiddenFileInput); var realSubmitInput = new TagBuilder("input"); realSubmitInput.Attributes.Add("id", $"input-{FieldId}"); realSubmitInput.Attributes.Add("type", $"hidden"); realSubmitInput.Attributes.Add("value", $"{Value}"); realSubmitInput.Attributes.Add("data-newfilepath", $"{Value}"); realSubmitInput.Attributes.Add("data-filename", $"{FileName}"); realSubmitInput.Attributes.Add("data-newfilename", $"{FileName}"); output.Content.AppendHtml(realSubmitInput); } #endregion var jsCompressor = new JavaScriptCompressor(); #region << Init Scripts >> var tagHelperInitialized = false; if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldFile) + "-inline-edit")) { var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldFile) + "-inline-edit"]; tagHelperInitialized = tagHelperContext.Initialized; } if (!tagHelperInitialized) { var scriptContent = WvHelpers.GetEmbeddedTextResource("inline-edit.js", "WebVella.TagHelpers.TagHelpers.WvFieldFile", "WebVella.TagHelpers"); var scriptEl = new TagBuilder("script"); scriptEl.Attributes.Add("type", "text/javascript"); scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent)); output.PostContent.AppendHtml(scriptEl); ViewContext.HttpContext.Items[typeof(WvFieldFile) + "-inline-edit"] = new WvTagHelperContext() { Initialized = true }; } #endregion #region << Add Inline Init Script for this instance >> var initScript = new TagBuilder("script"); initScript.Attributes.Add("type", "text/javascript"); var scriptTemplate = @" $(function(){ FileInlineEditInit(""{{FieldId}}"",""{{Name}}"",{{ConfigJson}}); });" ; scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString()); scriptTemplate = scriptTemplate.Replace("{{Name}}", Name); var fieldConfig = new WvFieldFileConfig() { ApiUrl = ApiUrl, CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false, Accept = Accept, FileUploadApi = FileUploadApi, SrcPrefix = SrcPrefix }; scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig)); initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate)); output.PostContent.AppendHtml(initScript); #endregion } else if (Access == WvFieldAccess.ReadOnly) { var divEl = new TagBuilder("div"); divEl.AddCssClass("input-group"); var prependEl = new TagBuilder("span"); prependEl.AddCssClass($"input-group-prepend icon-addon {(ValidationErrors.Count > 0 ? "is-invalid" : "")}"); prependEl.Attributes.Add("title", $"{SrcPrefix}{Value}"); var prependText = new TagBuilder("span"); prependText.AddCssClass("input-group-text"); var prependIcon = new TagBuilder("span"); prependIcon.AddCssClass($"fa fa-fw type-icon {PathTypeIcon} {(String.IsNullOrWhiteSpace(Value) ? "d-none" : "")}"); prependText.InnerHtml.AppendHtml(prependIcon); prependEl.InnerHtml.AppendHtml(prependText); divEl.InnerHtml.AppendHtml(prependEl); var inputEl = new TagBuilder("div"); inputEl.AddCssClass("form-control erp-file disabled"); var inputElLink = new TagBuilder("a"); inputElLink.Attributes.Add("href", $"{SrcPrefix}{Value}"); inputElLink.Attributes.Add("target", "_blank"); inputElLink.Attributes.Add("title", $"{SrcPrefix}{Value}"); inputElLink.InnerHtml.Append(FileName); inputEl.InnerHtml.AppendHtml(inputElLink); divEl.InnerHtml.AppendHtml(inputEl); var appendActionSpan = new TagBuilder("span"); appendActionSpan.AddCssClass("input-group-append"); appendActionSpan.AddCssClass("action"); var appendTextSpan = new TagBuilder("span"); appendTextSpan.AddCssClass("input-group-text"); var appendIconSpan = new TagBuilder("span"); appendIconSpan.AddCssClass("fa fa-fw fa-lock"); appendTextSpan.InnerHtml.AppendHtml(appendIconSpan); appendActionSpan.InnerHtml.AppendHtml(appendTextSpan); divEl.InnerHtml.AppendHtml(appendActionSpan); output.Content.AppendHtml(divEl); } } #endregion //Finally if (SubInputEl != null) { output.PostContent.AppendHtml(SubInputEl); } return(Task.CompletedTask); }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (!isVisible) { output.SuppressOutput(); return(Task.CompletedTask); } #region << Init >> var initSuccess = InitField(context, output); if (!initSuccess) { return(Task.CompletedTask); } if (Value is string && Value == "") { Value = null; } if (Value != null && !(Value is List <string>)) { output.Content.AppendHtml("<div class='go-red'>'value' property should be 'List<string>'</div>"); return(Task.CompletedTask); } if (Value != null && Value is List <string> ) { FilePaths = (List <string>)Value; } #endregion #region << Render >> if (Mode == WvFieldRenderMode.Form) { if (Access == WvFieldAccess.Full || Access == WvFieldAccess.FullAndCreate) { #region << Hidden input for posting >> output.Content.AppendHtml($"<input type='hidden' id='input-{FieldId}' name='{Name}' value='{String.Join(',', FilePaths)}'/>"); #endregion #region << fake upload >> var inputGroupEl = new TagBuilder("div"); inputGroupEl.AddCssClass("input-group erp-file-multiple-input"); var fakeInputEl = new TagBuilder("div"); var inputElCssClassList = new List <string>(); inputElCssClassList.Add("form-control erp-file with-progress"); fakeInputEl.Attributes.Add("id", $"fake-{FieldId}"); if (ValidationErrors.Count > 0) { inputElCssClassList.Add("is-invalid"); } fakeInputEl.Attributes.Add("class", String.Join(' ', inputElCssClassList)); var fakeInputProgress = new TagBuilder("div"); fakeInputProgress.AddCssClass("form-control-progress"); fakeInputEl.InnerHtml.AppendHtml(fakeInputProgress); inputGroupEl.InnerHtml.AppendHtml(fakeInputEl); var appendEl = new TagBuilder("span"); appendEl.AddCssClass("input-group-append action erp-file"); var selectFileLink = new TagBuilder("button"); selectFileLink.Attributes.Add("type", $"button"); selectFileLink.AddCssClass("btn btn-white"); selectFileLink.Attributes.Add("onclick", $"window.document.getElementById('file-{FieldId}').click();"); selectFileLink.InnerHtml.AppendHtml("browse"); appendEl.InnerHtml.AppendHtml(selectFileLink); inputGroupEl.InnerHtml.AppendHtml(appendEl); output.Content.AppendHtml(inputGroupEl); var realHiddenFileInput = new TagBuilder("input"); realHiddenFileInput.Attributes.Add("id", $"file-{FieldId}"); realHiddenFileInput.Attributes.Add("type", $"file"); realHiddenFileInput.Attributes.Add("multiple", $"true"); realHiddenFileInput.AddCssClass("d-none"); realHiddenFileInput.Attributes.Add("value", $""); if (!String.IsNullOrWhiteSpace(Accept)) { realHiddenFileInput.Attributes.Add("accept", $"{Accept}"); } output.Content.AppendHtml(realHiddenFileInput); #endregion #region << Files list element >> var filesListEl = new TagBuilder("div"); filesListEl.AddCssClass("form-control erp-file-multiple-list form"); filesListEl.Attributes.Add("id", $"fake-list-{FieldId}"); if (FilePaths.Count == 0) { filesListEl.AddCssClass("d-none"); } //Generate the files list foreach (var filePath in FilePaths) { var fileRow = new TagBuilder("div"); fileRow.AddCssClass("filerow"); fileRow.Attributes.Add("data-file-path", filePath); //Append icon var iconClass = WvHelpers.GetPathTypeIcon(filePath); fileRow.InnerHtml.AppendHtml($"<div class='icon'><i class='fa {iconClass}'></i></div>"); //Append meta var rowMeta = new TagBuilder("div"); rowMeta.AddCssClass("meta"); //Append file var fileName = WvHelpers.GetFileNameFromPath(filePath); rowMeta.InnerHtml.AppendHtml($"<a class='link' href='{filePath}' target='_blank' title='{filePath}'>{fileName}<em></em></a>"); fileRow.InnerHtml.AppendHtml(rowMeta); //Action var rowAction = new TagBuilder("div"); rowAction.AddCssClass("action remove"); rowAction.InnerHtml.AppendHtml($"<a class='link' href='#'><i class='fa fa-times-circle'></i></a>"); //rowAction.InnerHtml.AppendHtml($"<span class='progress d-none'>0%</span>"); //rowAction.InnerHtml.AppendHtml($"<span class='error go-red d-none'><i class='fas fa-exclamation-circle'></i></span>"); fileRow.InnerHtml.AppendHtml(rowAction); filesListEl.InnerHtml.AppendHtml(fileRow); } output.Content.AppendHtml(filesListEl); #endregion var jsCompressor = new JavaScriptCompressor(); #region << Init Scripts >> { var tagHelperInitialized = false; var fileName = "form"; if (ViewContext.HttpContext.Items.ContainsKey(typeof(WvFieldFileMultiple) + fileName)) { var tagHelperContext = (WvTagHelperContext)ViewContext.HttpContext.Items[typeof(WvFieldFileMultiple) + fileName]; tagHelperInitialized = tagHelperContext.Initialized; } if (!tagHelperInitialized) { var scriptContent = WvHelpers.GetEmbeddedTextResource("form.js", "WebVella.TagHelpers.TagHelpers.WvFieldFileMultiple", "WebVella.TagHelpers"); var scriptEl = new TagBuilder("script"); scriptEl.Attributes.Add("type", "text/javascript"); //scriptEl.InnerHtml.AppendHtml(scriptContent); scriptEl.InnerHtml.AppendHtml(jsCompressor.Compress(scriptContent)); output.PostContent.AppendHtml(scriptEl); ViewContext.HttpContext.Items[typeof(WvFieldFileMultiple) + fileName] = new WvTagHelperContext() { Initialized = true }; } } #endregion #region << Add Inline Init Script for this instance >> var initScript = new TagBuilder("script"); initScript.Attributes.Add("type", "text/javascript"); var scriptTemplate = @" $(function(){ FieldMultiFileFormInit(""{{FieldId}}"",{{ConfigJson}}); });" ; scriptTemplate = scriptTemplate.Replace("{{FieldId}}", (FieldId ?? null).ToString()); var fieldConfig = new WvFieldFileConfig() { ApiUrl = ApiUrl, CanAddValues = Access == WvFieldAccess.FullAndCreate ? true : false, Accept = Accept, FileUploadApi = FileUploadApi }; scriptTemplate = scriptTemplate.Replace("{{ConfigJson}}", JsonConvert.SerializeObject(fieldConfig)); initScript.InnerHtml.AppendHtml(jsCompressor.Compress(scriptTemplate)); output.PostContent.AppendHtml(initScript); #endregion } else if (Access == WvFieldAccess.ReadOnly) { output.Content.AppendHtml("Not implemented yet"); } } else if (Mode == WvFieldRenderMode.Display) { output.Content.AppendHtml("Not implemented yet"); } else if (Mode == WvFieldRenderMode.Simple) { output.Content.AppendHtml("Not implemented yet"); } else if (Mode == WvFieldRenderMode.InlineEdit) { output.Content.AppendHtml("Not implemented yet"); } #endregion //Finally if (SubInputEl != null) { output.PostContent.AppendHtml(SubInputEl); } return(Task.CompletedTask); }