2015-08-17 17:00:26 -07:00
/ * *
* @ file
* Handles AJAX submission and response in Views UI .
* /
( function ( $ , Drupal , drupalSettings ) {
2015-10-21 21:44:50 -07:00
'use strict' ;
2015-08-17 17:00:26 -07:00
/ * *
2015-09-04 13:20:09 -07:00
* Ajax command for highlighting elements .
2015-08-17 17:00:26 -07:00
*
* @ param { Drupal . Ajax } [ ajax ]
2015-09-04 13:20:09 -07:00
* An Ajax object .
2015-08-17 17:00:26 -07:00
* @ param { object } response
2015-09-04 13:20:09 -07:00
* The Ajax response .
2015-08-17 17:00:26 -07:00
* @ param { string } response . selector
2015-09-04 13:20:09 -07:00
* The selector in question .
2015-08-17 17:00:26 -07:00
* @ param { number } [ status ]
2015-09-04 13:20:09 -07:00
* The HTTP status code .
2015-08-17 17:00:26 -07:00
* /
Drupal . AjaxCommands . prototype . viewsHighlight = function ( ajax , response , status ) {
$ ( '.hilited' ) . removeClass ( 'hilited' ) ;
$ ( response . selector ) . addClass ( 'hilited' ) ;
} ;
2016-04-07 11:19:57 -07:00
/ * *
* Ajax command to set the form submit action in the views modal edit form .
*
* @ param { Drupal . Ajax } [ ajax ]
* An Ajax object .
* @ param { object } response
* The Ajax response . Contains . url
* @ param { string } [ status ]
* The XHR status code ?
* /
Drupal . AjaxCommands . prototype . viewsSetForm = function ( ajax , response , status ) {
var $form = $ ( '.js-views-ui-dialog form' ) ;
// Identify the button that was clicked so that .ajaxSubmit() can use it.
// We need to do this for both .click() and .mousedown() since JavaScript
// code might trigger either behavior.
var $submit _buttons = $form . find ( 'input[type=submit].js-form-submit, button.js-form-submit' ) . once ( 'views-ajax-submit' ) ;
$submit _buttons . on ( 'click mousedown' , function ( ) {
this . form . clk = this ;
} ) ;
$form . once ( 'views-ajax-submit' ) . each ( function ( ) {
var $form = $ ( this ) ;
var element _settings = {
url : response . url ,
event : 'submit' ,
base : $form . attr ( 'id' ) ,
element : this
} ;
var ajaxForm = Drupal . ajax ( element _settings ) ;
ajaxForm . $form = $form ;
} ) ;
} ;
2015-08-17 17:00:26 -07:00
/ * *
2015-09-04 13:20:09 -07:00
* Ajax command to show certain buttons in the views edit form .
2015-08-17 17:00:26 -07:00
*
* @ param { Drupal . Ajax } [ ajax ]
2015-09-04 13:20:09 -07:00
* An Ajax object .
2015-08-17 17:00:26 -07:00
* @ param { object } response
2015-09-04 13:20:09 -07:00
* The Ajax response .
2015-08-17 17:00:26 -07:00
* @ param { bool } response . changed
2015-09-04 13:20:09 -07:00
* Whether the state changed for the buttons or not .
2015-08-17 17:00:26 -07:00
* @ param { number } [ status ]
2015-09-04 13:20:09 -07:00
* The HTTP status code .
2015-08-17 17:00:26 -07:00
* /
Drupal . AjaxCommands . prototype . viewsShowButtons = function ( ajax , response , status ) {
$ ( 'div.views-edit-view div.form-actions' ) . removeClass ( 'js-hide' ) ;
if ( response . changed ) {
$ ( 'div.views-edit-view div.view-changed.messages' ) . removeClass ( 'js-hide' ) ;
}
} ;
/ * *
2015-09-04 13:20:09 -07:00
* Ajax command for triggering preview .
2015-08-17 17:00:26 -07:00
*
* @ param { Drupal . Ajax } [ ajax ]
2015-09-04 13:20:09 -07:00
* An Ajax object .
2015-08-17 17:00:26 -07:00
* @ param { object } [ response ]
2015-09-04 13:20:09 -07:00
* The Ajax response .
2015-08-17 17:00:26 -07:00
* @ param { number } [ status ]
2015-09-04 13:20:09 -07:00
* The HTTP status code .
2015-08-17 17:00:26 -07:00
* /
Drupal . AjaxCommands . prototype . viewsTriggerPreview = function ( ajax , response , status ) {
if ( $ ( 'input#edit-displays-live-preview' ) . is ( ':checked' ) ) {
$ ( '#preview-submit' ) . trigger ( 'click' ) ;
}
} ;
/ * *
2015-09-04 13:20:09 -07:00
* Ajax command to replace the title of a page .
2015-08-17 17:00:26 -07:00
*
* @ param { Drupal . Ajax } [ ajax ]
2015-09-04 13:20:09 -07:00
* An Ajax object .
2015-08-17 17:00:26 -07:00
* @ param { object } response
2015-09-04 13:20:09 -07:00
* The Ajax response .
2015-08-17 17:00:26 -07:00
* @ param { string } response . siteName
2015-09-04 13:20:09 -07:00
* The site name .
2015-08-17 17:00:26 -07:00
* @ param { string } response . title
2015-09-04 13:20:09 -07:00
* The new page title .
2015-08-17 17:00:26 -07:00
* @ param { number } [ status ]
2015-09-04 13:20:09 -07:00
* The HTTP status code .
2015-08-17 17:00:26 -07:00
* /
Drupal . AjaxCommands . prototype . viewsReplaceTitle = function ( ajax , response , status ) {
var doc = document ;
// For the <title> element, make a best-effort attempt to replace the page
// title and leave the site name alone. If the theme doesn't use the site
// name in the <title> element, this will fail.
var oldTitle = doc . title ;
// Escape the site name, in case it has special characters in it, so we can
// use it in our regex.
2015-10-21 21:44:50 -07:00
var escapedSiteName = response . siteName . replace ( /[-[\]{}()*+?.,\\^$|#\s]/g , '\\$&' ) ;
2015-08-17 17:00:26 -07:00
var re = new RegExp ( '.+ (.) ' + escapedSiteName ) ;
doc . title = oldTitle . replace ( re , response . title + ' $1 ' + response . siteName ) ;
$ ( 'h1.page-title' ) . text ( response . title ) ;
} ;
/ * *
* Get rid of irritating tabledrag messages .
*
* @ return { Array }
2015-09-04 13:20:09 -07:00
* An array of messages . Always empty array , to get rid of the messages .
2015-08-17 17:00:26 -07:00
* /
Drupal . theme . tableDragChangedWarning = function ( ) {
return [ ] ;
} ;
/ * *
* Trigger preview when the "live preview" checkbox is checked .
*
* @ type { Drupal ~ behavior }
2015-09-04 13:20:09 -07:00
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches behavior to trigger live preview if the live preview option is
* checked .
2015-08-17 17:00:26 -07:00
* /
Drupal . behaviors . livePreview = {
attach : function ( context ) {
$ ( 'input#edit-displays-live-preview' , context ) . once ( 'views-ajax' ) . on ( 'click' , function ( ) {
if ( $ ( this ) . is ( ':checked' ) ) {
$ ( '#preview-submit' ) . trigger ( 'click' ) ;
}
} ) ;
}
} ;
/ * *
* Sync preview display .
*
* @ type { Drupal ~ behavior }
2015-09-04 13:20:09 -07:00
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches behavior to sync the preview display when needed .
2015-08-17 17:00:26 -07:00
* /
Drupal . behaviors . syncPreviewDisplay = {
attach : function ( context ) {
2015-10-21 21:44:50 -07:00
$ ( '#views-tabset a' ) . once ( 'views-ajax' ) . on ( 'click' , function ( ) {
2015-08-17 17:00:26 -07:00
var href = $ ( this ) . attr ( 'href' ) ;
// Cut of #views-tabset.
var display _id = href . substr ( 11 ) ;
// Set the form element.
2015-10-21 21:44:50 -07:00
$ ( '#views-live-preview #preview-display-id' ) . val ( display _id ) ;
2015-08-17 17:00:26 -07:00
} ) ;
}
} ;
/ * *
2015-09-04 13:20:09 -07:00
* Ajax behaviors for the views _ui module .
2015-08-17 17:00:26 -07:00
*
* @ type { Drupal ~ behavior }
2015-09-04 13:20:09 -07:00
*
* @ prop { Drupal ~ behaviorAttach } attach
* Attaches ajax behaviors to the elements with the classes in question .
2015-08-17 17:00:26 -07:00
* /
Drupal . behaviors . viewsAjax = {
collapseReplaced : false ,
attach : function ( context , settings ) {
var base _element _settings = {
2015-09-04 13:20:09 -07:00
event : 'click' ,
progress : { type : 'fullscreen' }
2015-08-17 17:00:26 -07:00
} ;
// Bind AJAX behaviors to all items showing the class.
$ ( 'a.views-ajax-link' , context ) . once ( 'views-ajax' ) . each ( function ( ) {
var element _settings = base _element _settings ;
element _settings . base = base ;
element _settings . element = this ;
// Set the URL to go to the anchor.
if ( $ ( this ) . attr ( 'href' ) ) {
element _settings . url = $ ( this ) . attr ( 'href' ) ;
}
var base = $ ( this ) . attr ( 'id' ) ;
Drupal . ajax ( element _settings ) ;
} ) ;
$ ( 'div#views-live-preview a' )
. once ( 'views-ajax' ) . each ( function ( ) {
// We don't bind to links without a URL.
if ( ! $ ( this ) . attr ( 'href' ) ) {
return true ;
}
var element _settings = base _element _settings ;
// Set the URL to go to the anchor.
element _settings . url = $ ( this ) . attr ( 'href' ) ;
if ( Drupal . Views . getPath ( element _settings . url ) . substring ( 0 , 21 ) !== 'admin/structure/views' ) {
return true ;
}
element _settings . wrapper = 'views-preview-wrapper' ;
element _settings . method = 'replaceWith' ;
element _settings . base = base ;
element _settings . element = this ;
var base = $ ( this ) . attr ( 'id' ) ;
Drupal . ajax ( element _settings ) ;
} ) ;
// Within a live preview, make exposed widget form buttons re-trigger the
// Preview button.
// @todo Revisit this after fixing Views UI to display a Preview outside
// of the main Edit form.
$ ( 'div#views-live-preview input[type=submit]' )
. once ( 'views-ajax' ) . each ( function ( event ) {
$ ( this ) . on ( 'click' , function ( ) {
this . form . clk = this ;
return true ;
} ) ;
var element _settings = base _element _settings ;
// Set the URL to go to the anchor.
element _settings . url = $ ( this . form ) . attr ( 'action' ) ;
if ( Drupal . Views . getPath ( element _settings . url ) . substring ( 0 , 21 ) !== 'admin/structure/views' ) {
return true ;
}
element _settings . wrapper = 'views-preview-wrapper' ;
element _settings . method = 'replaceWith' ;
element _settings . event = 'click' ;
element _settings . base = base ;
element _settings . element = this ;
var base = $ ( this ) . attr ( 'id' ) ;
Drupal . ajax ( element _settings ) ;
} ) ;
}
} ;
} ) ( jQuery , Drupal , drupalSettings ) ;