24
24
var List = (function () {
25
25
var LIST_DATA_ROLE = 'list';
27
var __addUlIfNotFound = function(list) {
29
var uls = list.querySelectorAll('ul');
30
if (uls == null || uls.length == 0) {
31
var ul = document.createElement('ul');
27
var __addUlIfNotFound = function (list) {
29
var uls = list.querySelectorAll('ul');
30
if (uls == null || uls.length == 0) {
31
var ul = document.createElement('ul');
37
37
var List = function (selector) {
38
var list = document.querySelector(selector);
39
if (list == null || list.nodeName.toLowerCase() !== 'section' || list.getAttribute('data-role') != LIST_DATA_ROLE) {
40
throw new Error('Element with selector "' + selector + '" does not exist or not declared as a "list" <section>');
38
var list = document.querySelector(selector);
39
if (list == null || list.nodeName.toLowerCase() !== 'section' || list.getAttribute('data-role') != LIST_DATA_ROLE) {
40
throw new Error('Element with selector "' + selector + '" does not exist or not declared as a "list" <section>');
44
__addUlIfNotFound(this._list);
44
__addUlIfNotFound(this._list);
48
setHeader: function (text) {
49
if (typeof(text) == 'string') {
50
var header = this._list.querySelectorAll('header');
52
if (header.length > 1) {
53
// more than one <header> detected
54
throw new Error("More than one <header> tag detected");
56
if (header.length == 1)
63
header = document.createElement('header');
65
header.innerText = text;
69
\brief Appends a item to the list
71
Appends a given item to the current list.
73
\param text A string of text (images & custom html not supported at the moment)
74
\param onclick (optional) A function callback that is to be called when the item is clicked.
75
It will be called with the clicked node as a parameter along with the value of
77
\param id (optional) An optional Id for the added node, the id will only be added if it does not already
78
conflict with another one.
48
setHeader: function (text) {
49
if (typeof (text) == 'string') {
50
var header = this._list.querySelectorAll('header');
52
if (header.length > 1) {
53
// more than one <header> detected
54
throw new Error("More than one <header> tag detected");
56
if (header.length == 1)
63
header = document.createElement('header');
65
header.innerText = text;
69
\brief Appends a item to the list
71
Appends a given item to the current list.
73
\param text A string of text (images & custom html not supported at the moment)
74
\param onclick (optional) A function callback that is to be called when the item is clicked.
75
It will be called with the clicked node as a parameter along with the value of
77
\param id (optional) An optional Id for the added node, the id will only be added if it does not already
78
conflict with another one.
79
79
\param user_data (optional) An javascript entity that will be passed to the onclick callback (if any)
80
when the event has been trigger.
81
\return added item node or null
83
append: function (text, label, id, onclick, user_data) {
84
var li = document.createElement('li');
85
var a = document.createElement('a');
87
a.setAttribute('href', '#');
88
if (onclick && typeof(onclick) == 'function') {
89
li.addEventListener('click', function(event) {
90
onclick(event ? event.target : null, user_data);
92
event.preventDefault();
95
//FIXME: no real checks on text content
99
if (label && typeof(label) == 'string') {
100
var n = document.createElement('label');
105
if (id && typeof(id) == 'string') {
106
if ( ! document.getElementById(id))
107
li.setAttribute('id', id);
109
this._list.querySelector('ul').appendChild(li);
114
\brief returns the nth child item from a list
116
\param index Index of the child to return (0 based number)
117
\return selected item node or null
119
at: function (index) {
120
if (typeof(index) != 'number')
122
return this._list.querySelector('ul').querySelector('li:nth-child(' + index + ')');
125
\brief removes the nth child item from a list
127
\param index Index of the child to remove (0 based number)
130
remove: function (index) {
131
var item = this.at(index);
133
item.parendNode.removeChild(item);
137
\brief removes all the child items from a list
140
removeAllItems: function() {
141
if (this._list.querySelector('ul'))
142
this._list.querySelector('ul').innerHTML = '';
145
\brief iterator over the list of list items and calls a function on each one
147
\param func function to be called for each list item. The function
148
receives the DOM node associated with the current item along with its
152
forEach: function (func) {
153
if (typeof(func) !== 'function')
155
var items = this._list.querySelector('ul').querySelectorAll('li');
156
Array.prototype.forEach.call(items, function (element, index) {
157
func(element, index);
80
when the event has been trigger.
81
\return added item node or null
83
append: function (text, label, id, onclick, user_data) {
84
var li = document.createElement('li');
85
var a = document.createElement('a');
87
a.setAttribute('href', '#');
88
if (onclick && typeof (onclick) == 'function') {
89
li.addEventListener('click', function (event) {
90
onclick(event ? event.target : null, user_data);
92
event.preventDefault();
95
//FIXME: no real checks on text content
99
if (label && typeof (label) == 'string') {
100
var n = document.createElement('label');
105
if (id && typeof (id) == 'string') {
106
if (!document.getElementById(id))
107
li.setAttribute('id', id);
109
this._list.querySelector('ul').appendChild(li);
114
\brief returns the nth child item from a list
116
\param index Index of the child to return (0 based number)
117
\return selected item node or null
119
at: function (index) {
120
if (typeof (index) != 'number')
122
return this._list.querySelector('ul').querySelector('li:nth-child(' + index + ')');
125
\brief removes the nth child item from a list
127
\param index Index of the child to remove (0 based number)
130
remove: function (index) {
131
var item = this.at(index);
133
item.parentNode.removeChild(item);
137
\brief removes all the child items from a list
140
removeAllItems: function () {
141
if (this._list.querySelector('ul'))
142
this._list.querySelector('ul').innerHTML = '';
145
\brief iterator over the list of list items and calls a function on each one
147
\param func function to be called for each list item. The function
148
receives the DOM node associated with the current item along with its
152
forEach: function (func) {
153
if (typeof (func) !== 'function')
155
var items = this._list.querySelector('ul').querySelectorAll('li');
156
Array.prototype.forEach.call(items, function (element, index) {
157
func(element, index);
b'\\ No newline at end of file'