SambaPOS 3 Documentation

repository·master·Indexed 19 days ago

https://github.com/emreeren/sambapos-3

Source code and technical documentation for version 3 of SambaPOS, a Touch Screen Point of Sale (POS) software system. Includes details on FluentScript integration for registering C# types, the TemplateEngineDefault for script templating, and UI command implementations such as CaptionCommand, CustomCommand, and ICategoryCommand.

Tokens
2.3K
Snippets
9
Records
12
Agent score
69%

What's inside SambaPOS 3

  1. How TemplateEngineDefault parsing works

    master

    The engine works by iterating through the input script and categorizing segments into CodeBlock objects with specific State types: Html, Expression, CodeBlock, or Comment.

    When Render() is called, the engine:

    1. Parses the script into a buffer of these blocks.
    2. Builds a final string that initializes a JavaScript variable buffer and uses string concatenation (+=) to append HTML segments and execute code blocks.

    This results in a single executable string that can be used to build dynamic content.

  2. Create a CustomCommand for UI interaction

    master

    The CustomCommand class is used to implement command logic for UI elements (like buttons) within the SambaPOS presentation layer. It inherits from DelegateCommand<object> and implements ICaptionCommand, allowing it to carry both an execution method and a displayable caption.

    You can instantiate a CustomCommand using two primary patterns:

    1. Basic Execution: Provide a caption, a data object, and an action to execute.
    2. Conditional Execution: Provide a caption, an action, a data object, and a canExecuteMethod (a Func<object, bool>) to determine if the command is currently valid (e.g., to enable/disable a button).

    CustomCommand automatically hooks into the CommandManager.RequerySuggested event, meaning the command's ability to execute will be re-evaluated whenever the UI requests a requery.

    // Pattern 1: Basic execution
    var command = new CustomCommand("Click Me", myDataObject, (obj) => 
    {
        // Execute logic here
    });
    
    // Pattern 2: Conditional execution (canExecute)
    var conditionalCommand = new CustomCommand(
        "Submit", 
        (obj) => { /* execute logic */ }, 
        myDataObject, 
        (obj) => 
        {
            // Return true if the command can be executed
            return obj != null; 
        }
    );
  3. Create instances of registered types

    master

    Once a type is registered via Register, you can instantiate it within the FluentScript environment using the Create method. You can provide an optional array of objects (object[] args) to serve as constructor arguments.

    // Create an instance using the default parameterless constructor
    object instance = registeredTypes.Create("MyCustomClass");
    
    // Create an instance passing constructor arguments
    object instanceWithArgs = registeredTypes.Create("MyCustomClass", new object[] { "arg1", 42 });
  4. Use TemplateEngineDefault for script templating

    master

    The TemplateEngineDefault class provides a templating engine with syntax similar to jQuery templates or Python-Django templates. It parses a script containing HTML and embedded code blocks (expressions, code blocks, or comments) and converts them into a JavaScript-like string concatenation format.

    To use it, instantiate the engine with your script and call Render() to get the processed output.

    var engine = new TemplateEngineDefault("<div><%= name %></div>");
    string result = engine.Render();
  5. Register custom C# types in FluentScript

    master

    The RegisteredTypes class allows you to bridge C# types into the FluentScript interpreter, enabling the language to call C# functions and interact with custom objects. You can register a type by providing its Type and a Func<object> creator that defines how a new instance of that type is instantiated.

    Note: DateTime is treated as a basic type with a specialized creator that uses DateTime.Now.Ticks.

    // Assuming an instance of RegisteredTypes named 'registeredTypes'
    registeredTypes.Register(typeof(MyCustomClass), () => new MyCustomClass());
  6. Check for and retrieve registered types

    master

    You can verify if a type is available in the interpreter and retrieve its underlying .NET Type using the following methods:

    • Contains(string nameOrFullName): Returns true if the type (by short name or full name) is registered.
    • Get(string nameOrFullName): Returns the System.Type associated with the provided name or full name.
    if (registeredTypes.Contains("MyCustomClass"))
    {
        Type type = registeredTypes.Get("MyCustomClass");
        // Proceed with logic using the retrieved Type
    }
  7. Implement a CaptionCommand for UI actions

    master

    The CaptionCommand<T> is a specialized implementation of DelegateCommand<T> used to bind UI actions to executable logic while providing a human-readable Caption property. This is useful for commands that need to display text (like button labels) that can change dynamically.

    It supports:

    • Execution logic: An Action<T> that defines what happens when the command is triggered.
    • Conditional execution: An optional Func<T, bool> to determine if the command is currently allowed to run.
    • Dynamic Captions: A Caption string property that notifies the UI when it changes via INotifyPropertyChanged.
    • Automatic Re-evaluation: It hooks into CommandManager.RequerySuggested to automatically refresh the command's CanExecute state based on UI changes.
    // Example: Creating a command for a button that displays "Save" 
    // and is only enabled if the data object 'myData' is valid.
    var saveCommand = new CaptionCommand<MyDataModel>(
        "Save", 
        (data) => SaveData(data), 
        (data) => data.IsValid
    );
    
    // You can update the caption dynamically:
    saveCommand.Caption = "Updating...";
  8. Template syntax for TemplateEngineDefault

    master

    The TemplateEngineDefault engine uses specific delimiters to distinguish between plain text (HTML), expressions, code blocks, and comments:

    FeatureSyntaxDescription
    Expression<%= expression %>Evaluates an expression and appends the result to the output.
    Code Block<% code %>Executes a block of code (e.g., loops, conditionals).
    Comment<%- comment %>Adds a comment block /* comment */ to the output.
    Escape%%Escapes the % character.

    Note: The engine treats everything outside these delimiters as standard HTML/text.

    /* Syntax Reference */
    <%= expression %> // Expression
    <% code %>        // Code Block
    <%- comment %>    // Comment
    %%                 // Escape %
  9. Properties of CustomCommand

    master

    The CustomCommand class exposes the following properties for managing command state and display:

    • Caption (string): The text label associated with the command, used for UI display.
    • DataObject (object): The data context or object passed to the execution and validation methods.
    • CanExecuteChanged (event): An event that notifies the UI when the command's execution status changes. It is wired to CommandManager.RequerySuggested to ensure automatic UI updates.
  10. Implement ICategoryCommand for category-related commands

    master

    The ICategoryCommand interface is used to define commands that operate on or represent a specific category within the SambaPOS system. Any implementation of this interface must provide properties for the category name, its associated image source, and its display order. It inherits from ICaptionCommand, implying it also supports captioning functionality.

    public interface ICategoryCommand : ICaptionCommand
    {
        string Category { get; set; }
        string ImageSource { get; set; }
        int Order { get; set; }
    }