4 Description: Defines ULList, which represents a doubly linked list
5 in which each item has a unique identifier stored in the `uid` field.
15 /* Add an item to the beginning of the list */
16 ULList.prototype.prepend = function(item) {
17 if(this.first !== null) {
18 item.next = this.first;
19 this.first.prev = item;
24 this.first.prev = null;
29 /* Add an item to the end of the list */
30 ULList.prototype.append = function(item) {
31 if(this.last !== null) {
32 item.prev = this.last;
33 this.last.next = item;
38 this.last.next = null;
43 /* Insert an item after one which has a specified UID */
44 ULList.prototype.insertAfter = function(item, uid) {
45 var after = this.find(uid);
51 item.next = after.next;
53 item.next.prev = item;
58 if(after == this.last)
66 /* Insert an item before one that has a specified UID */
67 ULList.prototype.insertBefore = function(item, uid) {
68 var before = this.find(uid);
75 item.prev = before.prev;
77 item.prev.next = item;
80 // New beginning of list
81 if(before == this.first)
89 /* Remove an item from the list */
90 ULList.prototype.remove = function(uid) {
91 var item = this.find(uid);
95 // Boundary conditions
96 if(item == this.first)
97 this.first = item.next;
99 this.last = item.prev;
103 item.prev.next = item.next;
105 item.next.prev = item.prev;
111 /* Find an element in the list, return false if specified UID not found */
112 ULList.prototype.find = function(uid) {
113 // Can't possibly find it in an empty list
114 if(this.first === null)
117 var item = this.first;
118 var iter = this.first;
119 while(iter !== null && item.uid != uid) {
124 if(item && item.uid == uid)
129 /* Clear all elements from the list */
130 ULList.prototype.clear = function() {
136 /* Dump the contents of the list into an array */
137 ULList.prototype.toArray = function(pack) {
138 var arr = new Array(this.length);
139 var item = this.first;
141 while(item !== null) {
142 if(pack !== false && typeof item.pack == "function")
143 arr[i++] = item.pack();
151 /* iterate across the playlist */
152 ULList.prototype.forEach = function (fn) {
153 var item = this.first;
154 while(item !== null) {
160 /* find a media with the given video id */
161 ULList.prototype.findVideoId = function (id) {
162 var item = this.first;
163 while(item !== null) {
164 if(item.media && item.media.id === id)
171 ULList.prototype.findAll = function(fn) {
173 this.forEach(function(item) {
181 module.exports = ULList;