MediaWiki:FandomizedChat/library.js

var FandomizedChat = { version: '1.0.0 beta', loaded: true, storeUserList: true, storeUserData: true, Api: function(method, data, callback){ var _method = method.indexOf(':') > -1 ? method.split(':')[0] : method, _type = method.indexOf(':') > -1 ? method.split(':')[1] : 'json', xhr = null; if (typeof callback !== 'undefined'){ xhr = $.ajax({                   method: _method,                    dataType: _type,                    url: mw.config.get('wgScriptPath', wgScriptPath) + mw.util.wikiScript('api'),                    data: data                }); if (typeof callback == 'object'){ if (!(callback instanceof Object)) throw new TypeError('The callback must be a function or an object'); Object.keys(callback).forEach(function(name){                       switch (name){                            case 'complete':                            case 'success':                            case 'done':                                xhr.done(callback[name]);                                break;                            case 'fail':                            case 'error':                                xhr.fail(callback[name]);                                break;                            case 'always':                                xhr.always(callback[name]);                                break;                            default:                                throw new Error('Not a valid callback name.');                        }                    }); } else if (typeof callback == 'function'){ xhr.done(callback); }           } else { xhr = $.ajax({                   method: _method,                    dataType: _type,                    url: mw.config.get('wgScriptPath', wgScriptPath) + mw.util.wikiScript('api'),                    data: data                }); return xhr; }       }    }; FandomizedChat = $.extend(FandomizedChat, {    getJSON: function(data, callback){        return FandomizedChat.Api('GET:json', data, callback);    },    postJSON: function(data, callback){        return FandomizedChat.Api('POST:json', data, callback);    },    getHTML: function(data, callback){        return FandomizedChat.Api('GET:html', data, callback);    },    getXML: function(data, callback){        return FandomizedChat.Api('GET:xml', data, callback);    },    getPlainText: function(data, callback){        return FandomizedChat.Api('GET:text', data, callback);    } });

if (FandomizedChat.storeUserList){ FandomizedChat = $.extend(FandomizedChat, {       createUserList: function(groups){            var users = mainRoom.model.users,                userlist = null;            if (typeof groups !== 'undefined'){                if (typeof groups === 'string'){                    userlist = users.filter(function(child){ var _groups = child.attributes.groups, inGroup = (                               typeof Array.prototype.some !== 'undefined' ?                                Array.prototype.some.call(_groups, function(group){ return group == groups; }) : _groups.indexOf(groups) > -1                           ); return inGroup; }).map(function(child){ return child.attributes.name; });               } else if (typeof groups === 'object'){                    if (groups instanceof Array){                        userlist = users.filter(function(child){ var _groups = child.attributes.groups, inGroup = (                                   typeof Array.prototype.some !== 'undefined' ?                                    Array.prototype.some.call(_groups, function(group){ return groups.indexOf(group) > -1; }) : (function(g){ var a = g, b = null, i = 0; for (i < a.length; i++){ var group = a[i]; if (groups.indexOf(group) > -1){ b = true; break; } else { b = false; }                                       }                                        return b;                                    })(_groups)                                ); return inGroup; }).map(function(child){ return child.attributes.name; });                   }                }            } else {                userlist = users.map(function(child){ return child.attributes.name; });           }            return userlist.sort;        }    }); }