136 lines
6.4 KiB
C#
136 lines
6.4 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
|
||
namespace Sozsoft.Platform.Editors;
|
||
|
||
/// <summary>
|
||
/// ListForm alanlarinin DevExtreme editor options JSON'unu uretir.
|
||
///
|
||
/// Buradaki metotlar hazir bir baslangic dondurur; uzerine
|
||
/// <see cref="EditorOptionsBuilder"/> metotlariyla ekleme yapilabilir ve sonuc
|
||
/// string'e ortuk donustugu icin dogrudan atanabilir:
|
||
///
|
||
/// <code>
|
||
/// EditorOptions = EditorOptions.ShowClearButton(),
|
||
/// EditorOptions = EditorOptions.Number(4).ShowClearButton(),
|
||
/// EditorOptions = EditorOptions.Multiline(60).Placeholder("Aciklama"),
|
||
/// EditorOptions = EditorOptions.New().Flag("acceptCustomValue", true),
|
||
/// </code>
|
||
/// </summary>
|
||
public static class EditorOptions
|
||
{
|
||
/// <summary>Bos baslangic; secenekler elle eklenir.</summary>
|
||
public static EditorOptionsBuilder New() => new EditorOptionsBuilder();
|
||
|
||
/// <summary>Alan pasif: kullanici degistiremez.</summary>
|
||
public static EditorOptionsBuilder Disabled() => New().Disabled();
|
||
|
||
/// <summary>Alan salt okunur: deger gorunur ve gonderilir, degistirilemez.</summary>
|
||
public static EditorOptionsBuilder ReadOnly() => New().ReadOnly();
|
||
|
||
/// <summary>Temizleme (x) dugmesi.</summary>
|
||
public static EditorOptionsBuilder ShowClearButton() => New().ShowClearButton();
|
||
|
||
/// <summary>Cok satirli metin alani icin yukseklik.</summary>
|
||
public static EditorOptionsBuilder Multiline(int height) => New().Height(height);
|
||
|
||
/// <summary>Sabit ondalikli sayi: bicim, maske davranisi ve artir/azalt dugmeleri.</summary>
|
||
public static EditorOptionsBuilder Number(int precision = 2)
|
||
=> New().FixedPointFormat(precision).UseMaskBehavior().ShowSpinButtons();
|
||
|
||
/// <summary>Yuzde alani.</summary>
|
||
public static EditorOptionsBuilder Percent()
|
||
=> New().Format("#0.##'%'").UseMaskBehavior().ShowSpinButtons();
|
||
|
||
/// <summary>Tarih alani (gun/ay/yil).</summary>
|
||
public static EditorOptionsBuilder Date()
|
||
=> New().Format("dd/MM/yyyy").DisplayFormat("dd/MM/yyyy");
|
||
|
||
/// <summary>Tarih + saat alani.</summary>
|
||
public static EditorOptionsBuilder DateTime()
|
||
=> New().Format("dd/MM/yyyy HH:mm").DisplayFormat("dd/MM/yyyy HH:mm");
|
||
|
||
/// <summary>Listeden secilen saat alani.</summary>
|
||
public static EditorOptionsBuilder Time(int intervalMinutes = 5)
|
||
=> New()
|
||
.Text("type", "time")
|
||
.Text("pickerType", "list")
|
||
.DisplayFormat("HH:mm")
|
||
.DateSerializationFormat("yyyy-MM-ddTHH:mm:ss")
|
||
.Number("interval", intervalMinutes)
|
||
.Width("100%");
|
||
|
||
/// <summary>Telefon alani: global bicim, maske ve ornek deger.</summary>
|
||
public static EditorOptionsBuilder Phone(string invalidMessage = "Lütfen geçerli bir telefon numarası girin")
|
||
=> New()
|
||
.Format("phoneGlobal")
|
||
.Mask("(000) 000-0000", invalidMessage)
|
||
.Raw("maskRules", "{\"X\":\"[0-9]\"}")
|
||
.Placeholder("(555) 123-4567");
|
||
|
||
/// <summary>Kaydirma cubugu; degeri ipucu olarak gosterir.</summary>
|
||
public static EditorOptionsBuilder Slider()
|
||
=> New().Raw("tooltip", "{\"enabled\":true}");
|
||
|
||
/// <summary>Gorsel yukleme alani.</summary>
|
||
public static EditorOptionsBuilder ImageUpload(bool multiple = true, int width = 80, int height = 80)
|
||
=> New().Width(width).Height(height).Flag("multiple", multiple);
|
||
|
||
/// <summary>Zengin metin (HTML) editoru; tam arac cubugu ile.</summary>
|
||
/// <remarks>
|
||
/// <c>height</c> verilirse editor sabitlenir ve icerik kaydirilir; <c>min-height</c> ise
|
||
/// en az yuksekligi korurken yeni satirlarla buyumesine izin verir.
|
||
/// Bloklar UI'daki <c>htmlEditorOptions.ts</c> varsayilaniyla birebir aynidir: istemci ic ice
|
||
/// bloklari tek seviye birlestirdigi icin buradaki <c>toolbar</c> varsayilanin yerine gecer.
|
||
/// </remarks>
|
||
public static EditorOptionsBuilder Html(int minHeight = 200)
|
||
=> New()
|
||
.Raw("toolbar", "{\"multiline\":true,\"items\":[" + string.Join(",", HtmlToolbarItems()) + "]}")
|
||
.Raw("imageUpload", "{\"tabs\":[\"file\",\"url\"],\"fileUploadMode\":\"base64\"}")
|
||
.Raw("mediaResizing", "{\"enabled\":true}")
|
||
.Raw("tableResizing", "{\"enabled\":true}")
|
||
.Raw("tableContextMenu", "{\"enabled\":true}")
|
||
.Flag("encodeHtml", true)
|
||
.Raw("elementAttr", "{\"style\":\"min-height:" + EditorJson.Number(minHeight) + "px\"}");
|
||
|
||
private static readonly string[] HtmlToolbarNames =
|
||
[
|
||
"undo", "redo", "separator",
|
||
"header", "size", "font", "separator",
|
||
"bold", "italic", "underline", "strike", "superscript", "subscript", "separator",
|
||
"color", "background", "separator",
|
||
"alignLeft", "alignCenter", "alignRight", "alignJustify", "separator",
|
||
"orderedList", "bulletList", "decreaseIndent", "increaseIndent", "separator",
|
||
"blockquote", "codeBlock", "link", "image", "separator",
|
||
"insertTable", "insertHeaderRow", "insertRowAbove", "insertRowBelow", "deleteRow",
|
||
"insertColumnLeft", "insertColumnRight", "deleteColumn", "cellProperties", "tableProperties", "deleteTable",
|
||
"separator", "clear"
|
||
];
|
||
|
||
private static readonly string[] HtmlFontSizes = ["8pt", "10pt", "12pt", "14pt", "18pt", "24pt", "36pt"];
|
||
|
||
private static readonly string[] HtmlFontFamilies =
|
||
[
|
||
"Arial", "Courier New", "Georgia", "Impact",
|
||
"Lucida Console", "Segoe UI", "Tahoma", "Times New Roman", "Verdana"
|
||
];
|
||
|
||
private static readonly string[] HtmlHeaderValues = ["false", "1", "2", "3", "4", "5", "6"];
|
||
|
||
private static IEnumerable<string> HtmlToolbarItems()
|
||
=> HtmlToolbarNames.Select(name => name switch
|
||
{
|
||
"size" => Picker(name, HtmlFontSizes.Select(EditorJson.Quote), "Font size"),
|
||
"font" => Picker(name, HtmlFontFamilies.Select(EditorJson.Quote), "Font family"),
|
||
"header" => Picker(name, HtmlHeaderValues, "Header"),
|
||
_ => Item(name)
|
||
});
|
||
|
||
private static string Item(string name) => "{\"name\":" + EditorJson.Quote(name) + "}";
|
||
|
||
/// <summary><paramref name="values"/> JSON olarak yazilmis degerlerdir (metin tirnakli, sayi/bool ciplak).</summary>
|
||
private static string Picker(string name, IEnumerable<string> values, string ariaLabel)
|
||
=> "{\"name\":" + EditorJson.Quote(name) +
|
||
",\"acceptedValues\":[" + string.Join(",", values) + "]" +
|
||
",\"options\":{\"inputAttr\":{\"aria-label\":" + EditorJson.Quote(ariaLabel) + "}}}";
|
||
}
|