Products Technologies Demo Docs Blog Support Company

Building a Touch-enabled Button Bar with Javascript

This sample builds a custom touch-friendly button bar for Web.TextControl using its JavaScript API. HTML input elements styled with CSS replace the desktop ribbon bar. Click events call TXTextControl.sendCommand to toggle formatting, and the inputFormatReceived event syncs state.

Building a Touch-enabled Button Bar with Javascript

A ribbon bar is a user-friendly interface for desktop environments when users can utilize a mouse to navigate. On tablets or smart-phones, another, more touch-enabled, interface might be required. This sample shows how to implement your own button bar based on the Javascript API of Web.TextControl, the HTML5 based web editor.

Building a touch-enabled button bar with Javascript

The new button bar itself is a simple DIV element with CSS-styled HTML INPUT elements:

[from index.aspx]

<div id="iOSUXBar">
    <input type="btn btn-secondary btn-gradient" id="fontbold" title="Bold" class="btniOSUX" />
    <input type="btn btn-secondary btn-gradient" id="fontitalic" title="Italic" class="btniOSUX" />
    <input type="btn btn-secondary btn-gradient" id="listbullet"
        title="Bulleted List" class="btniOSUX separator" />
    <input type="btn btn-secondary btn-gradient" id="liststructured"
        title="Structured List" class="btniOSUX" />
</div>

The CSS for the DIV and the INPUT elements looks like this:

#iOSUXBar {
    border-top: 1px #bbbbbb solid;
    border-bottom: 1px #bbbbbb solid;
    padding: 5px;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.btniOSUX {
    background-repeat: no-repeat;
    background-position: center;
    border: none;
    background-color: transparent;
    width: 50px;
    height: 50px;
    border: 1px solid #c2c2c2;
    border-radius: 2px;
}

#fontbold {
    background-image: url(\'images/fontbold.png\');
}

.btniOSUX:hover {
    background-color: #d5e1f2;
}

.btniOSUX-selected {
    background-color: #c2d5f2;
}

The following Javascript adds the click event to the newly inserted bold button:

[from index.aspx]

$(\'#fontbold\').click(function(){
    if ($(this).hasClass(\'btniOSUX-selected\')) {
        $(this).removeClass(\'btniOSUX-selected\');
        TXTextControl.sendCommand(TXTextControl.Command.InputFormat,
            TXTextControl.InputFormatProperty.Bold, 0);
    }
    else
    {
        $(this).addClass(\'btniOSUX-selected\');
        TXTextControl.sendCommand(TXTextControl.Command.InputFormat,
            TXTextControl.InputFormatProperty.Bold, 1);
    }

    TXTextControl.focus();
})

Web.TextControl will raise the inputFormatReceived event, if the InputFormat at the current input position has been changed. The following Javascript attaches the event handler inputFormatReceivedHandler to the event:

[from index.aspx]

document.addEventListener("inputFormatReceived", inputFormatReceivedHandler);

function inputFormatReceivedHandler(e) {
    var msg = e.detail;
    if (msg.hasOwnProperty("Bold"))
        inputFormatChanged(TXTextControl.InputFormatProperty.Bold, msg.Bold);
}

The function inputFormatChanged changes the state of the buttons to reflect the input format at the current input position:

[from index.aspx]

function inputFormatChanged(id, value, stringValue) {
    var btns = null;

    switch (id) {
        case TXTextControl.InputFormatProperty.Bold:
            btns = [$("#fontbold")];
            break;
    }

    if (btns) {
        btns.forEach(function (btn) {
            if (value) btn.addClass(\'btniOSUX-selected\');
            else btn.removeClass(\'btniOSUX-selected\');
        });
    }
}

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET Server (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


ASP.NETReportingGitHub

ASP.NET MVC: Implementing a Simplistic, Custom Button Bar

This ASP.NET MVC sample replaces the Web.TextControl ribbon bar with a custom button bar built in HTML, CSS, and JavaScript. Toggle buttons apply formatting commands like bold, while the…


ASP.NETReportingGitHub

ASP.NET MVC: Adding Protected Sections to Documents

SubTextParts in TX Text Control mark document regions as non-editable in Web.TextControl. An MVC controller method converts selected text into a SubTextPart using ServerTextControl, and…


ASP.NETReportingElectronic Signature

ASP.NET: Adding Electronic Signatures to Documents

An ASP.NET MVC sample demonstrates electronic signatures using an HTML5 canvas-based JavaScript signature pad. The captured signature image is sent to a controller action, which merges it into a…

Share on this blog post on: