From 7960ccdf502e1ed7e0658ebd007a00dfee67fb01 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 16 Mar 2014 20:05:54 -0400 Subject: [PATCH] Tab to spaces conversion. --- extensions/debug/assets/toolbar.js | 78 +-- extensions/gii/assets/gii.js | 242 +++++----- framework/assets/yii.activeForm.js | 742 ++++++++++++++--------------- framework/assets/yii.captcha.js | 102 ++-- framework/assets/yii.gridView.js | 202 ++++---- framework/assets/yii.js | 388 +++++++-------- framework/assets/yii.validation.js | 372 +++++++-------- 7 files changed, 1064 insertions(+), 1062 deletions(-) diff --git a/extensions/debug/assets/toolbar.js b/extensions/debug/assets/toolbar.js index 0dca1deb30..2c6c9b479e 100644 --- a/extensions/debug/assets/toolbar.js +++ b/extensions/debug/assets/toolbar.js @@ -1,41 +1,41 @@ -(function() { - var ajax = function(url, settings) { - var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); - settings = settings || {}; - xhr.open(settings.method || 'GET', url, true); - xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - xhr.onreadystatechange = function(state) { - if (xhr.readyState == 4) { - if (xhr.status == 200 && settings.success) { - settings.success(xhr); - } else if (xhr.status != 200 && settings.error) { - settings.error(xhr); - } - } - }; - xhr.send(settings.data || ''); - }; +(function () { + var ajax = function (url, settings) { + var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); + settings = settings || {}; + xhr.open(settings.method || 'GET', url, true); + xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + xhr.onreadystatechange = function (state) { + if (xhr.readyState == 4) { + if (xhr.status == 200 && settings.success) { + settings.success(xhr); + } else if (xhr.status != 200 && settings.error) { + settings.error(xhr); + } + } + }; + xhr.send(settings.data || ''); + }; - var e = document.getElementById('yii-debug-toolbar'); - if (e) { - e.style.display = 'block'; - var url = e.getAttribute('data-url'); - ajax(url, { - success: function(xhr) { - var div = document.createElement('div'); - div.innerHTML = xhr.responseText; - e.parentNode.replaceChild(div, e); - if (window.localStorage) { - var pref = localStorage.getItem('yii-debug-toolbar'); - if (pref == 'minimized') { - document.getElementById('yii-debug-toolbar').style.display = 'none'; - document.getElementById('yii-debug-toolbar-min').style.display = 'block'; - } - } - }, - error: function(xhr) { - e.innerHTML = xhr.responseText; - } - }); - } + var e = document.getElementById('yii-debug-toolbar'); + if (e) { + e.style.display = 'block'; + var url = e.getAttribute('data-url'); + ajax(url, { + success: function (xhr) { + var div = document.createElement('div'); + div.innerHTML = xhr.responseText; + e.parentNode.replaceChild(div, e); + if (window.localStorage) { + var pref = localStorage.getItem('yii-debug-toolbar'); + if (pref == 'minimized') { + document.getElementById('yii-debug-toolbar').style.display = 'none'; + document.getElementById('yii-debug-toolbar-min').style.display = 'block'; + } + } + }, + error: function (xhr) { + e.innerHTML = xhr.responseText; + } + }); + } })(); diff --git a/extensions/gii/assets/gii.js b/extensions/gii/assets/gii.js index 282ce581c6..a0fd3ec742 100644 --- a/extensions/gii/assets/gii.js +++ b/extensions/gii/assets/gii.js @@ -1,131 +1,133 @@ yii.gii = (function ($) { - var isActive = $('.default-view').length > 0; + var isActive = $('.default-view').length > 0; - var initHintBlocks = function () { - $('.hint-block').each(function () { - var $hint = $(this); - $hint.parent().find('label').addClass('help').popover({ - html: true, - trigger: 'hover', - placement: 'right', - content: $hint.html() - }); - }); - }; + var initHintBlocks = function () { + $('.hint-block').each(function () { + var $hint = $(this); + $hint.parent().find('label').addClass('help').popover({ + html: true, + trigger: 'hover', + placement: 'right', + content: $hint.html() + }); + }); + }; - var initStickyInputs = function () { - $('.sticky:not(.error)').find('input[type="text"],select,textarea').each(function () { - var value; - if (this.tagName === 'SELECT') { - value = this.options[this.selectedIndex].text; - } else if (this.tagName === 'TEXTAREA') { - value = $(this).html(); - } else { - value = $(this).val(); - } - if (value === '') { - value = '[empty]'; - } - $(this).before('
' + value + '
').hide(); - }); - $('.sticky-value').on('click', function () { - $(this).hide(); - $(this).next().show().get(0).focus(); - }); - }; + var initStickyInputs = function () { + $('.sticky:not(.error)').find('input[type="text"],select,textarea').each(function () { + var value; + if (this.tagName === 'SELECT') { + value = this.options[this.selectedIndex].text; + } else if (this.tagName === 'TEXTAREA') { + value = $(this).html(); + } else { + value = $(this).val(); + } + if (value === '') { + value = '[empty]'; + } + $(this).before('
' + value + '
').hide(); + }); + $('.sticky-value').on('click', function () { + $(this).hide(); + $(this).next().show().get(0).focus(); + }); + }; - var initPreviewDiffLinks = function () { - $('.preview-code, .diff-code, .modal-refresh, .modal-previous, .modal-next').on('click', function () { - var $modal = $('#preview-modal'); - var $link = $(this); - $modal.find('.modal-refresh').attr('href', $link.attr('href')); - if ($link.hasClass('preview-code') || $link.hasClass('diff-code')) { - $modal.data('action', ($link.hasClass('preview-code') ? 'preview-code' : 'diff-code')) - } - $modal.find('.modal-title').text($link.data('title')); - $modal.find('.modal-body').html('Loading ...'); - $modal.modal('show'); - $.ajax({ - type: 'POST', - cache: false, - url: $link.prop('href'), - data: $('.default-view form').serializeArray(), - success: function (data) { - if (!$link.hasClass('modal-refresh')) { - var filesSelector = 'a.' + $modal.data('action'); - var $files = $(filesSelector); - var index = $files.filter('[href="' + $link.attr('href') + '"]').index(filesSelector); - var $prev = $files.eq(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-next').attr('href', $next.attr('href')).data('title', $next.data('title')); - } - $modal.find('.modal-body').html(data); - $modal.find('.content').css('max-height', ($(window).height() - 200) + 'px'); - }, - error: function (XMLHttpRequest, textStatus, errorThrown) { - $modal.find('.modal-body').html('
' + XMLHttpRequest.responseText + '
'); - } - }); - return false; - }); + var initPreviewDiffLinks = function () { + $('.preview-code, .diff-code, .modal-refresh, .modal-previous, .modal-next').on('click', function () { + var $modal = $('#preview-modal'); + var $link = $(this); + $modal.find('.modal-refresh').attr('href', $link.attr('href')); + if ($link.hasClass('preview-code') || $link.hasClass('diff-code')) { + $modal.data('action', ($link.hasClass('preview-code') ? 'preview-code' : 'diff-code')) + } + $modal.find('.modal-title').text($link.data('title')); + $modal.find('.modal-body').html('Loading ...'); + $modal.modal('show'); + $.ajax({ + type: 'POST', + cache: false, + url: $link.prop('href'), + data: $('.default-view form').serializeArray(), + success: function (data) { + if (!$link.hasClass('modal-refresh')) { + var filesSelector = 'a.' + $modal.data('action'); + var $files = $(filesSelector); + var index = $files.filter('[href="' + $link.attr('href') + '"]').index(filesSelector); + var $prev = $files.eq(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-next').attr('href', $next.attr('href')).data('title', $next.data('title')); + } + $modal.find('.modal-body').html(data); + $modal.find('.content').css('max-height', ($(window).height() - 200) + 'px'); + }, + error: function (XMLHttpRequest, textStatus, errorThrown) { + $modal.find('.modal-body').html('
' + XMLHttpRequest.responseText + '
'); + } + }); + return false; + }); - $('#preview-modal').on('keydown', function(e) { - if (e.keyCode === 37) { - $('.modal-previous').trigger('click'); - } else if(e.keyCode === 39) { - $('.modal-next').trigger('click'); - } else if(e.keyCode === 82) { - $('.modal-refresh').trigger('click'); - } - }); - }; + $('#preview-modal').on('keydown', function (e) { + if (e.keyCode === 37) { + $('.modal-previous').trigger('click'); + } else if (e.keyCode === 39) { + $('.modal-next').trigger('click'); + } else if (e.keyCode === 82) { + $('.modal-refresh').trigger('click'); + } + }); + }; - var initConfirmationCheckboxes = function () { - var $checkAll = $('#check-all'); - $checkAll.click(function () { - $('.default-view-files table .check input').prop('checked', this.checked); - }); - $('.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); - }; + var initConfirmationCheckboxes = function () { + var $checkAll = $('#check-all'); + $checkAll.click(function () { + $('.default-view-files table .check input').prop('checked', this.checked); + }); + $('.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); + }; - return { - autocomplete: function (counter, data) { - var datum = new Bloodhound({ - datumTokenizer: function(d){return Bloodhound.tokenizers.whitespace(d.word);}, - queryTokenizer: Bloodhound.tokenizers.whitespace, - local: data - }); - datum.initialize(); - jQuery('.typeahead-'+counter).typeahead(null,{displayKey: 'word', source: datum.ttAdapter()}); - }, - init: function () { - initHintBlocks(); - initStickyInputs(); - initPreviewDiffLinks(); - initConfirmationCheckboxes(); + return { + autocomplete: function (counter, data) { + var datum = new Bloodhound({ + datumTokenizer: function (d) { + return Bloodhound.tokenizers.whitespace(d.word); + }, + queryTokenizer: Bloodhound.tokenizers.whitespace, + local: data + }); + datum.initialize(); + jQuery('.typeahead-' + counter).typeahead(null, {displayKey: 'word', source: datum.ttAdapter()}); + }, + init: function () { + initHintBlocks(); + initStickyInputs(); + initPreviewDiffLinks(); + initConfirmationCheckboxes(); - // model generator: hide class name input when table name input contains * - $('#model-generator #generator-tablename').on('change', function () { - $('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1); - }).change(); + // model generator: hide class name input when table name input contains * + $('#model-generator #generator-tablename').on('change',function () { + $('#model-generator .field-generator-modelclass').toggle($(this).val().indexOf('*') == -1); + }).change(); - // hide Generate button if any input is changed - $('.default-view .form-group input,select,textarea').change(function () { - $('.default-view-results,.default-view-files').hide(); - $('.default-view button[name="generate"]').hide(); - }); + // hide Generate button if any input is changed + $('.default-view .form-group input,select,textarea').change(function () { + $('.default-view-results,.default-view-files').hide(); + $('.default-view button[name="generate"]').hide(); + }); - $('.module-form #generator-moduleclass').change(function () { - var value = $(this).val().match(/(\w+)\\\w+$/); - var $idInput = $('#generator-moduleid'); - if (value && value[1] && $idInput.val() == '') { - $idInput.val(value[1]); - } - }); - } - }; + $('.module-form #generator-moduleclass').change(function () { + var value = $(this).val().match(/(\w+)\\\w+$/); + var $idInput = $('#generator-moduleid'); + if (value && value[1] && $idInput.val() == '') { + $idInput.val(value[1]); + } + }); + } + }; })(jQuery); diff --git a/framework/assets/yii.activeForm.js b/framework/assets/yii.activeForm.js index 335d73e953..c1509c57eb 100644 --- a/framework/assets/yii.activeForm.js +++ b/framework/assets/yii.activeForm.js @@ -10,400 +10,400 @@ * @since 2.0 */ (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 = { - // the jQuery selector for the error summary - errorSummary: undefined, - // whether to perform validation before submitting the form. - validateOnSubmit: true, - // the container CSS class representing the corresponding attribute has validation error - errorCssClass: 'error', - // the container CSS class representing the corresponding attribute passes validation - 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' - }; + $.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 attributeDefaults = { - // attribute name or expression (e.g. "[0]content" for tabular input) - name: undefined, - // the jQuery selector of the container of the input field - container: undefined, - // 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 defaults = { + // the jQuery selector for the error summary + errorSummary: undefined, + // whether to perform validation before submitting the form. + validateOnSubmit: true, + // the container CSS class representing the corresponding attribute has validation error + errorCssClass: 'error', + // the container CSS class representing the corresponding attribute passes validation + 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 methods = { - init: function (attributes, options) { - return this.each(function () { - var $form = $(this); - if ($form.data('yiiActiveForm')) { - return; - } + var attributeDefaults = { + // attribute name or expression (e.g. "[0]content" for tabular input) + name: undefined, + // the jQuery selector of the container of the input field + container: undefined, + // 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 || {}); - 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 - }); + var methods = { + init: function (attributes, options) { + return this.each(function () { + var $form = $(this); + if ($form.data('yiiActiveForm')) { + return; + } - 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 + }); - /** - * 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); + watchAttributes($form, attributes); - if (settings.validateOnSubmit) { - $form.on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () { - $form.data('yiiActiveForm').submitObject = $(this); - }); - $form.on('submit', methods.submitForm); - } - }); - }, + /** + * 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); - destroy: function () { - return this.each(function () { - $(window).unbind('.yiiActiveForm'); - $(this).removeData('yiiActiveForm'); - }); - }, + if (settings.validateOnSubmit) { + $form.on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () { + $form.data('yiiActiveForm').submitObject = $(this); + }); + $form.on('submit', methods.submitForm); + } + }); + }, - data: function() { - return this.data('yiiActiveForm'); - }, + destroy: function () { + return this.each(function () { + $(window).unbind('.yiiActiveForm'); + $(this).removeData('yiiActiveForm'); + }); + }, - submitForm: function () { - var $form = $(this), - 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; - } + data: function () { + return this.data('yiiActiveForm'); + }, - if (data.settings.timer !== undefined) { - clearTimeout(data.settings.timer); - } - data.submitting = true; - validate($form, function (messages) { - var errors = []; - $.each(data.attributes, function () { - if (updateInput($form, this, messages)) { - errors.push(this.input); - } - }); - updateSummary($form, messages); - if (errors.length) { - 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; - }, + submitForm: function () { + var $form = $(this), + 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; + } - resetForm: function () { - var $form = $(this); - var data = $form.data('yiiActiveForm'); - // Because we bind directly to a form reset event instead of a reset button (that may not exist), - // when this function is executed form input values have not been reset yet. - // Therefore we do the actual reset work through setTimeout. - setTimeout(function () { - $.each(data.attributes, function () { - // 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); - $container.removeClass( - data.settings.validatingCssClass + ' ' + - data.settings.errorCssClass + ' ' + - data.settings.successCssClass - ); - $container.find(this.error).html(''); - }); - $form.find(data.settings.summary).hide().find('ul').html(''); - }, 1); - } - }; + if (data.settings.timer !== undefined) { + clearTimeout(data.settings.timer); + } + data.submitting = true; + validate($form, function (messages) { + var errors = []; + $.each(data.attributes, function () { + if (updateInput($form, this, messages)) { + errors.push(this.input); + } + }); + updateSummary($form, messages); + if (errors.length) { + 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; + }, - var watchAttributes = function ($form, attributes) { - $.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); - } - }); - } - }); - }; + resetForm: function () { + var $form = $(this); + var data = $form.data('yiiActiveForm'); + // Because we bind directly to a form reset event instead of a reset button (that may not exist), + // when this function is executed form input values have not been reset yet. + // Therefore we do the actual reset work through setTimeout. + setTimeout(function () { + $.each(data.attributes, function () { + // 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); + $container.removeClass( + data.settings.validatingCssClass + ' ' + + data.settings.errorCssClass + ' ' + + 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 data = $form.data('yiiActiveForm'); + var watchAttributes = function ($form, attributes) { + $.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) { - attribute.status = 2; - } - $.each(data.attributes, function () { - if (this.value !== getValue($form, this)) { - this.status = 2; - forceValidate = true; - } - }); - if (!forceValidate) { - return; - } + var validateAttribute = function ($form, attribute, forceValidate) { + var data = $form.data('yiiActiveForm'); - if (data.settings.timer !== undefined) { - clearTimeout(data.settings.timer); - } - data.settings.timer = setTimeout(function () { - if (data.submitting || $form.is(':hidden')) { - return; - } - $.each(data.attributes, function () { - if (this.status === 2) { - this.status = 3; - $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); - }; - - /** - * 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 = {}; + if (forceValidate) { + attribute.status = 2; + } + $.each(data.attributes, function () { + if (this.value !== getValue($form, this)) { + this.status = 2; + forceValidate = true; + } + }); + if (!forceValidate) { + return; + } - $.each(data.attributes, function () { - if (data.submitting || this.status === 2 || this.status === 3) { - var msg = []; - if (!data.settings.beforeValidate || data.settings.beforeValidate($form, this, msg)) { - if (this.validate) { - this.validate(this, getValue($form, this), msg); - } - if (msg.length) { - messages[this.name] = msg; - } else if (this.enableAjaxValidation) { - needAjaxValidation = true; - } - } - } - }); + if (data.settings.timer !== undefined) { + clearTimeout(data.settings.timer); + } + data.settings.timer = setTimeout(function () { + if (data.submitting || $form.is(':hidden')) { + return; + } + $.each(data.attributes, function () { + if (this.status === 2) { + this.status = 3; + $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. - // If the validation is triggered by form submission, ajax validation - // should be done only when all inputs pass client validation - var $button = data.submitObject, - extData = '&' + data.settings.ajaxParam + '=' + $form.prop('id'); - if ($button && $button.length && $button.prop('name')) { - extData += '&' + $button.prop('name') + '=' + $button.prop('value'); - } - $.ajax({ - 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); - } - }; + /** + * 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 = {}; - /** - * Updates the error message and the input container for a particular attribute. - * @param $form the form jQuery object - * @param attribute object the configuration for a particular attribute. - * @param messages array the validation error messages - * @return boolean whether there is a validation error for the specified attribute - */ - var updateInput = function ($form, attribute, messages) { - var data = $form.data('yiiActiveForm'), - $input = findInput($form, attribute), - hasError = false; + $.each(data.attributes, function () { + if (data.submitting || this.status === 2 || this.status === 3) { + var msg = []; + if (!data.settings.beforeValidate || data.settings.beforeValidate($form, this, msg)) { + if (this.validate) { + this.validate(this, getValue($form, this), msg); + } + if (msg.length) { + messages[this.name] = msg; + } else if (this.enableAjaxValidation) { + needAjaxValidation = true; + } + } + } + }); - if (data.settings.afterValidate) { - data.settings.afterValidate($form, attribute, messages); - } - attribute.status = 1; - if ($input.length) { - hasError = messages && $.isArray(messages[attribute.name]) && messages[attribute.name].length; - 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; - }; + if (needAjaxValidation && (!data.submitting || $.isEmptyObject(messages))) { + // Perform ajax validation when at least one input needs it. + // If the validation is triggered by form submission, ajax validation + // should be done only when all inputs pass client validation + var $button = data.submitObject, + extData = '&' + data.settings.ajaxParam + '=' + $form.prop('id'); + if ($button && $button.length && $button.prop('name')) { + extData += '&' + $button.prop('name') + '=' + $button.prop('value'); + } + $.ajax({ + 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); + } + }; - /** - * Updates the error summary. - * @param $form the form jQuery object - * @param messages array the validation error messages - */ - var updateSummary = function ($form, messages) { - var data = $form.data('yiiActiveForm'), - $summary = $form.find(data.settings.errorSummary), - $ul = $summary.find('ul').html(''); + /** + * Updates the error message and the input container for a particular attribute. + * @param $form the form jQuery object + * @param attribute object the configuration for a particular attribute. + * @param messages array the validation error messages + * @return boolean whether there is a validation error for the specified attribute + */ + var updateInput = function ($form, attribute, messages) { + var data = $form.data('yiiActiveForm'), + $input = findInput($form, attribute), + hasError = false; - if ($summary.length && messages) { - $.each(data.attributes, function () { - if ($.isArray(messages[this.name]) && messages[this.name].length) { - $ul.append($('
  • ').text(messages[this.name][0])); - } - }); - $summary.toggle($ul.find('li').length > 0); - } - }; + if (data.settings.afterValidate) { + data.settings.afterValidate($form, attribute, messages); + } + attribute.status = 1; + if ($input.length) { + hasError = messages && $.isArray(messages[attribute.name]) && messages[attribute.name].length; + 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); - 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(); - } - }; + /** + * Updates the error summary. + * @param $form the form jQuery object + * @param messages array the validation error messages + */ + var updateSummary = function ($form, messages) { + var data = $form.data('yiiActiveForm'), + $summary = $form.find(data.settings.errorSummary), + $ul = $summary.find('ul').html(''); - 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; - } - }; + if ($summary.length && messages) { + $.each(data.attributes, function () { + if ($.isArray(messages[this.name]) && messages[this.name].length) { + $ul.append($('
  • ').text(messages[this.name][0])); + } + }); + $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); diff --git a/framework/assets/yii.captcha.js b/framework/assets/yii.captcha.js index af14faa2c8..b5c01c5753 100644 --- a/framework/assets/yii.captcha.js +++ b/framework/assets/yii.captcha.js @@ -10,63 +10,63 @@ * @since 2.0 */ (function ($) { - $.fn.yiiCaptcha = 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.yiiCaptcha'); - return false; - } - }; + $.fn.yiiCaptcha = 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.yiiCaptcha'); + return false; + } + }; - var defaults = { - refreshUrl: undefined, - hashKey: undefined - }; + var defaults = { + refreshUrl: undefined, + hashKey: undefined + }; - var methods = { - init: function (options) { - return this.each(function () { - var $e = $(this); - var settings = $.extend({}, defaults, options || {}); - $e.data('yiiCaptcha', { - settings: settings - }); + var methods = { + init: function (options) { + return this.each(function () { + var $e = $(this); + var settings = $.extend({}, defaults, options || {}); + $e.data('yiiCaptcha', { + settings: settings + }); - $e.on('click.yiiCaptcha', function() { - methods.refresh.apply($e); - return false; - }); + $e.on('click.yiiCaptcha', function () { + methods.refresh.apply($e); + return false; + }); - }); - }, + }); + }, - refresh: function () { - var $e = this, - settings = this.data('yiiCaptcha').settings; - $.ajax({ - url: $e.data('yiiCaptcha').settings.refreshUrl, - dataType: 'json', - cache: false, - success: function(data) { - $e.attr('src', data.url); - $('body').data(settings.hashKey, [data.hash1, data.hash2]); - } - }); - }, + refresh: function () { + var $e = this, + settings = this.data('yiiCaptcha').settings; + $.ajax({ + url: $e.data('yiiCaptcha').settings.refreshUrl, + dataType: 'json', + cache: false, + success: function (data) { + $e.attr('src', data.url); + $('body').data(settings.hashKey, [data.hash1, data.hash2]); + } + }); + }, - destroy: function () { - return this.each(function () { - $(window).unbind('.yiiCaptcha'); - $(this).removeData('yiiCaptcha'); - }); - }, + destroy: function () { + return this.each(function () { + $(window).unbind('.yiiCaptcha'); + $(this).removeData('yiiCaptcha'); + }); + }, - data: function() { - return this.data('yiiCaptcha'); - } - }; + data: function () { + return this.data('yiiCaptcha'); + } + }; })(window.jQuery); diff --git a/framework/assets/yii.gridView.js b/framework/assets/yii.gridView.js index ca2b99a278..5bf65ef0e1 100644 --- a/framework/assets/yii.gridView.js +++ b/framework/assets/yii.gridView.js @@ -10,120 +10,120 @@ * @since 2.0 */ (function ($) { - $.fn.yiiGridView = 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.yiiGridView'); - return false; - } - }; + $.fn.yiiGridView = 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.yiiGridView'); + return false; + } + }; - var defaults = { - filterUrl: undefined, - filterSelector: undefined - }; + var defaults = { + filterUrl: undefined, + filterSelector: undefined + }; - var gridData = {}; + var gridData = {}; - var methods = { - init: function (options) { - return this.each(function () { - var $e = $(this); - var settings = $.extend({}, defaults, options || {}); - gridData[$e.prop('id')] = {settings: settings}; + var methods = { + init: function (options) { + return this.each(function () { + var $e = $(this); + var settings = $.extend({}, defaults, options || {}); + gridData[$e.prop('id')] = {settings: settings}; - var enterPressed = false; - $(document).off('change.yiiGridView keydown.yiiGridView', settings.filterSelector) - .on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) { - if (event.type === 'keydown') { - if (event.keyCode !== 13) { - return; // only react to enter key - } else { - enterPressed = true; - } - } else { - // prevent processing for both keydown and change events - if (enterPressed) { - enterPressed = false; - return; - } - } + var enterPressed = false; + $(document).off('change.yiiGridView keydown.yiiGridView', settings.filterSelector) + .on('change.yiiGridView keydown.yiiGridView', settings.filterSelector, function (event) { + if (event.type === 'keydown') { + if (event.keyCode !== 13) { + return; // only react to enter key + } else { + enterPressed = true; + } + } else { + // prevent processing for both keydown and change events + if (enterPressed) { + enterPressed = false; + return; + } + } - methods.applyFilter.apply($e); + methods.applyFilter.apply($e); - return false; - }); - }); - }, + return false; + }); + }); + }, - applyFilter: function () { - var $grid = $(this); - var settings = gridData[$grid.prop('id')].settings; - var data = {}; - $.each($(settings.filterSelector).serializeArray(), function () { - data[this.name] = this.value; - }); + applyFilter: function () { + var $grid = $(this); + var settings = gridData[$grid.prop('id')].settings; + var data = {}; + $.each($(settings.filterSelector).serializeArray(), function () { + data[this.name] = this.value; + }); - $.each(yii.getQueryParams(settings.filterUrl), function (name, value) { - if (data[name] === undefined) { - data[name] = value; - } - }); + $.each(yii.getQueryParams(settings.filterUrl), function (name, value) { + if (data[name] === undefined) { + data[name] = value; + } + }); - var pos = settings.filterUrl.indexOf('?'); - var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos); + var pos = settings.filterUrl.indexOf('?'); + var url = pos < 0 ? settings.filterUrl : settings.filterUrl.substring(0, pos); - $grid.find('form.gridview-filter-form').remove(); - var $form = $('').appendTo($grid); - $.each(data, function (name, value) { - $form.append($('').attr('name', name).val(value)); - }); - $form.submit(); - }, + $grid.find('form.gridview-filter-form').remove(); + var $form = $('').appendTo($grid); + $.each(data, function (name, value) { + $form.append($('').attr('name', name).val(value)); + }); + $form.submit(); + }, - setSelectionColumn: function (options) { - var $grid = $(this); - var id = $(this).prop('id'); - gridData[id].selectionColumn = options.name; - if (!options.multiple) { - return; - } - var inputs = "#" + id + " input[name='" + options.checkAll + "']"; - $(document).off('click.yiiGridView', inputs).on('click.yiiGridView', inputs, function () { - $grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked); - }); - $(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; - $grid.find("input[name='" + options.checkAll + "']").prop('checked', all); - }); - }, + setSelectionColumn: function (options) { + var $grid = $(this); + var id = $(this).prop('id'); + gridData[id].selectionColumn = options.name; + if (!options.multiple) { + return; + } + var inputs = "#" + id + " input[name='" + options.checkAll + "']"; + $(document).off('click.yiiGridView', inputs).on('click.yiiGridView', inputs, function () { + $grid.find("input[name='" + options.name + "']:enabled").prop('checked', this.checked); + }); + $(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; + $grid.find("input[name='" + options.checkAll + "']").prop('checked', all); + }); + }, - getSelectedRows: function () { - var $grid = $(this); - var data = gridData[$grid.prop('id')]; - var keys = []; - if (data.selectionColumn) { - $grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () { - keys.push($(this).parent().closest('tr').data('key')); - }); - } - return keys; - }, + getSelectedRows: function () { + var $grid = $(this); + var data = gridData[$grid.prop('id')]; + var keys = []; + if (data.selectionColumn) { + $grid.find("input[name='" + data.selectionColumn + "']:checked").each(function () { + keys.push($(this).parent().closest('tr').data('key')); + }); + } + return keys; + }, - destroy: function () { - return this.each(function () { - $(window).unbind('.yiiGridView'); - $(this).removeData('yiiGridView'); - }); - }, + destroy: function () { + return this.each(function () { + $(window).unbind('.yiiGridView'); + $(this).removeData('yiiGridView'); + }); + }, - data: function () { - var id = $(this).prop('id'); - return gridData[id]; - } - }; + data: function () { + var id = $(this).prop('id'); + return gridData[id]; + } + }; })(window.jQuery); diff --git a/framework/assets/yii.js b/framework/assets/yii.js index b85d19b9ff..e4c62b183f 100644 --- a/framework/assets/yii.js +++ b/framework/assets/yii.js @@ -42,220 +42,220 @@ * You must call "yii.initModule()" once for the root module of all your modules. */ yii = (function ($) { - var pub = { - /** - * 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. - */ - reloadableScripts: [], - /** - * 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"]', - /** - * The selector for changeable elements that need to support confirmation and form submission. - */ - changeableSelector: 'select, input, textarea', + var pub = { + /** + * 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. + */ + reloadableScripts: [], + /** + * 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"]', + /** + * The selector for changeable elements that need to support confirmation and form submission. + */ + changeableSelector: 'select, input, textarea', - /** - * @return string|undefined the CSRF parameter name. Undefined is returned if CSRF validation is not enabled. - */ - getCsrfParam: function () { - return $('meta[name=csrf-param]').prop('content'); - }, + /** + * @return string|undefined the CSRF parameter name. Undefined is returned if CSRF validation is not enabled. + */ + getCsrfParam: function () { + return $('meta[name=csrf-param]').prop('content'); + }, - /** - * @return string|undefined the CSRF token. Undefined is returned if CSRF validation is not enabled. - */ - getCsrfToken: function () { - return $('meta[name=csrf-token]').prop('content'); - }, + /** + * @return string|undefined the CSRF token. Undefined is returned if CSRF validation is not enabled. + */ + getCsrfToken: function () { + return $('meta[name=csrf-token]').prop('content'); + }, - /** - * Displays a confirmation dialog. - * The default implementation simply displays a js confirmation dialog. - * You may override this by setting `yii.confirm`. - * @param message the confirmation message. - * @return boolean whether the user confirms with the message in the dialog - */ - confirm: function (message) { - return confirm(message); - }, + /** + * Displays a confirmation dialog. + * The default implementation simply displays a js confirmation dialog. + * You may override this by setting `yii.confirm`. + * @param message the confirmation message. + * @return boolean whether the user confirms with the message in the dialog + */ + confirm: function (message) { + return confirm(message); + }, - /** - * 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 - * 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. - * @param $e the jQuery representation of the element - * @return boolean whether to allow executing the action defined for the specified element. - */ - allowAction: function ($e) { - var message = $e.data('confirm'); - return message === undefined || pub.confirm(message); - }, + /** + * 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 + * 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. + * @param $e the jQuery representation of the element + * @return boolean whether to allow executing the action defined for the specified element. + */ + allowAction: function ($e) { + var message = $e.data('confirm'); + return message === undefined || pub.confirm(message); + }, - /** - * Handles the action triggered by user. - * 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 - * 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 other elements, either the containing form action or the current page URL will be used - * as the form action URL. - * - * If the `data-method` attribute is not defined, the default element action will be performed. - * - * @param $e the jQuery representation of the element - * @return boolean whether to execute the default action for the element. - */ - handleAction: function ($e) { - var method = $e.data('method'); - if (method === undefined) { - return true; - } + /** + * Handles the action triggered by user. + * 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 + * 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 other elements, either the containing form action or the current page URL will be used + * as the form action URL. + * + * If the `data-method` attribute is not defined, the default element action will be performed. + * + * @param $e the jQuery representation of the element + * @return boolean whether to execute the default action for the element. + */ + handleAction: function ($e) { + var method = $e.data('method'); + if (method === undefined) { + return true; + } - var $form = $e.closest('form'); - var newForm = !$form.length; - if (newForm) { - var action = $e.prop('href'); - if (!action || !action.match(/(^\/|:\/\/)/)) { - action = window.location.href; - } - $form = $('
    '); - var target = $e.prop('target'); - if (target) { - $form.attr('target', target); - } - if (!method.match(/(get|post)/i)) { - $form.append(''); - } - var csrfParam = pub.getCsrfParam(); - if (csrfParam) { - $form.append(''); - } - $form.hide().appendTo('body'); - } + var $form = $e.closest('form'); + var newForm = !$form.length; + if (newForm) { + var action = $e.prop('href'); + if (!action || !action.match(/(^\/|:\/\/)/)) { + action = window.location.href; + } + $form = $('
    '); + var target = $e.prop('target'); + if (target) { + $form.attr('target', target); + } + if (!method.match(/(get|post)/i)) { + $form.append(''); + } + var csrfParam = pub.getCsrfParam(); + if (csrfParam) { + $form.append(''); + } + $form.hide().appendTo('body'); + } - var activeFormData = $form.data('yiiActiveForm'); - if (activeFormData) { - // remember who triggers the form submission. This is used by yii.activeForm.js - activeFormData.submitObject = $e; - } + var activeFormData = $form.data('yiiActiveForm'); + if (activeFormData) { + // remember who triggers the form submission. This is used by yii.activeForm.js + activeFormData.submitObject = $e; + } - $form.trigger('submit'); + $form.trigger('submit'); - if (newForm) { - $form.remove(); - } + if (newForm) { + $form.remove(); + } - return false; - }, + return false; + }, - getQueryParams: function (url) { - var pos = url.indexOf('?'); - if (pos < 0) { - return {}; - } - var qs = url.substring(pos + 1).split('&'); - for (var i = 0, result = {}; i < qs.length; i++) { - qs[i] = qs[i].split('='); - result[decodeURIComponent(qs[i][0])] = decodeURIComponent(qs[i][1]); - } - return result; - }, + getQueryParams: function (url) { + var pos = url.indexOf('?'); + if (pos < 0) { + return {}; + } + var qs = url.substring(pos + 1).split('&'); + for (var i = 0, result = {}; i < qs.length; i++) { + qs[i] = qs[i].split('='); + result[decodeURIComponent(qs[i][0])] = decodeURIComponent(qs[i][1]); + } + return result; + }, - initModule: function (module) { - if (module.isActive === undefined || module.isActive) { - if ($.isFunction(module.init)) { - module.init(); - } - $.each(module, function () { - if ($.isPlainObject(this)) { - pub.initModule(this); - } - }); - } - }, + initModule: function (module) { + if (module.isActive === undefined || module.isActive) { + if ($.isFunction(module.init)) { + module.init(); + } + $.each(module, function () { + if ($.isPlainObject(this)) { + pub.initModule(this); + } + }); + } + }, - init: function () { - initCsrfHandler(); - initRedirectHandler(); - initScriptFilter(); - initDataMethods(); - } - }; + init: function () { + initCsrfHandler(); + initRedirectHandler(); + initScriptFilter(); + initDataMethods(); + } + }; - function initRedirectHandler() { - // handle AJAX redirection - $(document).ajaxComplete(function (event, xhr, settings) { - var url = xhr.getResponseHeader('X-Redirect'); - if (url) { - window.location = url; - } - }); - } + function initRedirectHandler() { + // handle AJAX redirection + $(document).ajaxComplete(function (event, xhr, settings) { + var url = xhr.getResponseHeader('X-Redirect'); + if (url) { + window.location = url; + } + }); + } - function initCsrfHandler() { - // automatically send CSRF token for all AJAX requests - $.ajaxPrefilter(function (options, originalOptions, xhr) { - if (!options.crossDomain && pub.getCsrfParam()) { - xhr.setRequestHeader('X-CSRF-Token', pub.getCsrfToken()); - } - }); - } + function initCsrfHandler() { + // automatically send CSRF token for all AJAX requests + $.ajaxPrefilter(function (options, originalOptions, xhr) { + if (!options.crossDomain && pub.getCsrfParam()) { + xhr.setRequestHeader('X-CSRF-Token', pub.getCsrfToken()); + } + }); + } - function initDataMethods() { - var $document = $(document); - // handle data-confirm and data-method for clickable elements - $document.on('click.yii', pub.clickableSelector, function (event) { - var $this = $(this); - if (pub.allowAction($this)) { - return pub.handleAction($this); - } else { - event.stopImmediatePropagation(); - return false; - } - }); + function initDataMethods() { + var $document = $(document); + // handle data-confirm and data-method for clickable elements + $document.on('click.yii', pub.clickableSelector, function (event) { + var $this = $(this); + if (pub.allowAction($this)) { + return pub.handleAction($this); + } else { + event.stopImmediatePropagation(); + return false; + } + }); - // handle data-confirm and data-method for changeable elements - $document.on('change.yii', pub.changeableSelector, function (event) { - var $this = $(this); - if (pub.allowAction($this)) { - return pub.handleAction($this); - } else { - event.stopImmediatePropagation(); - return false; - } - }); - } + // handle data-confirm and data-method for changeable elements + $document.on('change.yii', pub.changeableSelector, function (event) { + var $this = $(this); + if (pub.allowAction($this)) { + return pub.handleAction($this); + } else { + event.stopImmediatePropagation(); + return false; + } + }); + } - function initScriptFilter() { - var hostInfo = location.protocol + '//' + location.host; - var loadedScripts = $('script[src]').map(function () { - return this.src.charAt(0) === '/' ? hostInfo + this.src : this.src; - }).toArray(); - $.ajaxPrefilter('script', function (options, originalOptions, xhr) { - if(options.dataType == 'jsonp') { - return; - } - var url = options.url.charAt(0) === '/' ? hostInfo + options.url : options.url; - if ($.inArray(url, loadedScripts) === -1) { - loadedScripts.push(url); - } else { - var found = $.inArray(url, $.map(pub.reloadableScripts, function (script) { - return script.charAt(0) === '/' ? hostInfo + script : script; - })) !== -1; - if (!found) { - xhr.abort(); - } - } - }); - } + function initScriptFilter() { + var hostInfo = location.protocol + '//' + location.host; + var loadedScripts = $('script[src]').map(function () { + return this.src.charAt(0) === '/' ? hostInfo + this.src : this.src; + }).toArray(); + $.ajaxPrefilter('script', function (options, originalOptions, xhr) { + if (options.dataType == 'jsonp') { + return; + } + var url = options.url.charAt(0) === '/' ? hostInfo + options.url : options.url; + if ($.inArray(url, loadedScripts) === -1) { + loadedScripts.push(url); + } else { + var found = $.inArray(url, $.map(pub.reloadableScripts, function (script) { + return script.charAt(0) === '/' ? hostInfo + script : script; + })) !== -1; + if (!found) { + xhr.abort(); + } + } + }); + } - return pub; + return pub; })(jQuery); jQuery(document).ready(function () { - yii.initModule(yii); + yii.initModule(yii); }); diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index 2b80af2cc7..381e66e34a 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -11,218 +11,218 @@ */ yii.validation = (function ($) { - var pub = { - isEmpty: function (value) { - return value === null || value === undefined || value == [] || value === ''; - }, + var pub = { + isEmpty: function (value) { + return value === null || value === undefined || value == [] || value === ''; + }, - addMessage: function (messages, message, value) { - messages.push(message.replace(/\{value\}/g, value)); - }, - - required: function (value, messages, options) { - var valid = false; - if (options.requiredValue === undefined) { - var isString = typeof value == 'string' || value instanceof String; - if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) { - valid = true; - } - } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) { - valid = true; - } + addMessage: function (messages, message, value) { + messages.push(message.replace(/\{value\}/g, value)); + }, - if (!valid) { - pub.addMessage(messages, options.message, value); - } - }, + required: function (value, messages, options) { + var valid = false; + if (options.requiredValue === undefined) { + var isString = typeof value == 'string' || value instanceof String; + if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) { + valid = true; + } + } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) { + valid = true; + } - boolean: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } - var valid = !options.strict && (value == options.trueValue || value == options.falseValue) - || options.strict && (value === options.trueValue || value === options.falseValue); + if (!valid) { + pub.addMessage(messages, options.message, value); + } + }, - if (!valid) { - pub.addMessage(messages, options.message, value); - } - }, + boolean: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } + var valid = !options.strict && (value == options.trueValue || value == options.falseValue) + || options.strict && (value === options.trueValue || value === options.falseValue); - string: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + if (!valid) { + pub.addMessage(messages, options.message, value); + } + }, - if (typeof value !== 'string') { - pub.addMessage(messages, options.message, value); - return; - } + string: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - if (options.min !== undefined && value.length < options.min) { - pub.addMessage(messages, options.tooShort, value); - } - if (options.max !== undefined && value.length > options.max) { - pub.addMessage(messages, options.tooLong, value); - } - if (options.is !== undefined && value.length != options.is) { - pub.addMessage(messages, options.is, value); - } - }, + if (typeof value !== 'string') { + pub.addMessage(messages, options.message, value); + return; + } - number: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + if (options.min !== undefined && value.length < options.min) { + pub.addMessage(messages, options.tooShort, value); + } + if (options.max !== undefined && value.length > options.max) { + pub.addMessage(messages, options.tooLong, value); + } + if (options.is !== undefined && value.length != options.is) { + pub.addMessage(messages, options.is, value); + } + }, - if (typeof value === 'string' && !value.match(options.pattern)) { - pub.addMessage(messages, options.message, value); - return; - } + number: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - if (options.min !== undefined && value < options.min) { - pub.addMessage(messages, options.tooSmall, value); - } - if (options.max !== undefined && value > options.max) { - pub.addMessage(messages, options.tooBig, value); - } - }, + if (typeof value === 'string' && !value.match(options.pattern)) { + pub.addMessage(messages, options.message, value); + return; + } - range: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } - var valid = !options.not && $.inArray(value, options.range) > -1 - || options.not && $.inArray(value, options.range) == -1; + if (options.min !== undefined && value < options.min) { + pub.addMessage(messages, options.tooSmall, value); + } + if (options.max !== undefined && value > options.max) { + pub.addMessage(messages, options.tooBig, value); + } + }, - if (!valid) { - pub.addMessage(messages, options.message, value); - } - }, + range: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } + var valid = !options.not && $.inArray(value, options.range) > -1 + || options.not && $.inArray(value, options.range) == -1; - regularExpression: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + if (!valid) { + pub.addMessage(messages, options.message, value); + } + }, - if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) { - pub.addMessage(messages, options.message, value); - } - }, + regularExpression: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - email: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) { + pub.addMessage(messages, options.message, value); + } + }, - var valid = true; + email: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - if (options.enableIDN) { - var regexp = /^(.*?)$/, - matches = regexp.exec(value); - if (matches === null) { - valid = false; - } else { - value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4]; - } - } + var valid = true; - if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) { - pub.addMessage(messages, options.message, value); - } - }, + if (options.enableIDN) { + var regexp = /^(.*?)$/, + matches = regexp.exec(value); + if (matches === null) { + valid = false; + } else { + value = matches[1] + punycode.toASCII(matches[2]) + '@' + punycode.toASCII(matches[3]) + matches[4]; + } + } - url: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) { + pub.addMessage(messages, options.message, value); + } + }, - if (options.defaultScheme && !value.match(/:\/\//)) { - value = options.defaultScheme + '://' + value; - } + url: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - var valid = true; + if (options.defaultScheme && !value.match(/:\/\//)) { + value = options.defaultScheme + '://' + value; + } - if (options.enableIDN) { - var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/, - matches = regexp.exec(value); - if (matches === null) { - valid = false; - } else { - value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3]; - } - } + var valid = true; - if (!valid || !value.match(options.pattern)) { - pub.addMessage(messages, options.message, value); - } - }, + if (options.enableIDN) { + var regexp = /^([^:]+):\/\/([^\/]+)(.*)$/, + matches = regexp.exec(value); + if (matches === null) { + valid = false; + } else { + value = matches[1] + '://' + punycode.toASCII(matches[2]) + matches[3]; + } + } - captcha: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + if (!valid || !value.match(options.pattern)) { + pub.addMessage(messages, options.message, value); + } + }, - // CAPTCHA may be updated via AJAX and the updated hash is stored in body data - var hash = $('body').data(options.hashKey); - if (hash == null) { - hash = options.hash; - } else { - hash = hash[options.caseSensitive ? 0 : 1]; - } - var v = options.caseSensitive ? value : value.toLowerCase(); - for (var i = v.length - 1, h = 0; i >= 0; --i) { - h += v.charCodeAt(i); - } - if (h != hash) { - pub.addMessage(messages, options.message, value); - } - }, + captcha: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - compare: function (value, messages, options) { - if (options.skipOnEmpty && pub.isEmpty(value)) { - return; - } + // CAPTCHA may be updated via AJAX and the updated hash is stored in body data + var hash = $('body').data(options.hashKey); + if (hash == null) { + hash = options.hash; + } else { + hash = hash[options.caseSensitive ? 0 : 1]; + } + var v = options.caseSensitive ? value : value.toLowerCase(); + for (var i = v.length - 1, h = 0; i >= 0; --i) { + h += v.charCodeAt(i); + } + if (h != hash) { + pub.addMessage(messages, options.message, value); + } + }, - var compareValue, valid = true; - if (options.compareAttribute === undefined) { - compareValue = options.compareValue; - } else { - compareValue = $('#' + options.compareAttribute).val(); - } - switch (options.operator) { - case '==': - valid = value == compareValue; - break; - case '===': - valid = value === compareValue; - break; - case '!=': - valid = value != compareValue; - break; - case '!==': - valid = value !== compareValue; - break; - case '>': - valid = value > compareValue; - break; - case '>=': - valid = value >= compareValue; - break; - case '<': - valid = value < compareValue; - break; - case '<=': - valid = value <= compareValue; - break; - default: - valid = false; - break; - } + compare: function (value, messages, options) { + if (options.skipOnEmpty && pub.isEmpty(value)) { + return; + } - if (!valid) { - pub.addMessage(messages, options.message, value); - } - } - }; - return pub; + var compareValue, valid = true; + if (options.compareAttribute === undefined) { + compareValue = options.compareValue; + } else { + compareValue = $('#' + options.compareAttribute).val(); + } + switch (options.operator) { + case '==': + valid = value == compareValue; + break; + case '===': + valid = value === compareValue; + break; + case '!=': + valid = value != compareValue; + break; + case '!==': + valid = value !== compareValue; + break; + case '>': + valid = value > compareValue; + break; + case '>=': + valid = value >= compareValue; + break; + case '<': + valid = value < compareValue; + break; + case '<=': + valid = value <= compareValue; + break; + default: + valid = false; + break; + } + + if (!valid) { + pub.addMessage(messages, options.message, value); + } + } + }; + return pub; })(jQuery);