~daker/ubuntu-html5-theme/fix.1240682

« back to all changes in this revision

Viewing changes to 0.1/ambiance/js/list.js

  • Committer: daker
  • Date: 2013-10-30 20:51:51 UTC
  • mfrom: (87.2.1 ambiance)
  • Revision ID: adnane002@gmail.com-20131030205151-e1e9v5eb7h80h2in
Fixed list.js

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 * This file is part of ubuntu-html5-theme.
6
6
 *
7
7
 * This package is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU Lesser General Public License as 
9
 
 * published by the Free Software Foundation; either version 3 of the 
 
8
 * it under the terms of the GNU Lesser General Public License as
 
9
 * published by the Free Software Foundation; either version 3 of the
10
10
 * License, or
11
11
 * (at your option) any later version.
12
 
 
 
12
 
13
13
 * This package is distributed in the hope that it will be useful,
14
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
16
 * GNU General Public License for more details.
17
 
 
18
 
 * You should have received a copy of the GNU Lesser General Public 
19
 
 * License along with this program. If not, see 
 
17
 
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this program. If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
24
24
var List = (function () {
25
25
    var LIST_DATA_ROLE = 'list';
26
26
 
27
 
    var __addUlIfNotFound = function(list) {
28
 
        if (list) {
29
 
            var uls = list.querySelectorAll('ul');
30
 
            if (uls == null || uls.length == 0) {
31
 
                var ul = document.createElement('ul');
32
 
                list.appendChild(ul);
33
 
            }
34
 
        }
 
27
    var __addUlIfNotFound = function (list) {
 
28
        if (list) {
 
29
            var uls = list.querySelectorAll('ul');
 
30
            if (uls == null || uls.length == 0) {
 
31
                var ul = document.createElement('ul');
 
32
                list.appendChild(ul);
 
33
            }
 
34
        }
35
35
    };
36
36
 
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>');
41
 
        }
42
 
        this._list = list;
 
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>');
 
41
        }
 
42
        this._list = list;
43
43
 
44
 
        __addUlIfNotFound(this._list);
 
44
        __addUlIfNotFound(this._list);
45
45
    };
46
46
 
