123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- window.yii = (function ($) {
- var pub = {
-
- reloadableScripts: [],
-
- clickableSelector: 'a, button, input[type="submit"], input[type="button"], input[type="reset"], ' +
- 'input[type="image"]',
-
- changeableSelector: 'select, input, textarea',
-
- getCsrfParam: function () {
- return $('meta[name=csrf-param]').attr('content');
- },
-
- getCsrfToken: function () {
- return $('meta[name=csrf-token]').attr('content');
- },
-
- setCsrfToken: function (name, value) {
- $('meta[name=csrf-param]').attr('content', name);
- $('meta[name=csrf-token]').attr('content', value);
- },
-
- refreshCsrfToken: function () {
- var token = pub.getCsrfToken();
- if (token) {
- $('form input[name="' + pub.getCsrfParam() + '"]').val(token);
- }
- },
-
- confirm: function (message, ok, cancel) {
- if (window.confirm(message)) {
- !ok || ok();
- } else {
- !cancel || cancel();
- }
- },
-
- handleAction: function ($e, event) {
- var $form = $e.attr('data-form') ? $('#' + $e.attr('data-form')) : $e.closest('form'),
- method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'),
- action = $e.attr('href'),
- isValidAction = action && action !== '#',
- params = $e.data('params'),
- areValidParams = params && $.isPlainObject(params),
- pjax = $e.data('pjax'),
- usePjax = pjax !== undefined && pjax !== 0 && $.support.pjax,
- pjaxContainer,
- pjaxOptions = {},
- conflictParams = ['submit', 'reset', 'elements', 'length', 'name', 'acceptCharset',
- 'action', 'enctype', 'method', 'target'];
-
-
- $.each(conflictParams, function (index, param) {
- if (areValidParams && params.hasOwnProperty(param)) {
- console.error("Parameter name '" + param + "' conflicts with a same named form property. " +
- "Please use another name.");
- }
- });
- if (usePjax) {
- pjaxContainer = $e.data('pjax-container');
- if (pjaxContainer === undefined || !pjaxContainer.length) {
- pjaxContainer = $e.closest('[data-pjax-container]').attr('id')
- ? ('#' + $e.closest('[data-pjax-container]').attr('id'))
- : '';
- }
- if (!pjaxContainer.length) {
- pjaxContainer = 'body';
- }
- pjaxOptions = {
- container: pjaxContainer,
- push: !!$e.data('pjax-push-state'),
- replace: !!$e.data('pjax-replace-state'),
- scrollTo: $e.data('pjax-scrollto'),
- pushRedirect: $e.data('pjax-push-redirect'),
- replaceRedirect: $e.data('pjax-replace-redirect'),
- skipOuterContainers: $e.data('pjax-skip-outer-containers'),
- timeout: $e.data('pjax-timeout'),
- originalEvent: event,
- originalTarget: $e
- };
- }
- if (method === undefined) {
- if (isValidAction) {
- usePjax ? $.pjax.click(event, pjaxOptions) : window.location.assign(action);
- } else if ($e.is(':submit') && $form.length) {
- if (usePjax) {
- $form.on('submit', function (e) {
- $.pjax.submit(e, pjaxOptions);
- });
- }
- $form.trigger('submit');
- }
- return;
- }
- var oldMethod,
- oldAction,
- newForm = !$form.length;
- if (!newForm) {
- oldMethod = $form.attr('method');
- $form.attr('method', method);
- if (isValidAction) {
- oldAction = $form.attr('action');
- $form.attr('action', action);
- }
- } else {
- if (!isValidAction) {
- action = pub.getCurrentUrl();
- }
- $form = $('<form/>', {method: method, action: action});
- var target = $e.attr('target');
- if (target) {
- $form.attr('target', target);
- }
- if (!/(get|post)/i.test(method)) {
- $form.append($('<input/>', {name: '_method', value: method, type: 'hidden'}));
- method = 'post';
- $form.attr('method', method);
- }
- if (/post/i.test(method)) {
- var csrfParam = pub.getCsrfParam();
- if (csrfParam) {
- $form.append($('<input/>', {name: csrfParam, value: pub.getCsrfToken(), type: 'hidden'}));
- }
- }
- $form.hide().appendTo('body');
- }
- var activeFormData = $form.data('yiiActiveForm');
- if (activeFormData) {
-
- activeFormData.submitObject = $e;
- }
- if (areValidParams) {
- $.each(params, function (name, value) {
- $form.append($('<input/>').attr({name: name, value: value, type: 'hidden'}));
- });
- }
- if (usePjax) {
- $form.on('submit', function (e) {
- $.pjax.submit(e, pjaxOptions);
- });
- }
- $form.trigger('submit');
- $.when($form.data('yiiSubmitFinalizePromise')).done(function () {
- if (newForm) {
- $form.remove();
- return;
- }
- if (oldAction !== undefined) {
- $form.attr('action', oldAction);
- }
- $form.attr('method', oldMethod);
- if (areValidParams) {
- $.each(params, function (name) {
- $('input[name="' + name + '"]', $form).remove();
- });
- }
- });
- },
- getQueryParams: function (url) {
- var pos = url.indexOf('?');
- if (pos < 0) {
- return {};
- }
- var pairs = $.grep(url.substring(pos + 1).split('#')[0].split('&'), function (value) {
- return value !== '';
- });
- var params = {};
- for (var i = 0, len = pairs.length; i < len; i++) {
- var pair = pairs[i].split('=');
- var name = decodeURIComponent(pair[0].replace(/\+/g, '%20'));
- var value = decodeURIComponent(pair[1].replace(/\+/g, '%20'));
- if (!name.length) {
- continue;
- }
- if (params[name] === undefined) {
- params[name] = value || '';
- } else {
- if (!$.isArray(params[name])) {
- params[name] = [params[name]];
- }
- params[name].push(value || '');
- }
- }
- return params;
- },
- initModule: function (module) {
- if (module.isActive !== undefined && !module.isActive) {
- return;
- }
- if ($.isFunction(module.init)) {
- module.init();
- }
- $.each(module, function () {
- if ($.isPlainObject(this)) {
- pub.initModule(this);
- }
- });
- },
- init: function () {
- initCsrfHandler();
- initRedirectHandler();
- initAssetFilters();
- initDataMethods();
- },
-
- getBaseCurrentUrl: function () {
- return window.location.protocol + '//' + window.location.host;
- },
-
- getCurrentUrl: function () {
- return window.location.href;
- }
- };
- function initCsrfHandler() {
-
- $.ajaxPrefilter(function (options, originalOptions, xhr) {
- if (!options.crossDomain && pub.getCsrfParam()) {
- xhr.setRequestHeader('X-CSRF-Token', pub.getCsrfToken());
- }
- });
- pub.refreshCsrfToken();
- }
- function initRedirectHandler() {
-
- $(document).ajaxComplete(function (event, xhr) {
- var url = xhr && xhr.getResponseHeader('X-Redirect');
- if (url) {
- window.location.assign(url);
- }
- });
- }
- function initAssetFilters() {
-
- var loadedScripts = {};
- $('script[src]').each(function () {
- var url = getAbsoluteUrl(this.src);
- loadedScripts[url] = true;
- });
- $.ajaxPrefilter('script', function (options, originalOptions, xhr) {
- if (options.dataType == 'jsonp') {
- return;
- }
- var url = getAbsoluteUrl(options.url),
- forbiddenRepeatedLoad = loadedScripts[url] === true && !isReloadableAsset(url),
- cleanupRunning = loadedScripts[url] !== undefined && loadedScripts[url]['xhrDone'] === true;
- if (forbiddenRepeatedLoad || cleanupRunning) {
- xhr.abort();
- return;
- }
- if (loadedScripts[url] === undefined || loadedScripts[url] === true) {
- loadedScripts[url] = {
- xhrList: [],
- xhrDone: false
- };
- }
- xhr.done(function (data, textStatus, jqXHR) {
-
- if (loadedScripts[jqXHR.yiiUrl]['xhrDone'] === true) {
- return;
- }
- loadedScripts[jqXHR.yiiUrl]['xhrDone'] = true;
- for (var i = 0, len = loadedScripts[jqXHR.yiiUrl]['xhrList'].length; i < len; i++) {
- var singleXhr = loadedScripts[jqXHR.yiiUrl]['xhrList'][i];
- if (singleXhr && singleXhr.readyState !== XMLHttpRequest.DONE) {
- singleXhr.abort();
- }
- }
- loadedScripts[jqXHR.yiiUrl] = true;
- }).fail(function (jqXHR, textStatus) {
- if (textStatus === 'abort') {
- return;
- }
- delete loadedScripts[jqXHR.yiiUrl]['xhrList'][jqXHR.yiiIndex];
- var allFailed = true;
- for (var i = 0, len = loadedScripts[jqXHR.yiiUrl]['xhrList'].length; i < len; i++) {
- if (loadedScripts[jqXHR.yiiUrl]['xhrList'][i]) {
- allFailed = false;
- }
- }
- if (allFailed) {
- delete loadedScripts[jqXHR.yiiUrl];
- }
- });
-
- xhr.yiiIndex = loadedScripts[url]['xhrList'].length;
- xhr.yiiUrl = url;
- loadedScripts[url]['xhrList'][xhr.yiiIndex] = xhr;
- });
- $(document).ajaxComplete(function () {
- var styleSheets = [];
- $('link[rel=stylesheet]').each(function () {
- var url = getAbsoluteUrl(this.href);
- if (isReloadableAsset(url)) {
- return;
- }
- $.inArray(url, styleSheets) === -1 ? styleSheets.push(url) : $(this).remove();
- });
- });
- }
- function initDataMethods() {
- var handler = function (event) {
- var $this = $(this),
- method = $this.data('method'),
- message = $this.data('confirm'),
- form = $this.data('form');
- if (method === undefined && message === undefined && form === undefined) {
- return true;
- }
- if (message !== undefined && message !== false && message !== '') {
- $.proxy(pub.confirm, this)(message, function () {
- pub.handleAction($this, event);
- });
- } else {
- pub.handleAction($this, event);
- }
- event.stopImmediatePropagation();
- return false;
- };
-
- $(document).on('click.yii', pub.clickableSelector, handler)
- .on('change.yii', pub.changeableSelector, handler);
- }
- function isReloadableAsset(url) {
- for (var i = 0; i < pub.reloadableScripts.length; i++) {
- var rule = getAbsoluteUrl(pub.reloadableScripts[i]);
- var match = new RegExp("^" + escapeRegExp(rule).split('\\*').join('.+') + "$").test(url);
- if (match === true) {
- return true;
- }
- }
- return false;
- }
-
- function escapeRegExp(str) {
- return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
- }
-
- function getAbsoluteUrl(url) {
- return url.charAt(0) === '/' ? pub.getBaseCurrentUrl() + url : url;
- }
- return pub;
- })(window.jQuery);
- window.jQuery(function () {
- window.yii.initModule(window.yii);
- });
|