I wrote small JavaScript library for improve my level :)
Rate my code. I need this for rising

http://pastebin.com/AUcsN7Uz

Code:

var Solid;
if(Solid)
throw new Error('name Solid already taken');
(function () {
/**
 * @file src/core/core.js
 * @author Taras Kustov <[email protected]>
 **/

/**
 * Global $ function
 * @function $([node,]query)
 * @param {Node} node context for performing query
 * @param {String} query query
 **/
function $(node, query)
{
    var selector;
    var isValid = /^[#\.]?[a-z0-9\-_]+$/i;

    if(typeof node === 'string') {
        query = node;
        node = document;
    } else if(!(node && node.getElementsByClassName &&
node.getElementsByTagName)
        || typeof query !== 'string'
    )
        return null;

    if(!isValid.test(query))
        return null;

    selector = query.slice(1);

    switch(query.charAt(0))
    {
        case '#':
            return document.getElementById(selector);
        case '.':
            return node.getElementsByClassName(selector);
        default:
            return node.getElementsByTagName(query);
    }

    return null;
};

/**
 * Sequnce iterator
 * @function
 * @param {Function} action, to do with each item in sequence
 *
 * TIP: Usage
 * each.call([1, 2, 3], function(item, index)
 * {
 *     console.log(item + index);
 * });
 * -> 1
 * -> 3
 * -> 5
 **/
function each(f)
{
    if(!(f instanceof Function))
        return;

    for(var i = 0; i < this.length; i++)
        f.call(this, this[i], i);
};
Array.prototype.each = each;

/**
 * @file src/core/object.js
 * @author Taras Kustov <[email protected]>
 **/

/**
 * Implementation of the inheritance
 * @method extend
 * @param {Class} parent class
 * @param {Array} arguments for a constructor
 *
 * TIP: Call in extends class context
 * function <Child>([arguments])
 * {
 *     this.extend(<Parent>[, arguments]);
 *     ...
 * }
 **/
Object.prototype.extend = function(parent, args)
{
    var property;
    // If parent is class
    if(parent && parent.prototype)
        for(property in parent.prototype)
            // if the child class does not have such properties as the
parent
            if(!this.hasOwnProperty(property))
                // Copy it
                this[property] = parent.prototype[property];
    // Call parent constructor in child context
    parent.apply(this, args || []);
    return this;
};



Solid = {
$: $,
each: each
};
}) ();



(function() {

/**
 * @file src/widgets/Seekbar.js
 **/

/**
 * @class Seekbar
 * @namespace Solid.widgets
 * @constructor
 **/
function Seekbar(id)
{
    // TIP: Performing inheritance
    // this.extend(ParentClass[, arguments]);
    this.extend(Widget, arguments);

    this._dragging = false;
    this._shift = null;

    this.onpick = null;
    this.onrelease = null;
    this.onchange = null;

    this.setLayout({
        'track': 'div',
        'pointer': 'div'
    }, {
        'root': ['track'],
        'track': ['pointer']
    });
    this.initialize();
}

(function(Seekbar) {
    // TIP: Define methods
    // Seekbar.[methodName] = function([arguments])
    // {
    //     [implementation]
    // };
    Seekbar.initialize = function()
    {
        (function(that, track, pointer) {
            pointer.style.position = 'relative';
            pointer.onmousedown = function(e)
            {
                e = e || event;
                that._dragging = true;
                that._shift = e.clientX - pointer.offsetLeft;
                if(that.onpick instanceof Function)
                    that.onpick(event || e);
            }
            pointer.onmouseup = function(e)
            {
                e = e || event;
                that._dragging = false;
                that._shift = null;
                if(that.onrelease instanceof Function)
                    that.onrelease(e)
            };
            pointer.onmouseout = pointer.onmouseup;
            pointer.onmousemove = function(e)
            {
                e = e || event;
                var TX = track.offsetLeft + track.clientLeft;
                var TW = track.clientWidth;
                var PW = pointer.offsetWidth;
                var MX = e.clientX - TX;

                if(that._dragging) {
                    if((MX - that._shift) >= 0 && (MX - that._shift)
<= (TW - PW))
                        pointer.style.left = (MX -
that._shift).toString() + 'px';
                    if(that.onchange instanceof Function)
                        that.onchange(that.value(), e);
                }
            };
            track.onclick = function(e)
            {
                e = e || event;
                var TX = track.offsetLeft + track.clientLeft;
                var TW = track.clientWidth;
                var PW = pointer.offsetWidth;
                var MX = e.clientX - TX;

                if((MX - PW / 2) >= 0 && (MX - PW / 2) <= (TW - PW))
                    pointer.style.left = (MX - PW / 2).toString() +
'px';
                if(that.onchange instanceof Function)
                    that.onchange(that.value(), e);
            };
        }) (this, this.layout.track, this.layout.pointer);
    };

    Seekbar.value = function()
    {
        return (function(track, pointer) {
            var TX = track.offsetLeft + track.clientLeft;
            var TW = track.clientWidth;
            var PX = pointer.offsetLeft - TX;
            var PW = pointer.offsetWidth;
            return PX / (TW - PW);
        }) (this.layout.track, this.layout.pointer);
    }
}) (Seekbar.prototype);

/**
 * @file src/widgets/Widget.js
 **/

/**
 * @class Widget
 * @namespace Solid.widgets
 * @constructor
 **/
function Widget(id)
{
    // TIP: Performing inheritance
    // this.extend(ParentClass[, arguments]);
    this.id = (typeof id === 'string')? id: 'widget';
    this.container = null;
    this.layout = null;
}

(function(Widget, $) {
    // TIP: Define methods
    // Widget.[methodName] = function([arguments])
    // {
    //     [implementation]
    // };
    Widget.setContainer = function(e)
    {
        if(typeof e === 'string')
            e = $('#' + e);
        if(e && e.appendChild) {
            this.container = e;
            if(this.layout && this.layout.hasOwnProperty('root'))
                this.container.appendChild(this.layout.root);
        }
        return this;
    };

    Widget.setLayout = function(entrys, scheme)
    {
        var layout = {
            root: document.createElement('div')
        };
        layout.root.id = this.id;
        for(var i in entrys)
            if(typeof entrys[i] === 'string') {
                layout[i] = document.createElement(entrys[i]);
                layout[i].className = i;
            }
        for(var i in scheme)
            if(scheme[i] instanceof Array)
                for(var j = 0; j < scheme[i].length; j++)
                    layout[i].appendChild(layout[scheme[i][j]]);
        this.layout = layout;
        return this;
    };

    Widget.getStyleText = function()
    {
        var css = '';
        var selectors;
        var node;
        for(var i in this.layout) {
            if(!this.layout.hasOwnProperty(i))
                continue;
            selectors = [];
            node = this.layout[i];
            do {
                selectors.push((node.id !== '')? '#' + node.id: '.' +
node.className);
            } while((node = node.parentNode) !==
this.layout.root.parentNode);
            css += selectors.reverse().join(' ') + ' {\n}\n';
        }
        return css;
    };
}) (Widget.prototype, Solid.$);



Solid.widgets = {
Widget: Widget,
Seekbar: Seekbar
};
}) ();

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to