Click here to Skip to main content
15,896,606 members
Articles / Web Development / HTML

JavaScript UI Control Suite using TypeScript

Rate me:
Please Sign up or sign in to vote.
3.20/5 (4 votes)
7 Dec 2014CPOL12 min read 40.4K   32  
A usable suite of JavaScript UI controls written with TypeScript.
var __extends = this.__extends || function (d, b) {
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
}
var Zenith;
(function (Zenith) {
    var CheckBoxList = (function (_super) {
        __extends(CheckBoxList, _super);
        function CheckBoxList(baseDivElementId) {
                _super.call(this, baseDivElementId);
            this.NumColumns = 1;
            this.ColumnSpace = 10;
            this.ItemList = new Zenith.List();
        }
        CheckBoxList.prototype.AddItem = function (value, text) {
            this.ItemList.Add(new Zenith.ListItem(value, text));
        };
        CheckBoxList.prototype.AddArrayData = function (data) {
            for(var index = 0; index < data.length; index++) {
                this.AddItem(data[index][0], data[index][1]);
            }
        };
        CheckBoxList.prototype.AddJSONData = function (data, valueDataField, textDataField) {
            if(!valueDataField || valueDataField.length <= 0) {
                valueDataField = 'Value';
            }
            if(!textDataField || textDataField.length <= 0) {
                textDataField = 'Text';
            }
            for(var index = 0; index < data.length; index++) {
                this.AddItem(data[index][valueDataField], data[index][textDataField]);
            }
        };
        CheckBoxList.prototype.Build = function () {
            var _this = this;
            if(this.ItemList.Count() <= 0) {
                throw new Error("The item list is empty.");
            }
            this.Clear();
            var table = document.createElement('table');
            this.BaseElement.appendChild(table);
            table.className = 'ZenithCheckBoxTable';
            var tbody = document.createElement('tbody');
            table.appendChild(tbody);
            var trow;
            var tcell;

            var colIndex = 0;
            for(var index = 0; index < this.ItemList.Count(); index++) {
                if(!trow || colIndex >= this.NumColumns) {
                    trow = document.createElement('tr');
                    tbody.appendChild(trow);
                    colIndex = 0;
                }
                tcell = document.createElement('td');
                trow.appendChild(tcell);
                if(colIndex > 0) {
                    tcell.style.paddingLeft = this.ColumnSpace + "px";
                }
                this.addEventListener(tcell, 'click', function (event) {
                    _this.selectedEventHandler(event);
                });
                var itemCheckbox = document.createElement('input');
                itemCheckbox.type = 'checkbox';
                itemCheckbox.name = 'ZenithControlCheckBox';
                itemCheckbox.value = this.ItemList.ElementAt(index).Value;
                itemCheckbox.id = 'chk_' + this.ItemList.ElementAt(index).Value;
                tcell.appendChild(itemCheckbox);
                var label = document.createElement('label');
                label.htmlFor = 'chk_' + this.ItemList.ElementAt(index).Value;
                label.className = 'ZenithCheckBoxLabel_Unselected';
                label.textContent = this.ItemList.ElementAt(index).Text;
                label.innerHTML = this.ItemList.ElementAt(index).Text;
                label.style.width = "150px";
                tcell.appendChild(label);
                colIndex++;
            }
            this.ParentElement = table;
            if(this.IsPopup()) {
                _super.prototype.SetPopup.call(this);
            }
            _super.prototype.Build.call(this);
        };
        CheckBoxList.prototype.SelectedValues = function () {
            var selectedValues = [];
            var checkboxes = this.BaseElement.getElementsByTagName('input');
            for(var index = 0; index < checkboxes.length; index++) {
                if(checkboxes[index] instanceof HTMLInputElement) {
                    if((checkboxes[index]).checked) {
                        selectedValues.push((checkboxes[index]).value);
                    }
                }
            }
            return selectedValues;
        };
        CheckBoxList.prototype.SelectedNames = function () {
            var selectedValues = [];
            var checkboxes = this.BaseElement.getElementsByTagName('input');
            for(var index = 0; index < checkboxes.length; index++) {
                if(checkboxes[index] instanceof HTMLInputElement) {
                    if((checkboxes[index]).checked) {
                        selectedValues.push(((checkboxes[index]).nextSibling).textContent);
                    }
                }
            }
            return selectedValues;
        };
        CheckBoxList.prototype.SetChecked = function (selectedValues) {
            var checkboxes = this.BaseElement.getElementsByTagName('input');
            for(var index = 0; index < checkboxes.length; index++) {
                if(checkboxes[index] instanceof HTMLInputElement) {
                    var checkbox = checkboxes[index];
                    checkbox.checked = false;
                    if(checkbox.nextElementSibling) {
                        var label = checkbox.nextElementSibling;
                        if(label) {
                            for(var valuesIndex = 0; valuesIndex < selectedValues.length; valuesIndex++) {
                                if(selectedValues[valuesIndex] == checkbox.value) {
                                    label.className = 'ZenithCheckBoxLabel_Selected';
                                    checkbox.checked = true;
                                } else {
                                    label.className = 'ZenithCheckBoxLabel_Unselected';
                                }
                            }
                        }
                    }
                }
            }
        };
        CheckBoxList.prototype.selectedEventHandler = function (event) {
            var targetElement = event.srcElement;
            if(!targetElement) {
                targetElement = event.target;
            }
            if(targetElement) {
                var checkbox = null;
                if(targetElement instanceof HTMLInputElement) {
                    checkbox = targetElement;
                } else {
                    if(targetElement.childElementCount > 0) {
                        checkbox = targetElement.childNodes[0];
                        checkbox.checked = !checkbox.checked;
                    }
                }
                this.SetOutput(this.SelectedValues());
                if(checkbox) {
                    if(checkbox instanceof HTMLInputElement) {
                        if(checkbox.nextElementSibling) {
                            var label = checkbox.nextElementSibling;
                            label.className = 'ZenithCheckBoxLabel_Selected';
                            _super.prototype.ExecuteEvent.call(this, Zenith.ZenithEvent.EventType.Selected, [
                                checkbox.value, 
                                checkbox.nextElementSibling.textContent, 
                                checkbox.checked
                            ]);
                        }
                    }
                }
            }
        };
        return CheckBoxList;
    })(Zenith.ControlBase);
    Zenith.CheckBoxList = CheckBoxList;    
})(Zenith || (Zenith = {}));

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
For over 25 years I have worked in the Information Systems field as both a full-time employee and an independent contractor for a variety of companies.

I have extensive professional experience with numerous programming languages and technologies including C#, JavaScript, SQL, VB.NET, and ASP.NET as well as a working knowledge of a great variety of others. I also have an advanced understanding of the concepts behind these technologies including Object-Oriented Programming, Relational Data, Functional Programming, MVC and MVVM.

Some of my more recent work has been designing and building web applications primarily with JavaScript in conjunction with many of the JavaScript libraries/frameworks including jQuery, KnockoutJS and Bootstrap and consuming both JSON and REST services.

In nearly all of the work I have been involved with in the past ten years I have played a lead role in the design as well as the development of the work. More recently I have managed a team of software developers at a local mid-size company.

Comments and Discussions