47
47
    List.prototype = {
48
 
        setHeader: function (text) {
49
 
            if (typeof(text) == 'string') {
50
 
                var header = this._list.querySelectorAll('header');
51
 
                if (header) {
52
 
                    if (header.length > 1) {
53
 
                        // more than one <header> detected
54
 
                        throw new Error("More than one <header> tag detected");
55
 
                    }
56
 
                    if (header.length == 1)
57
 
                        header = header[0];
58
 
                    else
59
 
                        header = null;
60
 
                }
61
 
 
62
 
                if (!header)
63
 
                    header = document.createElement('header');
64
 
 
65
 
                header.innerText = text;
66
 
            }
67
 
        },
68
 
        /*
69
 
          \brief Appends a item to the list
70
 
          
71
 
          Appends a given item to the current list.
72
 
          
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
76
 
                         user_data (if any).
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');
 
51
                if (header) {
 
52
                    if (header.length > 1) {
 
53
                        // more than one <header> detected
 
54
                        throw new Error("More than one <header> tag detected");
 
55
                    }
 
56
                    if (header.length == 1)
 
57
                        header = header[0];
 
58
                    else
 
59
                        header = null;
 
60
                }
 
61
 
 
62
                if (!header)
 
63
                    header = document.createElement('header');
 
64
 
 
65
                header.innerText = text;
 
66
            }
 
67
        },
 
68
        /*
 
69
      \brief Appends a item to the list
 
70
 
 
71
      Appends a given item to the current list.
 
72
 
 
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
 
76
             user_data (if any).
 
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
82
 
         */
83
 
        append: function (text, label, id, onclick, user_data) {
84
 
            var li = document.createElement('li');
85
 
            var a = document.createElement('a');
86
 
 
87
 
            a.setAttribute('href', '#');
88
 
            if (onclick && typeof(onclick) == 'function') {
89
 
                li.addEventListener('click', function(event) {
90
 
                    onclick(event ? event.target : null, user_data);
91
 
                    if (event)
92
 
                        event.preventDefault();
93
 
                });
94
 
            }
95
 
            //FIXME: no real checks on text content
96
 
            a.innerText = text;
97
 
            li.appendChild(a);
98
 
 
99
 
            if (label && typeof(label) == 'string') {
100
 
                var n = document.createElement('label');
101
 
                n.innerText = label;
102
 
                li.appendChild(n);
103
 
            }
104
 
 
105
 
            if (id && typeof(id) == 'string') {
106
 
                if ( ! document.getElementById(id))
107
 
                    li.setAttribute('id', id);
108
 
            }
109
 
            this._list.querySelector('ul').appendChild(li);
110
 
 
111
 
            return li;
112
 
        },
113
 
        /*
114
 
          \brief returns the nth child item from a list
115
 
          
116
 
          \param index Index of the child to return (0 based number)
117
 
          \return selected item node or null
118
 
         */
119
 
        at: function (index) {
120
 
            if (typeof(index) != 'number')
121
 
                return null;
122
 
            return this._list.querySelector('ul').querySelector('li:nth-child(' + index + ')');
123
 
        },
124
 
        /*
125
 
          \brief removes the nth child item from a list
126
 
          
127
 
          \param index Index of the child to remove (0 based number)
128
 
          \return nothing
129
 
         */
130
 
        remove: function (index) {
131
 
            var item = this.at(index);
132
 
            if (item) {
133
 
                item.parendNode.removeChild(item);
134
 
            }
135
 
        },
136
 
        /*
137
 
          \brief removes all the child items from a list
138
 
          \return nothing
139
 
         */
140
 
        removeAllItems: function() {
141
 
            if (this._list.querySelector('ul'))
142
 
                this._list.querySelector('ul').innerHTML = '';
143
 
        },
144
 
        /*
145
 
          \brief iterator over the list of list items and calls a function on each one
146
 
          
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
149
 
                      index.
150
 
          \return nothing
151
 
        */
152
 
        forEach: function (func) {
153
 
            if (typeof(func) !== 'function')
154
 
                return;
155
 
            var items = this._list.querySelector('ul').querySelectorAll('li');
156
 
            Array.prototype.forEach.call(items, function (element, index) {
157
 
                func(element, index);
158
 
            });
159
 
        },
 
80
                                  when the event has been trigger.
 
81
      \return added item node or null
 
82
     */
 
83
        append: function (text, label, id, onclick, user_data) {
 
84
            var li = document.createElement('li');
 
85
            var a = document.createElement('a');
 
86
 
 
87
            a.setAttribute('href', '#');
 
88
            if (onclick && typeof (onclick) == 'function') {
 
89
                li.addEventListener('click', function (event) {
 
90
                    onclick(event ? event.target : null, user_data);
 
91
                    if (event)
 
92
                        event.preventDefault();
 
93
                });
 
94
            }
 
95
            //FIXME: no real checks on text content
 
96
            a.innerText = text;
 
97
            li.appendChild(a);
 
98
 
 
99
            if (label && typeof (label) == 'string') {
 
100
                var n = document.createElement('label');
 
101
                n.innerText = label;
 
102
                li.appendChild(n);
 
103
            }
 
104
 
 
105
            if (id && typeof (id) == 'string') {
 
106
                if (!document.getElementById(id))
 
107
                    li.setAttribute('id', id);
 
108
            }
 
109
            this._list.querySelector('ul').appendChild(li);
 
110
 
 
111
            return li;
 
112
        },
 
113
        /*
 
114
      \brief returns the nth child item from a list
 
115
 
 
116
      \param index Index of the child to return (0 based number)
 
117
      \return selected item node or null
 
118
     */
 
119
        at: function (index) {
 
120
            if (typeof (index) != 'number')
 
121
                return null;
 
122
            return this._list.querySelector('ul').querySelector('li:nth-child(' + index + ')');
 
123
        },
 
124
        /*
 
125
      \brief removes the nth child item from a list
 
126
 
 
127
      \param index Index of the child to remove (0 based number)
 
128
      \return nothing
 
129
     */
 
130
        remove: function (index) {
 
131
            var item = this.at(index);
 
132
            if (item) {
 
133
                item.parentNode.removeChild(item);
 
134
            }
 
135
        },
 
136
        /*
 
137
      \brief removes all the child items from a list
 
138
      \return nothing
 
139
     */
 
140
        removeAllItems: function () {
 
141
            if (this._list.querySelector('ul'))
 
142
                this._list.querySelector('ul').innerHTML = '';
 
143
        },
 
144
        /*
 
145
      \brief iterator over the list of list items and calls a function on each one
 
146
 
 
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
 
149
              index.
 
150
      \return nothing
 
151
    */
 
152
        forEach: function (func) {
 
153
            if (typeof (func) !== 'function')
 
154
                return;
 
155
            var items = this._list.querySelector('ul').querySelectorAll('li');
 
156
            Array.prototype.forEach.call(items, function (element, index) {
 
157
                func(element, index);
 
158
            });
 
159
        },
160
160
    };
161
161
 
162
162
    return List;
163
 
}) ();
164
 
 
165
 
 
 
163
})();
 
 
b'\\ No newline at end of file'