settings

Main package containing all neccesarry functions

See Also

settings.aliases for shorter UDAs

Examples

  1. enum FavoriteFood { fish, meat, vegetables, fruit } //dfmt off enum Country { none, AF, AX, AL, DZ, AS, AD, AO, AI, AQ, AG, AR, AM, AW, AC, AU, AT, AZ, BS, BH, BD, BB, BY, BE, BZ, BJ, BM, BT, BO, BA, BW, BR, IO, VG, BN, BG, BF, BI, KH, CM, CA, IC, CV, BQ, KY, CF, EA, TD, CL, CN, CX, CC, CO, KM, CG, CD, CK, CR, CI, HR, CU, CW, CY, CZ, DK, DG, DJ, DM, DO, EC, EG, SV, GQ, ER, EE, ET, FK, FO, FJ, FI, FR, GF, PF, TF, GA, GM, GE, DE, GH, GI, GR, GL, GD, GP, GU, GT, GG, GN, GW, GY, HT, HN, HK, HU, IS, IN, ID, IR, IQ, IE, IM, IL, IT, JM, JP, JE, JO, KZ, KE, KI, XK, KW, KG, LA, LV, LB, LS, LR, LY, LI, LT, LU, MO, MK, MG, MW, MY, MV, ML, MT, MH, MQ, MR, MU, YT, MX, FM, MD, MC, MN, ME, MS, MA, MZ, MM, NA, NR, NP, NL, NC, NZ, NI, NE, NG, NU, NF, KP, MP, NO, OM, PK, PW, PS, PA, PG, PY, PE, PH, PN, PL, PT, PR, QA, RE, RO, RU, RW, WS, SM, ST, SA, SN, RS, SC, SL, SG, SX, SK, SI, SB, SO, ZA, GS, KR, SS, ES, LK, BL, SH, KN, LC, MF, PM, VC, SD, SR, SJ, SZ, SE, CH, SY, TW, TJ, TZ, TH, TL, TG, TK, TO, TT, TA, TN, TR, TM, TC, TV, UM, VI, UG, UA, AE, GB, US, UY, UZ, VU, VA, VE, VN, WF, EH, YE, ZM, ZW } //dfmt on enum SocialMedia { twitter = 1 << 0, facebook = 1 << 1, myspace = 1 << 2, } struct Config { @requiredSetting // Must be filled out @nonAutomaticSetting // Don't auto sync when typing @emailSetting string userEmail; bool married; @urlSetting @settingLength(64) string resourceURI; // OR @settingLength(64) URL myWebsite; @multilineSetting @settingLength(1000) string aboutMe; @rangeSetting @settingRange(0, 10) int rating; @timeSetting string favoriteTimeOfDay; // OR TimeOfDay leastFavoriteTimeOfDay; @weekSetting string bestWeekYouHad; @monthSetting string firstMonthOfWork; // Timezone-less @datetimeLocalSetting string birthdayTimeAndDate; // OR DateTime myChildsBirthdayTimeAndDate; @dateSetting string myMothersBirthday; // OR Date myFathersBirthday; @colorSetting string favoriteColor; @disabledSetting string someInformation = "Just a hint, nothing changable"; Country favoriteCountry; @optionsSetting FavoriteFood favoriteFood; BitFlags!SocialMedia usedSocialMedia; @settingTitle("If you don't have any you can still say 1 because you have yourself.") // Hover & validation text @settingMin(1) int numberOfFriends; @settingRange(0, 100) @settingStep(0.1) double englishSkillLevelPercentage; @settingMax(10) ubyte orderedProductCount; @settingLabel("Accept terms of service") @requiredSetting bool acceptTOS; @settingPattern(`(ISBN\s+)?\d{3}-\d-\d{5}-\d{3}-\d`) string favoriteBookISBN; } import vibe.vibe; auto router = new URLRouter; router.get("/style.css", serveStaticFile("styles/material.css")); router.get("/", staticRedirect("/settings")); enum html = `<html> <head> <title>Settings</title> <link rel="stylesheet" href="/style.css"/> <style> body,html{background:#efefef;color:rgba(0,0,0,0.87);font-family:Roboto,"Segoe UI",sans-serif;} .settings{background:white;border-radius:2px;padding:16px;margin:32px auto;box-shadow:0 2px 5px rgba(0,0,0,0.3);max-width:600px;} </style> </head> <body> <div class="settings"> <h2>Settings</h2> %s </div> </body> </html>`; Config settingsInstance; // You might fetch & save this per user, web-config only changes the struct router.get("/settings", delegate(scope req, scope res) @safe{ string settings = renderSettings(settingsInstance); res.writeBody(html.format(settings), "text/html"); }); router.post("/settings", delegate(scope req, scope res) @safe{ // no-js & nonautomatic setting route auto ret = req.processSettings(settingsInstance); string settings = renderSettings(settingsInstance, ret); if (ret) { // Something changed, you can save here } res.writeBody(html.format(settings), "text/html"); }); router.post("/api/setting", delegate(scope req, scope res) @safe{ // js route called for each individual setting if (req.processSettings(settingsInstance)) { // Save settings res.writeBody("", 204); // Send 200 or 204 } else res.writeBody("", HTTPStatus.badRequest); }); listenHTTP(new HTTPServerSettings, router); runApplication();

  • Declaration

    @safe string renderSettings(T, InputGenerator = DefaultInputGenerator, alias javascript = DefaultJavascriptCode)(T value, string formAttributes = "", string action = "/settings", string method = "POST", string jsAction = "/api/setting");
    @safe string renderSettings(T, InputGenerator = DefaultInputGenerator, alias javascript = DefaultJavascriptCode)(T value, ulong set, string formAttributes = "", string action = "/settings", string method = "POST", string jsAction = "/api/setting");

    Generates a HTML form for a configuration struct T with automatic instant updates using AJAX.

    Discussion

    The fields can be annotated with the various UDAs found in this module. (setting enums + structs)
    Supported types: enum (drop down lists or radio box lists), std.typecons.BitFlags (checkbox lists), bool (checkbox), string types (text, email, url, etc.), numeric types (number), std.datetime.DateTime (datetime-local), std.datetime.Date (date), std.datetime.TimeOfDay (time), vibe.inet.URL (url)

  • Declaration

    @safe string renderSetting(InputGenerator = DefaultInputGenerator, string name, Config)(ref Config config, bool success = true);

    Generates a single input

  • Declaration

    @safe ulong processSettings(T)(scope HTTPServerRequest req, ref T config, bool strict = false, bool post = true);
    @safe bool processSetting(string name, Config)(HTTPServerRequest req, ref Config config, bool strict = false, bool post = true);

    Function processing user input and validating for correctness.

    The following validations are done:
    If the setting is a disabledSetting, it will always skip this field.
    If the setting has a settingPattern, it will validate the raw value (no matter what type) against this regex.
    If the setting is a number, std.conv.to will be used to try to convert it to a double and then it will be cast to the type after checking min/max/step.
    If the setting is a BitFlags!T every passed argument will be checked if it is contained inside the enum T or when submitted via JS only the one specified argument will get validated and inverted if starting with !
    If the setting is an enum the value will be checked if it is contained inside the enum.
    Additionally if the setting is a floating point number and there hasn't been a min/max setup but it is a rangeSetting, the number will be finite.
    Integral numbers will always be checked if finite & if no range is given they will be clamped.

    Attributes for strings:
    emailSetting is validated using std.net.isemail.isEmail(CheckDns.no, EmailStatusCode.any)
    urlSetting is validated using vibe.inet.url.URL
    timeSetting is checked against pattern 00:00 + checking if 0 <= hour < 24 && 0 <= minute < 60
    weekSetting is checked against pattern 0{4,6}-W00 + checking if 1 <= year <= 200000 && 1 <= week <= 52
    monthSetting is checked against pattern 0{4,6}-00 + checking if 1 <= year <= 200000 && 1 <= month <= 12
    datetimeLocalSetting is checked against pattern 0000-00-00T00:00 + passing into std.datetime.SysTime.fromISOExtString`
    dateSetting is checked against pattern 0000-00-00 + checking the date using std.datetime.Date
    colorSetting is checked against pattern #FFFFFF
    Values using these attributes can be used without the need to validate the input.

    Parameters

    bool strict

    if false, values will be fixed to conform to the input instead of discarding them. Currently only fixing numbers and string lengths and new lines in single line strings is implemented.

    Return Value

    a bit array where each bit represents an input and is set to 1 if valid

  • Declaration

    @safe bool validateTimeString(string s);

    Validates s == pattern "00:00"

  • Declaration

    @safe bool validateWeekString(string s);

    Validates s == pattern "0{4,6}-W00"

  • Declaration

    @safe bool validateMonthString(string s);

    Validates s == pattern "0{4,6}-00"

  • Declaration

    @safe bool validateDatetimeLocalString(string s);

    Validates s == pattern "0000-00-00T00:00"

  • Declaration

    @safe bool validateDateString(string s);

    Validates s == pattern "0000-00-00"

  • Declaration

    @safe bool validateColorString(string s);

    Validates s == pattern "#xxxxxx"

  • Declaration

    @safe string makeHumanName(string identifier);

    Converts correctBookISBN_number to "Correct Book ISBN Number"

  • Declaration

    struct DefaultInputGenerator;

    Controls how the input HTML is generated

    • Declaration

      static @safe string textfield(string name, string type, string value, string raw, bool success);

      Called for single line input types

    • Declaration

      static @safe string textarea(string name, string value, string raw, bool success);

      Called for textareas

    • Declaration

      static @safe string checkbox(string name, bool checked, string raw, bool success);

      Called for boolean values

    • Declaration

      string dropdownList(Enum)(string name, Enum value, string raw, bool success);

      Called for enums disabled as select (you need to iterate over the enum members)

    • Declaration

      string optionList(Enum)(string name, Enum value, string raw, bool success);

      Called for enums displayed as list of radio boxes (you need to iterate over the enum members)

    • Declaration

      string checkboxList(Enum)(string name, BitFlags!Enum value, string raw, bool success);

      Called for BitFlags displayed as list of checkboxes.

  • Declaration

    enum emailSetting;

    Adds type="email" to string types

  • Declaration

    enum urlSetting;

    Adds type="url" to string types

  • Declaration

    enum multilineSetting;

    Makes string types textareas

  • Declaration

    enum rangeSetting;

    Adds type="range" to numeric types

  • Declaration

    enum timeSetting;

    Adds type="time" to string types

  • Declaration

    enum weekSetting;

    Adds type="week" to string types

  • Declaration

    enum monthSetting;

    Adds type="month" to string types

  • Declaration

    enum datetimeLocalSetting;

    Adds type="datetime-local" to string types

  • Declaration

    enum dateSetting;

    Adds type="date" to string types

  • Declaration

    enum colorSetting;

    Adds type="color" to string types

  • Declaration

    enum disabledSetting;

    Adds disabled to any input

  • Declaration

    enum requiredSetting;

    Adds required to any input

  • Declaration

    enum nonAutomaticSetting;

    Disables automatic JS saving when changing the input

  • Declaration

    enum optionsSetting;

    Changes a dropdown to a radio button list

  • Declaration

    struct settingMin;

    Changes the min="" attribute for numerical values

    • min

      Declaration

      double min;

  • Declaration

    struct settingMax;

    Changes the max="" attribute for numerical values

    • max

      Declaration

      double max;

  • Declaration

    struct settingStep;

    Changes the step="" attribute for numerical values

    • Declaration

      double step;

  • Declaration

    struct settingRange;

    Changes the min="" and max="" attribute for numerical values

    • min

      Declaration

      double min;

    • max

      Declaration

      double max;

  • Declaration

    struct settingLength;

    Changes the minlength="" and maxlength="" attribute for string values

    • max

      Declaration

      int max;

    • min

      Declaration

      int min;

  • Declaration

    struct settingPattern;

    Changes the pattern="regex" attribute

    • Declaration

      string regex;

  • Declaration

    struct settingTitle;

    Changes the title="" attribute for custom error messages & tooltips

    • Declaration

      string title;

  • Declaration

    struct settingLabel;

    Overrides the label of the input

    • Declaration

      string label;

  • Declaration

    enum string DefaultJavascriptCode;

    Contains a updateSetting(input) function which automatically sends changes to the server.