/a10/web/themes/classic/skins/js/plugins/jquery.ext.js
JavaScript | 655 lines | 542 code | 96 blank | 17 comment | 119 complexity | bf59e607154edfc1ff875e21abf16bd9 MD5 | raw file
Possible License(s): BSD-3-Clause
- /*!
- * jQuery extention.
- */
- (function($, undefined) {
-
- // Test debug.
- $.debug = (function() {
- var scripts = document.getElementsByTagName('script'),
- script = scripts[scripts.length - 1];
- return !(/\.min\.js$/.test(script.getAttribute('src')));
- })();
-
- // Extend console some function to jQuery.
- $.each(['log', 'trace', 'error'], function(i, name) {
- if ($.debug && window.console && window.console[name]) {
- if ($.browser.webkit) {
- $[name] = $.proxy(window.console[name], window.console) || $.noop;
- } else {
- $[name] = window.console[name];
- }
- } else {
- $[name] = $.noop;
- }
- });
-
- // Add php function.
- $.php = $.php || {};
- (function() {
- var php = new window.PHP_JS(),
- rdashAlpha = /_([a-z]|[0-9])/ig,
- fcamelCase = function(all, letter) {
- return (letter + '').toUpperCase();
- },
- key;
-
- for (key in php) {
- if ($.isFunction(php[key]) && key !== 'constructor') {
- $.php[key.replace(rdashAlpha, fcamelCase)] = php[key];
- }
- }
- })();
-
- // Add ax special function.
- $.ax = $.ax || {};
- $.ax.CONST = $.ax.CONST || {};
-
- $.extend($.ax.CONST, {
- ASC_ERROR: 0,
- ASC_TPL: -1,
- ASC_LOGIN: -2,
- ASC_SUCCESS: -3,
- ASC_REDIRECT: -4,
- ASC_RELOAD: -5
- });
-
- $.extend($.ax, {
- t: function(message) {
- return message;
- },
-
- asset: function(url) {
- // @todo Remove the global variables.
- return '/skin/' + window.global_theme + '/' + window.global_skin + '/' + url;
- },
-
- msg: function(msg, delay, method) {
- var self = $.ax;
-
- self.msg.element = self.msg.element || $('<div class="gmsg"></div>').axMsg({classPrefix: 'gmsg-'}).prependTo('body');
- self.msg.element.axMsg(method, msg, delay);
- },
-
- success: function(msg, delay) {
- var self = $.ax;
- self.msg(msg, delay, 'success');
- },
-
- error: function(msg, delay) {
- var self = $.ax;
- self.msg(msg, delay, 'error');
- },
-
- info: function(msg, delay) {
- var self = $.ax;
- self.msg(msg, delay, 'info');
- },
-
- failure: function(msg, delay) {
- var self = $.ax;
- self.msg(msg, delay, 'error');
- },
-
- close: function() {
- var self = $.ax;
- self.msg('', 0, 'close');
- },
-
- clearUrl: function(url) {
- return (url.match(/^([^#]+)/)||[])[1];
- },
-
- format: $.validator && $.validator.format,
-
- string: function() {
- var i, len, ret = '';
-
- for (i = 0, len = arguments.length; i < len; i++) {
- if (arguments[i] !== undefined && arguments[i] !== null) {
- ret = arguments[i];
- break;
- }
- }
-
- if (typeof ret === 'boolean') {
- ret = ret ? '1' : '';
- } else if ($.isNumeric(ret)) {
- ret += '';
- }
-
- return ret;
- },
-
- rebuildUrl: function(url, params) {
- var path, query, fragment,
- pos, tmp,
- pair = {};
-
- path = query = fragment = '';
- if ((pos = url.indexOf('?')) !== -1) {
- path = url.substring(0, pos);
- query = url.substr(pos + 1);
- if ((pos = query.lastIndexOf('#')) !== -1) {
- tmp = query;
- query = tmp.substring(0, pos);
- fragment = tmp.substr(pos + 1);
- }
- } else {
- path = url;
- }
-
- if (!!query) {
- $.php.parseStr(query, pair);
- }
- query = $.php.httpBuildQuery($.extend(true, pair, params || {}));
- query = !!query ? ('?' + query) : '';
- fragment = !!fragment ? ('#' + fragment) : '';
-
- return path + query + fragment;
- },
-
- addCssRules: function(cssString) {
- var self = $.ax.addCssRules,
- cssText, heads;
-
- if (!self.style) {
- self.style = document.createElement('style');
- self.style.setAttribute('type', 'text/css');
- heads = document.getElementsByTagName('head');
- if (heads.length) {
- heads[0].appendChild(self.style);
- } else {
- document.documentElement.appendChild(self.style);
- }
- }
-
- if (self.style.styleSheet) {// IE
- self.style.styleSheet.cssText += cssString;
- } else {// W3C
- cssText = document.createTextNode(cssString);
- self.style.appendChild(cssText);
- }
- },
-
- callback: function(namespace, handler, defaults) {
- var self = $.ax.callback,
- key, keys = [], ret;
-
- self.callbacks = self.callbacks || {};
- if (typeof namespace === 'string') {
- if ($.isFunction(handler)) {
- self.callbacks[namespace] = {
- defaults: defaults || {},
- handler: handler
- };
-
- ret = self.callbacks;
- } else if (typeof handler === 'object') {
- self.callbacks[namespace] = handler;
- ret = self.callbacks;
- } else {
- ret = self.callbacks[namespace];
- }
- } else if (typeof namespace === 'object') {
- $.extend(self.callbacks, namespace);
- ret = self.callbacks;
- } else if (typeof namespace === 'boolean') {
- for (key in self.callbacks) {
- if (self.callbacks.hasOwnProperty(key)) {
- keys.push(key);
- }
- }
- ret = keys.join('\n');
- } else {
- ret = self.callbacks;
- }
-
- return ret;
- },
-
- isIpv4: function(ip) {
- return (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(ip)) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256);
- },
-
- isIpv6: function(ip) {
- return ip.match(/:/g) && ip.match(/:/g).length <= 7 && /::/.test(ip) ? (/^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i.test(ip) || /::[\da-f]{1,4}/i.test(ip)) : /^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i.test(ip);
- },
-
- ipMode: function(ip) {
- var self = $.ax;
-
- if (self.isIpv4(ip)) {
- return 4;
- } else if (self.isIpv6(ip)) {
- return 6;
- }
-
- return 0;
- },
-
- ipRange: function (from, to, max, step) {
- var self = $.ax,
- f = String.fromCharCode,
- ip6byte = 16,
- mode = self.ipMode(from),
- ret = [],
- ip, i,
- pton = function(ip) {
- var r = /^((?:[\da-f]{1,4}(?::|)){0,8})(::)?((?:[\da-f]{1,4}(?::|)){0,8})$/,
- m, j, i, x, c;
-
- ip = ip.toLowerCase();
- m = ip.match(r);
- for (j = 1; j < 4; j++) {
- if (j === 2 || m[j].length === 0) {
- continue;
- }
- m[j] = m[j].split(':');
- for (i = 0; i < m[j].length; i++) {
- m[j][i] = parseInt(m[j][i], 16);
- m[j][i] = f(m[j][i] >> 8) + f(m[j][i] & 0xFF);
- }
- m[j] = m[j].join('');
- }
- x = m[1].length + m[3].length;
- if (x === ip6byte) {
- c = m[1] + m[3];
- } else {
- c = m[1] + (new Array(ip6byte - x + 1)).join('\x00') + m[3];
- }
-
- return c;
- },
- ipadd = function(ip, step) {
- var c = [], i;
-
- for (i = 0; i < ip6byte; i++) {
- c[i] = ip.charCodeAt(i);
- }
-
- for (i = ip6byte - 1; i >= 0; i--) {
- if (i === ip6byte - 1) {
- c[i] += step;
- }
-
- if (c[i] >= 256) {
- c[i] -= 256;
- c[i - 1] += 1;
- } else {
- break;
- }
- }
-
- for (i = 0; i < ip6byte; i++) {
- c[i] = f(c[i]);
- }
-
- return c.join('');
- },
- ntop = function(ip) {
- var c = [],
- m = '',
- i;
-
- for (i = 0; i < ip6byte; i++) {
- c.push(((ip.charCodeAt(i++) << 8) + ip.charCodeAt(i)).toString(16));
- }
-
- return c.join(':').replace(/((^|:)0(?=:|$))+:?/g, function (t) {
- m = (t.length > m.length) ? t : m;
- return t;
- }).replace(m || ' ', '::');
- },
- ip2long = function(ip) {
- ip = ip.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
-
- return ip[1] * 16777216 + ip[2] * 65536 + ip[3] * 256 + ip[4] * 1;
- },
- long2ip = function(ip) {
- return Math.floor(ip / Math.pow(256, 3)) + '.' + Math.floor((ip % Math.pow(256, 3)) / Math.pow(256, 2)) + '.' + Math.floor(((ip % Math.pow(256, 3)) % Math.pow(256, 2)) / Math.pow(256, 1)) + '.' + Math.floor((((ip % Math.pow(256, 3)) % Math.pow(256, 2)) % Math.pow(256, 1)) / Math.pow(256, 0));
- };
-
- if (!mode) {
- return 1;
- }
-
- if (mode !== self.ipMode(to)) {
- return 2;
- }
-
- step = parseInt(step, 10);
- isNaN(step) && (step = 1);
- if (mode === 4) {
- from = ip2long(from);
- to = ip2long(to);
-
- if (from > to) {
- return 3;
- }
-
- ip = from;
- i = 0;
- while (ip <= to) {
- ret.push(long2ip(ip));
- ip += step;
- if (max && ++i >= max) {
- return 4;
- }
- }
- } else {
- from = pton(from);
- to = pton(to);
-
- if (from > to) {
- return 3;
- }
-
- ip = from;
- i = 0;
- while (ip <= to) {
- ret.push(ntop(ip));
- ip = ipadd(ip, step);
- if (max && ++i >= max) {
- return 4;
- }
- }
- }
-
- return ret;
- },
-
- dialog: function(id, options) {
- var self = $.ax,
- $el = $('#' + id),
- promise = {};
-
- self.dialog.list = self.dialog.list || {};
- self.dialog.list[id] = {};
- $.each(options.buttons || {}, function(name, props) {
- self.dialog.list[id][name] = $.Callbacks('once memory stopOnFalse');
- promise[name] = self.dialog.list[id][name].add;
- if (!props.click) {
- self.dialog.list[id][name].add(function() {
- $(this).axDialog('close');
- });
- } else {
- self.dialog.list[id][name].add(props.click);
- }
- });
-
- if (!$el.length) {
- $.each(options.buttons || {}, function(name, props) {
- options.buttons[name].click = function(e) {
- self.dialog.list[id][name].fireWith(this);
- e.stopPropagation();
- e.preventDefault();
- };
- });
- $el = $('<div id="' + id + '"></div>')
- .appendTo('body')
- .axDialog(options);
- }
-
- promise.element = $el;
- return promise;
- },
-
- confirm: function(message) {
- var self = $.ax,
- promise = self.dialog('JQ-AX-CONFIRM', {
- modal: true,
- title: 'Come from page message',
- autoOpen: false,
- buttons: {
- confirm: {text: 'Confirm'},
- cancel: {text: 'Cancel'}
- }
- });
-
- promise.element.html(message || 'Please give a message.').axDialog('open');
- return promise;
- },
-
- alert: function(message) {
- var $el = $('#JQ-AX-ALERT');
-
- if (!$el.length) {
- $el = $('<div id="JQ-AX-ALERT"></div>').appendTo('body');
- $el.axDialog({
- modal: true,
- title: 'Come from page message',
- autoOpen: false,
- buttons: [
- {
- text: 'Confirm',
- click: function(e) {
- $(this).axDialog('close');
- e.stopPropagation();
- e.preventDefault();
- }
- }
- ]
- });
- }
-
- $el.html(message || 'Please give a message.').axDialog('open');
- }
- });
-
- // Exend jQuery.fn
- $.extend($.fn, {
- axConfig: function(name) {
- var config = this.data('config'),
- res;
-
- if (typeof name === 'string') {
- try {
- res = config[name];
- } catch (e) {}
- } else {
- res = config;
- }
-
- return res;
- },
-
- axAjaxSubmit: function(options) {
- var stop = true, form;
-
- // using jquery.validator plugin verify the form field.
- if (this.is('form')) {
- form = this;
- stop = !form.valid();
- } else if ((form = this.find('form:first')) && form.length) {
- stop = !form.valid();
- } else {
- form = this;
- stop = !form.find(':input').not(':button').not(':hidden').valid();
- options.semantic = true;
- }
-
- if (stop) {
- return this;
- }
-
- // using jquery.form submit the form.
- form.ajaxSubmit(options);
- return this;
- },
-
- axSerializePartial: function() {
- return jQuery.param(this.axSerializePartialArray());
- },
-
- axSerializePartialArray: function() {
- return this.find(':input').serializeArray();
- }
- });
-
- // Extend jQuery.Widget.
- $.extend($.Widget.prototype, {
- _createWidget: function(options, element) {
- // $.widget.bridge stores the plugin instance, but we do it anyway
- // so that it's stored even before the _create function runs
- $.data(element, this.widgetName, this);
- this.element = $(element);
- this.options = $.extend(true, {},
- this.options,
- options,
- this._getCreateOptions()
- );
-
- var self = this;
- this.element.bind('remove.' + this.widgetName, function() {
- self.destroy();
- });
-
- this._create();
- this._trigger('create');
- this._init();
- },
- _getCreateOptions: function() {
- return (this.element.data('config') || {})[this.widgetName];
- },
- __trigger: $.Widget.prototype._trigger,
- _trigger: function(type, event, data) {
- var callback = this.options[type];
- if (typeof callback === 'string') {
- if (/^function\s*\(/i.test(callback)) {
- callback = (new Function('return ' + callback))();
- } else if (window.AX && $.isFunction(window.AX.App.Callbacks[this.widgetName][callback])) {
- callback = window.AX.App.Callbacks[this.widgetName][callback];
- }
- this.options[type] = callback;
- }
-
- return this.__trigger(type, event, data);
- }
- });
-
- // Extract to window
- // Deprecate
- $.extend(window, {
- asset: $.ax.asset,
- t: $.ax.t
- });
-
- // Ajax global settings.
- $.ajaxPrefilter(function(options, originalOptions, jqXhr) {
- var doneList = $.Callbacks('once memory stopOnFalse'),
- failList = $.Callbacks('once memory stopOnFalse'),
- $el, count;
-
- jqXhr.success = doneList.add;
- jqXhr.error = failList.add;
-
- jqXhr.done(function(data, status, jqXhr) {
- if (typeof data === 'object') {
- if (data) {
- if (data.info && data.info !== 'NULL') {
- $.log(data.info);
- }
-
- status = parseInt(data.status, 10);
- data = data.content;
- } else {
- $.error('Ajax returning data is empty');
- return false;
- }
- }
-
- try {
- doneList.fireWith(this, [data, status, jqXhr]);
- } catch (e) {
- $.error(e);
- }
-
- });
-
- jqXhr.fail(function() {
- try {
- failList.fireWith(this, arguments);
- } catch (e) {
- $.error(e);
- }
-
- });
-
- jqXhr.error(function(jqXhr, status, error) {
- if (status === 'parsererror') {
- $.ax.error('Response parse error.');
- $.error(error);
- return false;
- }
- });
-
- jqXhr.success(function(json, status, error) {
- if (status >= $.ax.CONST.ASC_ERROR) {
- $.ax.error(json);
- return false;
- }
- if (status === $.ax.CONST.ASC_SUCCESS) {
- $.ax.success(json);
- return !!originalOptions.pass;
- }
- });
-
- if (options.element) {
- $el = $(options.element);
- count = $el.data('JQ_AJAX_COUNT') || 0;
-
- if (count > 0) {
- jqXhr.abort();
- } else {
- $el.data('JQ_AJAX_COUNT', ++count);
- jqXhr.complete(function() {
- $el.data('JQ_AJAX_COUNT', --count);
- });
- }
- }
- });
-
- $.ajaxSetup({
- dataType: 'json',
- statusCode: {
- '401': function(jqXhr, status, error) {
- var id = 'JQ-LOGIN-FORM',
- json = $.parseJSON(jqXhr.responseText),
- $login = $('#' + id);
-
- if (!$login.length) {
- $login = $('<div id="' + id + '" class="dialog">' + (json.content || json) + '</div>');
- $login.css({'display': 'none'}).appendTo('body');
-
- $login.axDialog({
- modal: true,
- autoOpen: false,
- width: 480,
- height: 210,
- title: 'Please Login',
- buttons: [
- {
- text: 'Login',
- click: function(e) {
- $login.find('form').axAjaxSubmit({
- dataType: 'json',
- resetForm: true,
- success: function() {
- $login.axDialog('close');
- }
- });
- e.stopPropagation();
- e.preventDefault();
- }
- },
- 'cancel'
- ]
- });
- }
-
- $login.axDialog('open');
- }
- }
- });
-
- })(jQuery);