Commit 7960ccdf by Qiang Xue

Tab to spaces conversion.

parent 8cb144c5
(function() { (function () {
var ajax = function(url, settings) { var ajax = function (url, settings) {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
settings = settings || {}; settings = settings || {};
xhr.open(settings.method || 'GET', url, true); xhr.open(settings.method || 'GET', url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function(state) { xhr.onreadystatechange = function (state) {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
if (xhr.status == 200 && settings.success) { if (xhr.status == 200 && settings.success) {
settings.success(xhr); settings.success(xhr);
} else if (xhr.status != 200 && settings.error) { } else if (xhr.status != 200 && settings.error) {
settings.error(xhr); settings.error(xhr);
} }
} }
}; };
xhr.send(settings.data || ''); xhr.send(settings.data || '');
}; };
var e = document.getElementById('yii-debug-toolbar'); var e = document.getElementById('yii-debug-toolbar');
if (e) { if (e) {
e.style.display = 'block'; e.style.display = 'block';
var url = e.getAttribute('data-url'); var url = e.getAttribute('data-url');
ajax(url, { ajax(url, {
success: function(xhr) { success: function (xhr) {
var div = document.createElement('div'); var div = document.createElement('div');
div.innerHTML = xhr.responseText; div.innerHTML = xhr.responseText;
e.parentNode.replaceChild(div, e); e.parentNode.replaceChild(div, e);
if (window.localStorage) { if (window.localStorage) {
var pref = localStorage.getItem('yii-debug-toolbar'); var pref = localStorage.getItem('yii-debug-toolbar');
if (pref == 'minimized') { if (pref == 'minimized') {
document.getElementById('yii-debug-toolbar').style.display = 'none'; document.getElementById('yii-debug-toolbar').style.display = 'none';
document.getElementById('yii-debug-toolbar-min').style.display = 'block'; document.getElementById('yii-debug-toolbar-min').style.display = 'block';
} }
} }
}, },
error: function(xhr) { error: function (xhr) {
e.innerHTML = xhr.responseText; e.innerHTML = xhr.responseText;
} }
}); });
} }
})(); })();
yii.gii = (function ($) { yii.gii = (function ($) {
var isActive = $('.default-view').length > 0; var isActive = $('.default-view').length > 0;
var initHintBlocks = function () { var initHintBlocks = function () {
$('.hint-block').each(function () { $('.hint-block').each(function () {
var $hint = $(this); var $hint = $(this);
$hint.parent().find('label').addClass('help').popover({ $hint.parent().find('label').addClass('help').popover({
html: true, html: true,
trigger: 'hover', trigger: 'hover',
placement: 'right', placement: 'right',
content: $hint.html() content: $hint.html()
}); });
}); });
}; };
var initStickyInputs = function () { var initStickyInputs = function () {
$('.sticky:not(.error)').find('input[type="text"],select,textarea').each(function () { $('.sticky:not(.error)').find('input[type="text"],select,textarea').each(function () {
var value; var value;
if (this.tagName === 'SELECT') { if (this.tagName === 'SELECT') {
value = this.options[this.selectedIndex].text; value = this.options[this.selectedIndex].text;
} else if (this.tagName === 'TEXTAREA') { } else if (this.tagName === 'TEXTAREA') {
value = $(this).html(); value = $(this).html();
} else { } else {
value = $(this).val(); value = $(this).val();
} }
if (value === '') { if (value === '') {
value = '[empty]'; value = '[empty]';
} }
$(this).before('<div class="sticky-value">' + value + '</div>').hide(); $(this).before('<div class="sticky-value">' + value + '</div>').hide();
}); });
$('.sticky-value').on('click', function () { $('.sticky-value').on('click', function () {
$(this).hide(); $(this).hide();
$(this).next().show().get(0).focus(); $(this).next().show().get(0).focus();
}); });
}; };
var initPreviewDiffLinks = function () { var initPreviewDiffLinks = function () {
$('.preview-code, .diff-code, .modal-refresh, .modal-previous, .modal-next').on('click', function () { $('.preview-code, .diff-code, .modal-refresh, .modal-previous, .modal-next').on('click', function () {
var $modal = $('#preview-modal'); var $modal = $('#preview-modal');
var $link = $(this); var $link = $(this);
$modal.find('.modal-refresh').attr('href', $link.attr('href')); $modal.find('.modal-refresh').attr('href', $link.attr('href'));
if ($link.hasClass('preview-code') || $link.hasClass('diff-code')) { if ($link.hasClass('preview-code') || $link.hasClass('diff-code')) {
$modal.data('action', ($link.hasClass('preview-code') ? 'preview-code' : 'diff-code')) $modal.data('action', ($link.hasClass('preview-code') ? 'preview-code' : 'diff-code'))
} }
$modal.find('.modal-title').text($link.data('title')); $modal.find('.modal-title').text($link.data('title'));
$modal.find('.modal-body').html('Loading ...'); $modal.find('.modal-body').html('Loading ...');
$modal.modal('show'); $modal.modal('show');
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
cache: false, cache: false,
url: $link.prop('href'), url: $link.prop('href'),
data: $('.default-view form').serializeArray(), data: $('.default-view form').serializeArray(),
success: function (data) { success: function (data) {
if (!$link.hasClass('modal-refresh')) { if (!$link.hasClass('modal-refresh')) {
var filesSelector = 'a.' + $modal.data('action'); var filesSelector = 'a.' + $modal.data('action');
var $files = $(filesSelector); var $files = $(filesSelector);
var index = $files.filter('[href="' + $link.attr('href') + '"]').index(filesSelector); var index = $files.filter('[href="' + $link.attr('href') + '"]').index(filesSelector);
var $prev = $files.eq(index-1); var $prev = $files.eq(index - 1);
var $next = $files.eq((index+1 == $files.length ? 0 : index+1)); var $next = $files.eq((index + 1 == $files.length ? 0 : index + 1));
$modal.find('.modal-previous').attr('href', $prev.attr('href')).data('title', $prev.data('title')); $modal.find('.modal-previous').attr('href', $prev.attr('href')).data('title', $prev.data('title'));
$modal.find('.modal-next').attr('href', $next.attr('href')).data('title', $next.data('title')); $modal.find('.modal-next').attr('href', $next.attr('href')).data('title', $next.data('title'));
} }
$modal.find('.modal-body').html(data); $modal.find('.modal-body').html(data);
$modal.find('.content').css('max-height', ($(window).height() - 200) + 'px'); $modal.find('.content').css('max-height', ($(window).height() - 200) + 'px');
}, },
error: function (XMLHttpRequest, textStatus, errorThrown) { error: function (XMLHttpRequest, textStatus, errorThrown) {
$modal.find('.modal-body').html('<div class="error">' + XMLHttpRequest.responseText + '</div>'); $modal.find('.modal-body').html('<div class="error">' + XMLHttpRequest.responseText + '</div>');
} }
}); });
return false; return false;
}); });
$('#preview-modal').on('keydown', function(e) { $('#preview-modal').on('keydown', function (e) {
if (e.keyCode === 37) { if (e.keyCode === 37) {
$('.modal-previous').trigger('click'); $('.modal-previous').trigger('click');
} else if(e.keyCode === 39) { } else if (e.keyCode === 39) {
$('.modal-next').trigger('click'); $('.modal-next').trigger('click');
} else if(e.keyCode === 82) { } else if (e.keyCode === 82) {
$('.modal-refresh').trigger('click'); $('.modal-refresh').trigger('click');
} }
}); });
}; };
var initConfirmationCheckboxes = function () { var initConfirmationCheckboxes = function () {
var $checkAll = $('#check-all'); var $checkAll = $('#check-all');
$checkAll.click(function () { $checkAll.click(function () {
$('.default-view-files table .check input').prop('checked', this.checked); $('.default-view-files table .check input').prop('checked', this.checked);
}); });
$('.default-view-files table .check input').click(function () { $('.default-view-files table .check input').click(function () {
$checkAll.prop('checked', !$('.default-view-files table .check input:not(:checked)').length); $checkAll.prop('checked', !$('.default-view-files table .check input:not(:checked)').length);
}); });
$checkAll.prop('checked', !$('.default-view-files table .check input:not(:checked)').length); $checkAll.prop('checked', !$('.default-view-files table .check input:not(:checked)').length);
}; };
return { return {
autocomplete: function (counter, data) { autocomplete: function (counter, data) {
var datum = new Bloodhound({ var datum = new Bloodhound({
datumTokenizer: function(d){return Bloodhound.tokenizers.whitespace(d.word);}, datumTokenizer: function (d) {
queryTokenizer: Bloodhound.tokenizers.whitespace, return Bloodhound.tokenizers.whitespace(d.word);
local: data },
}); queryTokenizer: Bloodhound.tokenizers.whitespace,
datum.initialize(); local: data
jQuery('.typeahead-'+counter).typeahead(null,{displayKey: 'word', source: datum.ttAdapter()}); });
}, datum.initialize();
init: function () { jQuery('.typeahead-' + counter).typeahead(null, {displayKey: 'word', source: datum.ttAdapter()});
initHintBlocks(); },
initStickyInputs(); init: function () {
initPreviewDiffLinks(); initHintBlocks();
initConfirmationCheckboxes(); initStickyInputs();
initPreviewDiffLinks();
initConfirmationCheckboxes();
// model generator: hide class name input when table name input contains * // model generator: hide class name input when table name input contains *
$('#model-generator #generator-tablename').on('change', function () { $('#model-generator #generator-tablename').on('change',function () {
$('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1); $('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1);
}).change(); }).change();
// hide Generate button if any input is changed // hide Generate button if any input is changed
$('.default-view .form-group input,select,textarea').change(function () { $('.default-view .form-group input,select,textarea').change(function () {
$('.default-view-results,.default-view-files').hide(); $('.default-view-results,.default-view-files').hide();
$('.default-view button[name="generate"]').hide(); $('.default-view button[name="generate"]').hide();
}); });
$('.module-form #generator-moduleclass').change(function () { $('.module-form #generator-moduleclass').change(function () {
var value = $(this).val().match(/(\w+)\\\w+$/); var value = $(this).val().match(/(\w+)\\\w+$/);
var $idInput = $('#generator-moduleid'); var $idInput = $('#generator-moduleid');
if (value && value[1] && $idInput.val() == '') { if (value && value[1] && $idInput.val() == '') {
$idInput.val(value[1]); $idInput.val(value[1]);
} }
}); });
} }
}; };
})(jQuery); })(jQuery);
...@@ -10,400 +10,400 @@ ...@@ -10,400 +10,400 @@
* @since 2.0 * @since 2.0
*/ */
(function ($) { (function ($) {
$.fn.yiiActiveForm = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.yiiActiveForm');
return false;
}
};
var defaults = { $.fn.yiiActiveForm = function (method) {
// the jQuery selector for the error summary if (methods[method]) {
errorSummary: undefined, return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
// whether to perform validation before submitting the form. } else if (typeof method === 'object' || !method) {
validateOnSubmit: true, return methods.init.apply(this, arguments);
// the container CSS class representing the corresponding attribute has validation error } else {
errorCssClass: 'error', $.error('Method ' + method + ' does not exist on jQuery.yiiActiveForm');
// the container CSS class representing the corresponding attribute passes validation return false;
successCssClass: 'success', }
// the container CSS class representing the corresponding attribute is being validated };
validatingCssClass: 'validating',
// the URL for performing AJAX-based validation. If not set, it will use the the form's action
validationUrl: undefined,
// a callback that is called before submitting the form. The signature of the callback should be:
// function ($form) { ...return false to cancel submission...}
beforeSubmit: undefined,
// a callback that is called before validating each attribute. The signature of the callback should be:
// function ($form, attribute, messages) { ...return false to cancel the validation...}
beforeValidate: undefined,
// a callback that is called after an attribute is validated. The signature of the callback should be:
// function ($form, attribute, messages)
afterValidate: undefined,
// the GET parameter name indicating an AJAX-based validation
ajaxParam: 'ajax',
// the type of data that you're expecting back from the server
ajaxDataType: 'json'
};
var attributeDefaults = { var defaults = {
// attribute name or expression (e.g. "[0]content" for tabular input) // the jQuery selector for the error summary
name: undefined, errorSummary: undefined,
// the jQuery selector of the container of the input field // whether to perform validation before submitting the form.
container: undefined, validateOnSubmit: true,
// the jQuery selector of the input field // the container CSS class representing the corresponding attribute has validation error
input: undefined, errorCssClass: 'error',
// the jQuery selector of the error tag // the container CSS class representing the corresponding attribute passes validation
error: undefined, successCssClass: 'success',
// whether to perform validation when a change is detected on the input // the container CSS class representing the corresponding attribute is being validated
validateOnChange: false, validatingCssClass: 'validating',
// whether to perform validation when the user is typing. // the URL for performing AJAX-based validation. If not set, it will use the the form's action
validateOnType: false, validationUrl: undefined,
// number of milliseconds that the validation should be delayed when a user is typing in the input field. // a callback that is called before submitting the form. The signature of the callback should be:
validationDelay: 200, // function ($form) { ...return false to cancel submission...}
// whether to enable AJAX-based validation. beforeSubmit: undefined,
enableAjaxValidation: false, // a callback that is called before validating each attribute. The signature of the callback should be:
// function (attribute, value, messages), the client-side validation function. // function ($form, attribute, messages) { ...return false to cancel the validation...}
validate: undefined, beforeValidate: undefined,
// status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating // a callback that is called after an attribute is validated. The signature of the callback should be:
status: 0, // function ($form, attribute, messages)
// the value of the input afterValidate: undefined,
value: undefined // the GET parameter name indicating an AJAX-based validation
}; ajaxParam: 'ajax',
// the type of data that you're expecting back from the server
ajaxDataType: 'json'
};
var methods = { var attributeDefaults = {
init: function (attributes, options) { // attribute name or expression (e.g. "[0]content" for tabular input)
return this.each(function () { name: undefined,
var $form = $(this); // the jQuery selector of the container of the input field
if ($form.data('yiiActiveForm')) { container: undefined,
return; // the jQuery selector of the input field
} input: undefined,
// the jQuery selector of the error tag
error: undefined,
// whether to perform validation when a change is detected on the input
validateOnChange: false,
// whether to perform validation when the user is typing.
validateOnType: false,
// number of milliseconds that the validation should be delayed when a user is typing in the input field.
validationDelay: 200,
// whether to enable AJAX-based validation.
enableAjaxValidation: false,
// function (attribute, value, messages), the client-side validation function.
validate: undefined,
// status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating
status: 0,
// the value of the input
value: undefined
};
var settings = $.extend({}, defaults, options || {}); var methods = {
if (settings.validationUrl === undefined) { init: function (attributes, options) {
settings.validationUrl = $form.prop('action'); return this.each(function () {
} var $form = $(this);
$.each(attributes, function (i) { if ($form.data('yiiActiveForm')) {
attributes[i] = $.extend({value: getValue($form, this)}, attributeDefaults, this); return;
}); }
$form.data('yiiActiveForm', {
settings: settings,
attributes: attributes,
submitting: false,
validated: false
});
watchAttributes($form, attributes); var settings = $.extend({}, defaults, options || {});
if (settings.validationUrl === undefined) {
settings.validationUrl = $form.prop('action');
}
$.each(attributes, function (i) {
attributes[i] = $.extend({value: getValue($form, this)}, attributeDefaults, this);
});
$form.data('yiiActiveForm', {
settings: settings,
attributes: attributes,
submitting: false,
validated: false
});
/** watchAttributes($form, attributes);
* Clean up error status when the form is reset.
* Note that $form.on('reset', ...) does work because the "reset" event does not bubble on IE.
*/
$form.bind('reset.yiiActiveForm', methods.resetForm);
if (settings.validateOnSubmit) { /**
$form.on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () { * Clean up error status when the form is reset.
$form.data('yiiActiveForm').submitObject = $(this); * Note that $form.on('reset', ...) does work because the "reset" event does not bubble on IE.
}); */
$form.on('submit', methods.submitForm); $form.bind('reset.yiiActiveForm', methods.resetForm);
}
});
},
destroy: function () { if (settings.validateOnSubmit) {
return this.each(function () { $form.on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () {
$(window).unbind('.yiiActiveForm'); $form.data('yiiActiveForm').submitObject = $(this);
$(this).removeData('yiiActiveForm'); });
}); $form.on('submit', methods.submitForm);
}, }
});
},
data: function() { destroy: function () {
return this.data('yiiActiveForm'); return this.each(function () {
}, $(window).unbind('.yiiActiveForm');
$(this).removeData('yiiActiveForm');
});
},
submitForm: function () { data: function () {
var $form = $(this), return this.data('yiiActiveForm');
data = $form.data('yiiActiveForm'); },
if (data.validated) {
if (data.settings.beforeSubmit !== undefined) {
if (data.settings.beforeSubmit($form) == false) {
data.validated = false;
data.submitting = false;
return false;
}
}
// continue submitting the form since validation passes
return true;
}
if (data.settings.timer !== undefined) { submitForm: function () {
clearTimeout(data.settings.timer); var $form = $(this),
} data = $form.data('yiiActiveForm');
data.submitting = true; if (data.validated) {
validate($form, function (messages) { if (data.settings.beforeSubmit !== undefined) {
var errors = []; if (data.settings.beforeSubmit($form) == false) {
$.each(data.attributes, function () { data.validated = false;
if (updateInput($form, this, messages)) { data.submitting = false;
errors.push(this.input); return false;
} }
}); }
updateSummary($form, messages); // continue submitting the form since validation passes
if (errors.length) { return true;
var top = $form.find(errors.join(',')).first().offset().top; }
var wtop = $(window).scrollTop();
if (top < wtop || top > wtop + $(window).height) {
$(window).scrollTop(top);
}
} else {
data.validated = true;
var $button = data.submitObject || $form.find(':submit:first');
// TODO: if the submission is caused by "change" event, it will not work
if ($button.length) {
$button.click();
} else {
// no submit button in the form
$form.submit();
}
return;
}
data.submitting = false;
}, function () {
data.submitting = false;
});
return false;
},
resetForm: function () { if (data.settings.timer !== undefined) {
var $form = $(this); clearTimeout(data.settings.timer);
var data = $form.data('yiiActiveForm'); }
// Because we bind directly to a form reset event instead of a reset button (that may not exist), data.submitting = true;
// when this function is executed form input values have not been reset yet. validate($form, function (messages) {
// Therefore we do the actual reset work through setTimeout. var errors = [];
setTimeout(function () { $.each(data.attributes, function () {
$.each(data.attributes, function () { if (updateInput($form, this, messages)) {
// Without setTimeout() we would get the input values that are not reset yet. errors.push(this.input);
this.value = getValue($form, this); }
this.status = 0; });
var $container = $form.find(this.container); updateSummary($form, messages);
$container.removeClass( if (errors.length) {
data.settings.validatingCssClass + ' ' + var top = $form.find(errors.join(',')).first().offset().top;
data.settings.errorCssClass + ' ' + var wtop = $(window).scrollTop();
data.settings.successCssClass if (top < wtop || top > wtop + $(window).height) {
); $(window).scrollTop(top);
$container.find(this.error).html(''); }
}); } else {
$form.find(data.settings.summary).hide().find('ul').html(''); data.validated = true;
}, 1); var $button = data.submitObject || $form.find(':submit:first');
} // TODO: if the submission is caused by "change" event, it will not work
}; if ($button.length) {
$button.click();
} else {
// no submit button in the form
$form.submit();
}
return;
}
data.submitting = false;
}, function () {
data.submitting = false;
});
return false;
},
var watchAttributes = function ($form, attributes) { resetForm: function () {
$.each(attributes, function (i, attribute) { var $form = $(this);
var $input = findInput($form, attribute); var data = $form.data('yiiActiveForm');
if (attribute.validateOnChange) { // Because we bind directly to a form reset event instead of a reset button (that may not exist),
$input.on('change.yiiActiveForm', function () { // when this function is executed form input values have not been reset yet.
validateAttribute($form, attribute, false); // Therefore we do the actual reset work through setTimeout.
}).on('blur.yiiActiveForm', function () { setTimeout(function () {
if (attribute.status == 0 || attribute.status == 1) { $.each(data.attributes, function () {
validateAttribute($form, attribute, !attribute.status); // Without setTimeout() we would get the input values that are not reset yet.
} this.value = getValue($form, this);
}); this.status = 0;
} var $container = $form.find(this.container);
if (attribute.validateOnType) { $container.removeClass(
$input.on('keyup.yiiActiveForm', function () { data.settings.validatingCssClass + ' ' +
if (attribute.value !== getValue($form, attribute)) { data.settings.errorCssClass + ' ' +
validateAttribute($form, attribute, false); data.settings.successCssClass
} );
}); $container.find(this.error).html('');
} });
}); $form.find(data.settings.summary).hide().find('ul').html('');
}; }, 1);
}
};
var validateAttribute = function ($form, attribute, forceValidate) { var watchAttributes = function ($form, attributes) {
var data = $form.data('yiiActiveForm'); $.each(attributes, function (i, attribute) {
var $input = findInput($form, attribute);
if (attribute.validateOnChange) {
$input.on('change.yiiActiveForm',function () {
validateAttribute($form, attribute, false);
}).on('blur.yiiActiveForm', function () {
if (attribute.status == 0 || attribute.status == 1) {
validateAttribute($form, attribute, !attribute.status);
}
});
}
if (attribute.validateOnType) {
$input.on('keyup.yiiActiveForm', function () {
if (attribute.value !== getValue($form, attribute)) {
validateAttribute($form, attribute, false);
}
});
}
});
};
if (forceValidate) { var validateAttribute = function ($form, attribute, forceValidate) {
attribute.status = 2; var data = $form.data('yiiActiveForm');
}
$.each(data.attributes, function () {
if (this.value !== getValue($form, this)) {
this.status = 2;
forceValidate = true;
}
});
if (!forceValidate) {
return;
}
if (data.settings.timer !== undefined) { if (forceValidate) {
clearTimeout(data.settings.timer); attribute.status = 2;
} }
data.settings.timer = setTimeout(function () { $.each(data.attributes, function () {
if (data.submitting || $form.is(':hidden')) { if (this.value !== getValue($form, this)) {
return; this.status = 2;
} forceValidate = true;
$.each(data.attributes, function () { }
if (this.status === 2) { });
this.status = 3; if (!forceValidate) {
$form.find(this.container).addClass(data.settings.validatingCssClass); return;
} }
});
validate($form, function (messages) {
var hasError = false;
$.each(data.attributes, function () {
if (this.status === 2 || this.status === 3) {
hasError = updateInput($form, this, messages) || hasError;
}
});
});
}, data.settings.validationDelay);
};
/**
* Performs validation.
* @param $form jQuery the jquery representation of the form
* @param successCallback function the function to be invoked if the validation completes
* @param errorCallback function the function to be invoked if the ajax validation request fails
*/
var validate = function ($form, successCallback, errorCallback) {
var data = $form.data('yiiActiveForm'),
needAjaxValidation = false,
messages = {};
$.each(data.attributes, function () { if (data.settings.timer !== undefined) {
if (data.submitting || this.status === 2 || this.status === 3) { clearTimeout(data.settings.timer);
var msg = []; }
if (!data.settings.beforeValidate || data.settings.beforeValidate($form, this, msg)) { data.settings.timer = setTimeout(function () {
if (this.validate) { if (data.submitting || $form.is(':hidden')) {
this.validate(this, getValue($form, this), msg); return;
} }
if (msg.length) { $.each(data.attributes, function () {
messages[this.name] = msg; if (this.status === 2) {
} else if (this.enableAjaxValidation) { this.status = 3;
needAjaxValidation = true; $form.find(this.container).addClass(data.settings.validatingCssClass);
} }
} });
} validate($form, function (messages) {
}); var hasError = false;
$.each(data.attributes, function () {
if (this.status === 2 || this.status === 3) {
hasError = updateInput($form, this, messages) || hasError;
}
});
});
}, data.settings.validationDelay);
};
if (needAjaxValidation && (!data.submitting || $.isEmptyObject(messages))) { /**
// Perform ajax validation when at least one input needs it. * Performs validation.
// If the validation is triggered by form submission, ajax validation * @param $form jQuery the jquery representation of the form
// should be done only when all inputs pass client validation * @param successCallback function the function to be invoked if the validation completes
var $button = data.submitObject, * @param errorCallback function the function to be invoked if the ajax validation request fails
extData = '&' + data.settings.ajaxParam + '=' + $form.prop('id'); */
if ($button && $button.length && $button.prop('name')) { var validate = function ($form, successCallback, errorCallback) {
extData += '&' + $button.prop('name') + '=' + $button.prop('value'); var data = $form.data('yiiActiveForm'),
} needAjaxValidation = false,
$.ajax({ messages = {};
url: data.settings.validationUrl,
type: $form.prop('method'),
data: $form.serialize() + extData,
dataType: data.settings.ajaxDataType,
success: function (msgs) {
if (msgs !== null && typeof msgs === 'object') {
$.each(data.attributes, function () {
if (!this.enableAjaxValidation) {
delete msgs[this.name];
}
});
successCallback($.extend({}, messages, msgs));
} else {
successCallback(messages);
}
},
error: errorCallback
});
} else if (data.submitting) {
// delay callback so that the form can be submitted without problem
setTimeout(function () {
successCallback(messages);
}, 200);
} else {
successCallback(messages);
}
};
/** $.each(data.attributes, function () {
* Updates the error message and the input container for a particular attribute. if (data.submitting || this.status === 2 || this.status === 3) {
* @param $form the form jQuery object var msg = [];
* @param attribute object the configuration for a particular attribute. if (!data.settings.beforeValidate || data.settings.beforeValidate($form, this, msg)) {
* @param messages array the validation error messages if (this.validate) {
* @return boolean whether there is a validation error for the specified attribute this.validate(this, getValue($form, this), msg);
*/ }
var updateInput = function ($form, attribute, messages) { if (msg.length) {
var data = $form.data('yiiActiveForm'), messages[this.name] = msg;
$input = findInput($form, attribute), } else if (this.enableAjaxValidation) {
hasError = false; needAjaxValidation = true;
}
}
}
});
if (data.settings.afterValidate) { if (needAjaxValidation && (!data.submitting || $.isEmptyObject(messages))) {
data.settings.afterValidate($form, attribute, messages); // Perform ajax validation when at least one input needs it.
} // If the validation is triggered by form submission, ajax validation
attribute.status = 1; // should be done only when all inputs pass client validation
if ($input.length) { var $button = data.submitObject,
hasError = messages && $.isArray(messages[attribute.name]) && messages[attribute.name].length; extData = '&' + data.settings.ajaxParam + '=' + $form.prop('id');
var $container = $form.find(attribute.container); if ($button && $button.length && $button.prop('name')) {
var $error = $container.find(attribute.error); extData += '&' + $button.prop('name') + '=' + $button.prop('value');
if (hasError) { }
$error.text(messages[attribute.name][0]); $.ajax({
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass) url: data.settings.validationUrl,
.addClass(data.settings.errorCssClass); type: $form.prop('method'),
} else { data: $form.serialize() + extData,
$error.text(''); dataType: data.settings.ajaxDataType,
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ') success: function (msgs) {
.addClass(data.settings.successCssClass); if (msgs !== null && typeof msgs === 'object') {
} $.each(data.attributes, function () {
attribute.value = getValue($form, attribute); if (!this.enableAjaxValidation) {
} delete msgs[this.name];
return hasError; }
}; });
successCallback($.extend({}, messages, msgs));
} else {
successCallback(messages);
}
},
error: errorCallback
});
} else if (data.submitting) {
// delay callback so that the form can be submitted without problem
setTimeout(function () {
successCallback(messages);
}, 200);
} else {
successCallback(messages);
}
};
/** /**
* Updates the error summary. * Updates the error message and the input container for a particular attribute.
* @param $form the form jQuery object * @param $form the form jQuery object
* @param messages array the validation error messages * @param attribute object the configuration for a particular attribute.
*/ * @param messages array the validation error messages
var updateSummary = function ($form, messages) { * @return boolean whether there is a validation error for the specified attribute
var data = $form.data('yiiActiveForm'), */
$summary = $form.find(data.settings.errorSummary), var updateInput = function ($form, attribute, messages) {
$ul = $summary.find('ul').html(''); var data = $form.data('yiiActiveForm'),
$input = findInput($form, attribute),
hasError = false;
if ($summary.length && messages) { if (data.settings.afterValidate) {
$.each(data.attributes, function () { data.settings.afterValidate($form, attribute, messages);
if ($.isArray(messages[this.name]) && messages[this.name].length) { }
$ul.append($('<li/>').text(messages[this.name][0])); attribute.status = 1;
} if ($input.length) {
}); hasError = messages && $.isArray(messages[attribute.name]) && messages[attribute.name].length;
$summary.toggle($ul.find('li').length > 0); var $container = $form.find(attribute.container);
} var $error = $container.find(attribute.error);
}; if (hasError) {
$error.text(messages[attribute.name][0]);
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass)
.addClass(data.settings.errorCssClass);
} else {
$error.text('');
$container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ')
.addClass(data.settings.successCssClass);
}
attribute.value = getValue($form, attribute);
}
return hasError;
};
var getValue = function ($form, attribute) { /**
var $input = findInput($form, attribute); * Updates the error summary.
var type = $input.prop('type'); * @param $form the form jQuery object
if (type === 'checkbox' || type === 'radio') { * @param messages array the validation error messages
var $realInput = $input.filter(':checked'); */
if (!$realInput.length) { var updateSummary = function ($form, messages) {
$realInput = $form.find('input[type=hidden][name="'+$input.prop('name')+'"]'); var data = $form.data('yiiActiveForm'),
} $summary = $form.find(data.settings.errorSummary),
return $realInput.val(); $ul = $summary.find('ul').html('');
} else {
return $input.val();
}
};
var findInput = function ($form, attribute) { if ($summary.length && messages) {
var $input = $form.find(attribute.input); $.each(data.attributes, function () {
if ($input.length && $input[0].tagName.toLowerCase() === 'div') { if ($.isArray(messages[this.name]) && messages[this.name].length) {
// checkbox list or radio list $ul.append($('<li/>').text(messages[this.name][0]));
return $input.find('input'); }
} else { });
return $input; $summary.toggle($ul.find('li').length > 0);
} }
}; };
var getValue = function ($form, attribute) {
var $input = findInput($form, attribute);
var type = $input.prop('type');
if (type === 'checkbox' || type === 'radio') {
var $realInput = $input.filter(':checked');
if (!$realInput.length) {
$realInput = $form.find('input[type=hidden][name="' + $input.prop('name') + '"]');
}
return $realInput.val();
} else {
return $input.val();
}
};
var findInput = function ($form, attribute) {
var $input = $form.find(attribute.input);
if ($input.length && $input[0].tagName.toLowerCase() === 'div') {
// checkbox list or radio list
return $input.find('input');
} else {
return $input;
}
};
})(window.jQuery); })(window.jQuery);
...@@ -10,63 +10,63 @@ ...@@ -10,63 +10,63 @@
* @since 2.0 * @since 2.0
*/ */
(function ($) { (function ($) {
$.fn.yiiCaptcha = function (method) { $.fn.yiiCaptcha = function (method) {
if (methods[method]) { if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) { } else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments); return methods.init.apply(this, arguments);
} else { } else {
$.error('Method ' + method + ' does not exist on jQuery.yiiCaptcha'); $.error('Method ' + method + ' does not exist on jQuery.yiiCaptcha');
return false; return false;
} }
}; };
var defaults = { var defaults = {
refreshUrl: undefined, refreshUrl: undefined,
hashKey: undefined hashKey: undefined
}; };
var methods = { var methods = {
init: function (options) { init: function (options) {
return this.each(function () { return this.each(function () {
var $e = $(this); var $e = $(this);
var settings = $.extend({}, defaults, options || {}); var settings = $.extend({}, defaults, options || {});
$e.data('yiiCaptcha', { $e.data('yiiCaptcha', {
settings: settings settings: settings
}); });
$e.on('click.yiiCaptcha', function() { $e.on('click.yiiCaptcha', function () {
methods.refresh.apply($e); methods.refresh.apply($e);
return false; return false;
}); });
}); });
}, },
refresh: function () { refresh: function () {
var $e = this, var $e = this,
settings = this.data('yiiCaptcha').settings; settings = this.data('yiiCaptcha').settings;
$.ajax({ $.ajax({
url: $e.data('yiiCaptcha').settings.refreshUrl, url: $e.data('yiiCaptcha').settings.refreshUrl,
dataType: 'json', dataType: 'json',
cache: false, cache: false,
success: function(data) { success: function (data) {
$e.attr('src', data.url); $e.attr('src', data.url);
$('body').data(settings.hashKey, [data.hash1, data.hash2]); $('body').data(settings.hashKey, [data.hash1, data.hash2]);
} }
}); });
}, },
destroy: function () { destroy: function () {
return this.each(function () { return this.each(function () {
$(window).unbind('.yiiCaptcha'); $(window).unbind('.yiiCaptcha');
$(this).removeData('yiiCaptcha'); $(this).removeData('yiiCaptcha');
}); });
}, },
data: function() { data: function () {
return this.data('yiiCaptcha'); return this.data('yiiCaptcha');
} }
}; };
})(window.jQuery); })(window.jQuery);
...@@ -10,120 +10,120 @@ ...@@ -10,120 +10,120 @@
* @since 2.0 * @since 2.0
*/ */
(function ($) { (function ($) {
$.fn.yiiGridView = function (method) { $.fn.yiiGridView = function (method) {
if (methods[method]) { if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) { } else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments); return methods.init.apply(this, arguments);
} else { } else {
$.error('Method ' + method + ' does not exist on jQuery.yiiGridView'); $.error('Method ' + method + ' does not exist on jQuery.yiiGridView');
return false; return false;
} }
}; };
var defaults = { var defaults = {
filterUrl: undefined, filterUrl: undefined,
filterSelector: undefined filterSelector: undefined
}; };
var gridData = {}; var gridData = {};
var methods = { var methods = {
init: function (options) { init: function (options) {
return this.each(function () { return this.each(function () {
var $e = $(this); var $e = $(this);
var settings = $.extend({}, defaults, options || {}); var settings = $.extend({}, defaults, options || {});
gridData[$e.prop('id')] = {settings: settings}; gridData[$e.prop('id')] = {settings: settings};
var enterPressed = false; var enterPressed = false;
$(document).off('change.yiiGridView keydown.yiiGridView', settings.filterSelector) $(document).off('change.yiiGridView keydown.yiiGridView', settings.filterSelector)
.on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) { .on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) {
if (event.type === 'keydown') { if (event.type === 'keydown') {
if (event.keyCode !== 13) { if (event.keyCode !== 13) {
return; // only react to enter key return; // only react to enter key
} else { } else {
enterPressed = true; enterPressed = true;
} }
} else { } else {
// prevent processing for both keydown and change events // prevent processing for both keydown and change events
if (enterPressed) { if (enterPressed) {
enterPressed = false; enterPressed = false;
return; return;
} }
} }
methods.applyFilter.apply($e); methods.applyFilter.apply($e);
return false; return false;
}); });
}); });
}, },
applyFilter: function () { applyFilter: function () {
var $grid = $(this); var $grid = $(this);
var settings = gridData[$grid.prop('id')].settings; var settings = gridData[$grid.prop('id')].settings;
var data = {}; var data = {};
$.each($(settings.filterSelector).serializeArray(), function () { $.each($(settings.filterSelector).serializeArray(), function () {
data[this.name] = this.value; data[this.name] = this.value;
}); });
$.each(yii.getQueryParams(settings.filterUrl), function (name, value) { $.each(yii.getQueryParams(settings.filterUrl), function (name, value) {
if (data[name] === undefined) { if (data[name] === undefined) {
data[name] = value; data[name] = value;
} }
}); });
var pos = settings.filterUrl.indexOf('?'); var pos = settings.filterUrl.indexOf('?');
var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos); var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos);
$grid.find('form.gridview-filter-form').remove(); $grid.find('form.gridview-filter-form').remove();
var $form = $('<form action="' + url + '" method="get" class="gridview-filter-form" style="display:none" data-pjax></form>').appendTo($grid); var $form = $('<form action="' + url + '" method="get" class="gridview-filter-form" style="display:none" data-pjax></form>').appendTo($grid);
$.each(data, function (name, value) { $.each(data, function (name, value) {
$form.append($('<input type="hidden" name="t" value="" />').attr('name', name).val(value)); $form.append($('<input type="hidden" name="t" value="" />').attr('name', name).val(value));
}); });
$form.submit(); $form.submit();
}, },
setSelectionColumn: function (options) { setSelectionColumn: function (options) {
var $grid = $(this); var $grid = $(this);
var id = $(this).prop('id'); var id = $(this).prop('id');
gridData[id].selectionColumn = options.name; gridData[id].selectionColumn = options.name;
if (!options.multiple) { if (!options.multiple) {
return; return;
} }
var inputs = "#" + id + " input[name='" + options.checkAll + "']"; var inputs = "#" + id + " input[name='" + options.checkAll + "']";
$(document).off('click.yiiGridView', inputs).on('click.yiiGridView', inputs, function () { $(document).off('click.yiiGridView', inputs).on('click.yiiGridView', inputs, function () {
$grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked); $grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked);
}); });
$(document).off('click.yiiGridView', inputs + ":enabled").on('click.yiiGridView', inputs + ":enabled", function () { $(document).off('click.yiiGridView', inputs + ":enabled").on('click.yiiGridView', inputs + ":enabled", function () {
var all = $grid.find("input[name='" + options.name + "']").length == $grid.find("input[name='" + options.name + "']:checked").length; var all = $grid.find("input[name='" + options.name + "']").length == $grid.find("input[name='" + options.name + "']:checked").length;
$grid.find("input[name='" + options.checkAll + "']").prop('checked', all); $grid.find("input[name='" + options.checkAll + "']").prop('checked', all);
}); });
}, },
getSelectedRows: function () { getSelectedRows: function () {
var $grid = $(this); var $grid = $(this);
var data = gridData[$grid.prop('id')]; var data = gridData[$grid.prop('id')];
var keys = []; var keys = [];
if (data.selectionColumn) { if (data.selectionColumn) {
$grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () { $grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () {
keys.push($(this).parent().closest('tr').data('key')); keys.push($(this).parent().closest('tr').data('key'));
}); });
} }
return keys; return keys;
}, },
destroy: function () { destroy: function () {
return this.each(function () { return this.each(function () {
$(window).unbind('.yiiGridView'); $(window).unbind('.yiiGridView');
$(this).removeData('yiiGridView'); $(this).removeData('yiiGridView');
}); });
}, },
data: function () { data: function () {
var id = $(this).prop('id'); var id = $(this).prop('id');
return gridData[id]; return gridData[id];
} }
}; };
})(window.jQuery); })(window.jQuery);
...@@ -42,220 +42,220 @@ ...@@ -42,220 +42,220 @@
* You must call "yii.initModule()" once for the root module of all your modules. * You must call "yii.initModule()" once for the root module of all your modules.
*/ */
yii = (function ($) { yii = (function ($) {
var pub = { var pub = {
/** /**
* List of scripts that can be loaded multiple times via AJAX requests. Each script can be represented * List of scripts that can be loaded multiple times via AJAX requests. Each script can be represented
* as either an absolute URL or a relative one. * as either an absolute URL or a relative one.
*/ */
reloadableScripts: [], reloadableScripts: [],
/** /**
* The selector for clickable elements that need to support confirmation and form submission. * The selector for clickable elements that need to support confirmation and form submission.
*/ */
clickableSelector: 'a, button, input[type="submit"], input[type="button"], input[type="reset"], input[type="image"]', clickableSelector: 'a, button, input[type="submit"], input[type="button"], input[type="reset"], input[type="image"]',
/** /**
* The selector for changeable elements that need to support confirmation and form submission. * The selector for changeable elements that need to support confirmation and form submission.
*/ */
changeableSelector: 'select, input, textarea', changeableSelector: 'select, input, textarea',
/** /**
* @return string|undefined the CSRF parameter name. Undefined is returned if CSRF validation is not enabled. * @return string|undefined the CSRF parameter name. Undefined is returned if CSRF validation is not enabled.
*/ */
getCsrfParam: function () { getCsrfParam: function () {
return $('meta[name=csrf-param]').prop('content'); return $('meta[name=csrf-param]').prop('content');
}, },
/** /**
* @return string|undefined the CSRF token. Undefined is returned if CSRF validation is not enabled. * @return string|undefined the CSRF token. Undefined is returned if CSRF validation is not enabled.
*/ */
getCsrfToken: function () { getCsrfToken: function () {
return $('meta[name=csrf-token]').prop('content'); return $('meta[name=csrf-token]').prop('content');
}, },
/** /**
* Displays a confirmation dialog. * Displays a confirmation dialog.
* The default implementation simply displays a js confirmation dialog. * The default implementation simply displays a js confirmation dialog.
* You may override this by setting `yii.confirm`. * You may override this by setting `yii.confirm`.
* @param message the confirmation message. * @param message the confirmation message.
* @return boolean whether the user confirms with the message in the dialog * @return boolean whether the user confirms with the message in the dialog
*/ */
confirm: function (message) { confirm: function (message) {
return confirm(message); return confirm(message);
}, },
/** /**
* Returns a value indicating whether to allow executing the action defined for the specified element. * Returns a value indicating whether to allow executing the action defined for the specified element.
* This method recognizes the `data-confirm` attribute of the element and uses it * This method recognizes the `data-confirm` attribute of the element and uses it
* as the message in a confirmation dialog. The method will return true if this special attribute * as the message in a confirmation dialog. The method will return true if this special attribute
* is not defined or if the user confirms the message. * is not defined or if the user confirms the message.
* @param $e the jQuery representation of the element * @param $e the jQuery representation of the element
* @return boolean whether to allow executing the action defined for the specified element. * @return boolean whether to allow executing the action defined for the specified element.
*/ */
allowAction: function ($e) { allowAction: function ($e) {
var message = $e.data('confirm'); var message = $e.data('confirm');
return message === undefined || pub.confirm(message); return message === undefined || pub.confirm(message);
}, },
/** /**
* Handles the action triggered by user. * Handles the action triggered by user.
* This method recognizes the `data-method` attribute of the element. If the attribute exists, * This method recognizes the `data-method` attribute of the element. If the attribute exists,
* the method will submit the form containing this element. If there is no containing form, a form * the method will submit the form containing this element. If there is no containing form, a form
* will be created and submitted using the method given by this attribute value (e.g. "post", "put"). * will be created and submitted using the method given by this attribute value (e.g. "post", "put").
* For hyperlinks, the form action will take the value of the "href" attribute of the link. * For hyperlinks, the form action will take the value of the "href" attribute of the link.
* For other elements, either the containing form action or the current page URL will be used * For other elements, either the containing form action or the current page URL will be used
* as the form action URL. * as the form action URL.
* *
* If the `data-method` attribute is not defined, the default element action will be performed. * If the `data-method` attribute is not defined, the default element action will be performed.
* *
* @param $e the jQuery representation of the element * @param $e the jQuery representation of the element
* @return boolean whether to execute the default action for the element. * @return boolean whether to execute the default action for the element.
*/ */
handleAction: function ($e) { handleAction: function ($e) {
var method = $e.data('method'); var method = $e.data('method');
if (method === undefined) { if (method === undefined) {
return true; return true;
} }
var $form = $e.closest('form'); var $form = $e.closest('form');
var newForm = !$form.length; var newForm = !$form.length;
if (newForm) { if (newForm) {
var action = $e.prop('href'); var action = $e.prop('href');
if (!action || !action.match(/(^\/|:\/\/)/)) { if (!action || !action.match(/(^\/|:\/\/)/)) {
action = window.location.href; action = window.location.href;
} }
$form = $('<form method="' + method + '" action="' + action + '"></form>'); $form = $('<form method="' + method + '" action="' + action + '"></form>');
var target = $e.prop('target'); var target = $e.prop('target');
if (target) { if (target) {
$form.attr('target', target); $form.attr('target', target);
} }
if (!method.match(/(get|post)/i)) { if (!method.match(/(get|post)/i)) {
$form.append('<input name="_method" value="' + method + '" type="hidden">'); $form.append('<input name="_method" value="' + method + '" type="hidden">');
} }
var csrfParam = pub.getCsrfParam(); var csrfParam = pub.getCsrfParam();
if (csrfParam) { if (csrfParam) {
$form.append('<input name="' + csrfParam + '" value="' + pub.getCsrfToken() + '" type="hidden">'); $form.append('<input name="' + csrfParam + '" value="' + pub.getCsrfToken() + '" type="hidden">');
} }
$form.hide().appendTo('body'); $form.hide().appendTo('body');
} }
var activeFormData = $form.data('yiiActiveForm'); var activeFormData = $form.data('yiiActiveForm');
if (activeFormData) { if (activeFormData) {
// remember who triggers the form submission. This is used by yii.activeForm.js // remember who triggers the form submission. This is used by yii.activeForm.js
activeFormData.submitObject = $e; activeFormData.submitObject = $e;
} }
$form.trigger('submit'); $form.trigger('submit');
if (newForm) { if (newForm) {
$form.remove(); $form.remove();
} }
return false; return false;
}, },
getQueryParams: function (url) { getQueryParams: function (url) {
var pos = url.indexOf('?'); var pos = url.indexOf('?');
if (pos < 0) { if (pos < 0) {
return {}; return {};
} }
var qs = url.substring(pos + 1).split('&'); var qs = url.substring(pos + 1).split('&');
for (var i = 0, result = {}; i < qs.length; i++) { for (var i = 0, result = {}; i < qs.length; i++) {
qs[i] = qs[i].split('='); qs[i] = qs[i].split('=');
result[decodeURIComponent(qs[i][0])] = decodeURIComponent(qs[i][1]); result[decodeURIComponent(qs[i][0])] = decodeURIComponent(qs[i][1]);
} }
return result; return result;
}, },
initModule: function (module) { initModule: function (module) {
if (module.isActive === undefined || module.isActive) { if (module.isActive === undefined || module.isActive) {
if ($.isFunction(module.init)) { if ($.isFunction(module.init)) {
module.init(); module.init();
} }
$.each(module, function () { $.each(module, function () {
if ($.isPlainObject(this)) { if ($.isPlainObject(this)) {
pub.initModule(this); pub.initModule(this);
} }
}); });
} }
}, },
init: function () { init: function () {
initCsrfHandler(); initCsrfHandler();
initRedirectHandler(); initRedirectHandler();
initScriptFilter(); initScriptFilter();
initDataMethods(); initDataMethods();
} }
}; };
function initRedirectHandler() { function initRedirectHandler() {
// handle AJAX redirection // handle AJAX redirection
$(document).ajaxComplete(function (event, xhr, settings) { $(document).ajaxComplete(function (event, xhr, settings) {
var url = xhr.getResponseHeader('X-Redirect'); var url = xhr.getResponseHeader('X-Redirect');
if (url) { if (url) {
window.location = url; window.location = url;
} }
}); });
} }
function initCsrfHandler() { function initCsrfHandler() {
// automatically send CSRF token for all AJAX requests // automatically send CSRF token for all AJAX requests
$.ajaxPrefilter(function (options, originalOptions, xhr) { $.ajaxPrefilter(function (options, originalOptions, xhr) {
if (!options.crossDomain && pub.getCsrfParam()) { if (!options.crossDomain && pub.getCsrfParam()) {
xhr.setRequestHeader('X-CSRF-Token', pub.getCsrfToken()); xhr.setRequestHeader('X-CSRF-Token', pub.getCsrfToken());
} }
}); });
} }
function initDataMethods() { function initDataMethods() {
var $document = $(document); var $document = $(document);
// handle data-confirm and data-method for clickable elements // handle data-confirm and data-method for clickable elements
$document.on('click.yii', pub.clickableSelector, function (event) { $document.on('click.yii', pub.clickableSelector, function (event) {
var $this = $(this); var $this = $(this);
if (pub.allowAction($this)) { if (pub.allowAction($this)) {
return pub.handleAction($this); return pub.handleAction($this);
} else { } else {
event.stopImmediatePropagation(); event.stopImmediatePropagation();
return false; return false;
} }
}); });
// handle data-confirm and data-method for changeable elements // handle data-confirm and data-method for changeable elements
$document.on('change.yii', pub.changeableSelector, function (event) { $document.on('change.yii', pub.changeableSelector, function (event) {
var $this = $(this); var $this = $(this);
if (pub.allowAction($this)) { if (pub.allowAction($this)) {
return pub.handleAction($this); return pub.handleAction($this);
} else { } else {
event.stopImmediatePropagation(); event.stopImmediatePropagation();
return false; return false;
} }
}); });
} }
function initScriptFilter() { function initScriptFilter() {
var hostInfo = location.protocol + '//' + location.host; var hostInfo = location.protocol + '//' + location.host;
var loadedScripts = $('script[src]').map(function () { var loadedScripts = $('script[src]').map(function () {
return this.src.charAt(0) === '/' ? hostInfo + this.src : this.src; return this.src.charAt(0) === '/' ? hostInfo + this.src : this.src;
}).toArray(); }).toArray();
$.ajaxPrefilter('script', function (options, originalOptions, xhr) { $.ajaxPrefilter('script', function (options, originalOptions, xhr) {
if(options.dataType == 'jsonp') { if (options.dataType == 'jsonp') {
return; return;
} }
var url = options.url.charAt(0) === '/' ? hostInfo + options.url : options.url; var url = options.url.charAt(0) === '/' ? hostInfo + options.url : options.url;
if ($.inArray(url, loadedScripts) === -1) { if ($.inArray(url, loadedScripts) === -1) {
loadedScripts.push(url); loadedScripts.push(url);
} else { } else {
var found = $.inArray(url, $.map(pub.reloadableScripts, function (script) { var found = $.inArray(url, $.map(pub.reloadableScripts, function (script) {
return script.charAt(0) === '/' ? hostInfo + script : script; return script.charAt(0) === '/' ? hostInfo + script : script;
})) !== -1; })) !== -1;
if (!found) { if (!found) {
xhr.abort(); xhr.abort();
} }
} }
}); });
} }
return pub; return pub;
})(jQuery); })(jQuery);
jQuery(document).ready(function () { jQuery(document).ready(function () {
yii.initModule(yii); yii.initModule(yii);
}); });
...@@ -11,218 +11,218 @@ ...@@ -11,218 +11,218 @@
*/ */
yii.validation = (function ($) { yii.validation = (function ($) {
var pub = { var pub = {
isEmpty: function (value) { isEmpty: function (value) {
return value === null || value === undefined || value == [] || value === ''; return value === null || value === undefined || value == [] || value === '';
}, },
addMessage: function (messages, message, value) { addMessage: function (messages, message, value) {
messages.push(message.replace(/\{value\}/g, value)); messages.push(message.replace(/\{value\}/g, value));
}, },
required: function (value, messages, options) { required: function (value, messages, options) {
var valid = false; var valid = false;
if (options.requiredValue === undefined) { if (options.requiredValue === undefined) {
var isString = typeof value == 'string' || value instanceof String; var isString = typeof value == 'string' || value instanceof String;
if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) { if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) {
valid = true; valid = true;
} }
} else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) { } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) {
valid = true; valid = true;
} }
if (!valid) { if (!valid) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
boolean: function (value, messages, options) { boolean: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
var valid = !options.strict && (value == options.trueValue || value == options.falseValue) var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
|| options.strict && (value === options.trueValue || value === options.falseValue); || options.strict && (value === options.trueValue || value === options.falseValue);
if (!valid) { if (!valid) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
string: function (value, messages, options) { string: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
if (typeof value !== 'string') { if (typeof value !== 'string') {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
return; return;
} }
if (options.min !== undefined && value.length < options.min) { if (options.min !== undefined && value.length < options.min) {
pub.addMessage(messages, options.tooShort, value); pub.addMessage(messages, options.tooShort, value);
} }
if (options.max !== undefined && value.length > options.max) { if (options.max !== undefined && value.length > options.max) {
pub.addMessage(messages, options.tooLong, value); pub.addMessage(messages, options.tooLong, value);
} }
if (options.is !== undefined && value.length != options.is) { if (options.is !== undefined && value.length != options.is) {
pub.addMessage(messages, options.is, value); pub.addMessage(messages, options.is, value);
} }
}, },
number: function (value, messages, options) { number: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
if (typeof value === 'string' && !value.match(options.pattern)) { if (typeof value === 'string' && !value.match(options.pattern)) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
return; return;
} }
if (options.min !== undefined && value < options.min) { if (options.min !== undefined && value < options.min) {
pub.addMessage(messages, options.tooSmall, value); pub.addMessage(messages, options.tooSmall, value);
} }
if (options.max !== undefined && value > options.max) { if (options.max !== undefined && value > options.max) {
pub.addMessage(messages, options.tooBig, value); pub.addMessage(messages, options.tooBig, value);
} }
}, },
range: function (value, messages, options) { range: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
var valid = !options.not && $.inArray(value, options.range) > -1 var valid = !options.not && $.inArray(value, options.range) > -1
|| options.not && $.inArray(value, options.range) == -1; || options.not && $.inArray(value, options.range) == -1;
if (!valid) { if (!valid) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
regularExpression: function (value, messages, options) { regularExpression: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) { if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
email: function (value, messages, options) { email: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
var valid = true; var valid = true;
if (options.enableIDN) { if (options.enableIDN) {
var regexp = /^(.*<?)(.*)@(.*)(>?)$/, var regexp = /^(.*<?)(.*)@(.*)(>?)$/,
matches = regexp.exec(value); matches = regexp.exec(value);
if (matches === null) { if (matches === null) {
valid = false; valid = false;
} else { } else {
value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4]; value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4];
} }
} }
if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) { if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
url: function (value, messages, options) { url: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
if (options.defaultScheme && !value.match(/:\/\//)) { if (options.defaultScheme && !value.match(/:\/\//)) {
value = options.defaultScheme + '://' + value; value = options.defaultScheme + '://' + value;
} }
var valid = true; var valid = true;
if (options.enableIDN) { if (options.enableIDN) {
var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/, var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/,
matches = regexp.exec(value); matches = regexp.exec(value);
if (matches === null) { if (matches === null) {
valid = false; valid = false;
} else { } else {
value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3]; value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3];
} }
} }
if (!valid || !value.match(options.pattern)) { if (!valid || !value.match(options.pattern)) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
captcha: function (value, messages, options) { captcha: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
// CAPTCHA may be updated via AJAX and the updated hash is stored in body data // CAPTCHA may be updated via AJAX and the updated hash is stored in body data
var hash = $('body').data(options.hashKey); var hash = $('body').data(options.hashKey);
if (hash == null) { if (hash == null) {
hash = options.hash; hash = options.hash;
} else { } else {
hash = hash[options.caseSensitive ? 0 : 1]; hash = hash[options.caseSensitive ? 0 : 1];
} }
var v = options.caseSensitive ? value : value.toLowerCase(); var v = options.caseSensitive ? value : value.toLowerCase();
for (var i = v.length - 1, h = 0; i >= 0; --i) { for (var i = v.length - 1, h = 0; i >= 0; --i) {
h += v.charCodeAt(i); h += v.charCodeAt(i);
} }
if (h != hash) { if (h != hash) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
}, },
compare: function (value, messages, options) { compare: function (value, messages, options) {
if (options.skipOnEmpty && pub.isEmpty(value)) { if (options.skipOnEmpty && pub.isEmpty(value)) {
return; return;
} }
var compareValue, valid = true; var compareValue, valid = true;
if (options.compareAttribute === undefined) { if (options.compareAttribute === undefined) {
compareValue = options.compareValue; compareValue = options.compareValue;
} else { } else {
compareValue = $('#' + options.compareAttribute).val(); compareValue = $('#' + options.compareAttribute).val();
} }
switch (options.operator) { switch (options.operator) {
case '==': case '==':
valid = value == compareValue; valid = value == compareValue;
break; break;
case '===': case '===':
valid = value === compareValue; valid = value === compareValue;
break; break;
case '!=': case '!=':
valid = value != compareValue; valid = value != compareValue;
break; break;
case '!==': case '!==':
valid = value !== compareValue; valid = value !== compareValue;
break; break;
case '>': case '>':
valid = value > compareValue; valid = value > compareValue;
break; break;
case '>=': case '>=':
valid = value >= compareValue; valid = value >= compareValue;
break; break;
case '<': case '<':
valid = value < compareValue; valid = value < compareValue;
break; break;
case '<=': case '<=':
valid = value <= compareValue; valid = value <= compareValue;
break; break;
default: default:
valid = false; valid = false;
break; break;
} }
if (!valid) { if (!valid) {
pub.addMessage(messages, options.message, value); pub.addMessage(messages, options.message, value);
} }
} }
}; };
return pub; return pub;
})(jQuery); })(jQuery);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment