FANDOM


//
Modules = (function () {
 
    var length = 0,
        id = 0,
        first = false;
 
    var Node = function (properties) {
 
        this.up = this;
        this.down = this;
        this.id = id++;
        this.addOns = [];
 
        for (var i in properties) {
            this[i] = properties[i];
            this.addOns.push(i);
        }
 
        function extract (node) {
            node.down.up = node.up;
            node.up.down = node.down;
        }
 
        function move (node, position) {
            var walker = first;
            for (var i = 1; i < position; i++) {
                walker = walker.down;
            }
            if (node === walker) return;
            if (node === walker.up) {
                node.swapWith(walker);
            } else {
                extract(node);
                node.down = walker;
                node.up = walker.up;
                walker.up.down = node;
                walker.up = node;
            }
        }
 
        function stripProps (node) {
            var values = {};
            for (i = 0; i < node.addOns.length; i++) {
                key = node.addOns[i];
                values[key] = node[key];
                delete node[key];
            }
            values.id = node.id;
            node.addProps = function (values) {
                for (i in values) {
                    this[i] = values[i];
                }
                delete this.addProps;
            }
            return values;
        }
 
        this.remove = function () {
            if (--length < 0) return;
            if (length) {
                extract(this);
            } else {
                first = false;
            }
            if (this === first) {
                first = this.down;
            }
        };
 
        this.getPosition = function () {
            var p = 0, self = this;
            RotatingList.getByFilter(function (node) {
                p++; return node === self;
            });
            return p;
        };
 
        this.swapWith = function (node) {
            var sourceValues = stripProps(this);
            var targetValues = stripProps(node);
            this.addProps(targetValues);
            node.addProps(sourceValues);
        };
 
        this.moveToTop = function () {
            if (this === first) return;
            if (this === first.up) {
                first = first.up;
            } else {
                move(this, 1);
                first = this;
            }
        };
 
        this.moveToBottom = function () {
            if (this === first.up) return;
            if (this === first) {
                first = first.down;
            } else {
                move(this, length + 1);
            }
        }
 
        this.moveToPosition = function (position) {
            if (position === 1) {
                this.moveToTop();
            } else if (position === length) {
                this.moveToBottom();
            } else {
                move(this, position);
            }
        };
 
        this.log = function (prefix) {
 
            var feedback = [];
            feedback.add = function (key, value) {
                if (value.toString().length > 20) {
                    value = value.toString().substring(0, 20) + '...';
                }
                this.push(key + '[' + value + ']');
            }
 
            feedback.add('position', this.getPosition());
            feedback.add('id', this.id);
            feedback.add('up.id', this.up.id);
            feedback.add('down.id', this.down.id);
            for (var i = 0; i < this.addOns.length; i++) {
                feedback.add(this.addOns[i], this[this.addOns[i]]);
            }
            if ('undefined' === typeof prefix) prefix = '';
            console.log(prefix + feedback.join(', '));
        }
    };
 
    var RotatingList = {
 
        add: function (properties) {
            if ('undefined' === typeof properties) {
                properties = {};
            }
            var node = new Node(properties);
            switch (length) {
                case 0:
                    first = node;
                    break;
                case 1:
                    first.down = node;
                    first.up = node;
                    node.down = first;
                    node.up = first;
                    break;
                default:
                    node.up = first.up;
                    first.up = node;
                    node.down = first;
                    node.up.down = node;
            }
            length++;
            return node.id;
        },
 
        getFirst: function () {
            return first;
        },
 
        getLast: function () {
            return first.up;
        },
 
        getLength: function () {
            return length;
        },
 
        getByPosition: function (position) {
            var p = 1, m = this.getByFilter(function () {
                return p++ === position;
            });
            if (!m) throw new Error('unoccupied position');
            return m;
        },
 
        getById: function (id) {
            var m = this.getByFilter(function (node) {
                return node.id === id;
            });
            if (!m) throw new Error('unknown id');
            return m;
        },
 
        getByFilter: function (filter) {
            var walker = first;
            if (!length) return false;
            do {
                if (filter(walker)) return walker;
                walker = walker.down;
            } while (walker !== first);
            return false;
        },
 
        each: function (callback) {
            var walker = first;
            for (var i = 0; i < length; i++) {
                callback.apply(walker);
                walker = walker.down;
            }
        },
 
        downwards: function (amount) {
            for (var i = 0; i < (amount || 1); i++) {
                first = first.up;
            }
        },
 
        upwards: function (amount) {
            for (var i = 0; i < (amount || 1); i++) {
                first = first.down;
            }
        },
 
        log: function () {
            console.log('---------------------------');
            this.each(function () {
                this.log();
            });
            console.log('---------------------------');
        }
    };
 
    return RotatingList;
 
}());
 
console.clear();
 
console.log('creating a few modules:');
Modules.add({ name: 'one',   text: 'Lorem ipsum dolor sit amet' });
Modules.add({ name: 'two',   text: 'Lorem ipsum dolor sit amet' });
Modules.add({ name: 'three', text: 'Lorem ipsum dolor sit amet' });
Modules.add({ name: 'four',  text: 'Lorem ipsum dolor sit amet' });
Modules.add({ name: 'five',  text: 'Lorem ipsum dolor sit amet' });
Modules.add({ name: 'six',   text: 'Lorem ipsum dolor sit amet' });
Modules.log();
 
console.log('id of first module: ', Modules.getFirst().id);
console.log('id of last module: ', Modules.getLast().id);
console.log('number of modules: ', Modules.getLength());
console.log('---------------------------');
 
console.log('removing module with id 3:');
Modules.getById(3).remove();
Modules.log();
 
console.log('moving module with id 2 to 2nd position:');
Modules.getById(2).moveToPosition(2);
Modules.log();
 
console.log('modifying all "text" properties:');
Modules.each(function () {
    this.text = '<b>' + this.text + '</b>';
});
Modules.log();
 
console.log('rotate all modules 1 position down');
Modules.downwards();
Modules.log();
 
console.log('rotate all modules 2 positions up');
Modules.upwards(2);
Modules.log();
 
console.log('moving module from 4th position to first:');
Modules.getByPosition(4).moveToPosition(1);
Modules.log();
 
console.log('moving module from 5th position to first:');
Modules.getByPosition(5).moveToTop();
Modules.log();
 
console.log('moving module from 2nd position to 5th:');
Modules.getByPosition(2).moveToPosition(5);
Modules.log();
 
console.log('moving module from 3rd position to 5th:');
Modules.getByPosition(3).moveToBottom();
Modules.log();
 
console.log('moving module from 4th position to 5th:');
Modules.getByPosition(4).moveToPosition(5);
Modules.log();
 
console.log('moving module from 1st position to 5th:');
Modules.getByPosition(1).moveToPosition(5);
Modules.log();
 
console.log('moving module from 5th position to 1st:');
Modules.getByPosition(5).moveToTop();
Modules.log();
 
console.log('moving module from 3rd position to 4th:');
Modules.getByPosition(3).moveToPosition(4);
Modules.log();
 
console.log('moving module from 2nd position to 3rd:');
Modules.getByPosition(2).moveToPosition(3);
Modules.log();
 
console.log('moving module from 3rd position to 2nd:');
Modules.getByPosition(3).moveToPosition(2);
Modules.log();
 
//

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.