2012-12-21 17:03:57 +00:00
/ * *
* @ file
2014-04-16 21:42:14 +00:00
* Provides utility functions for Quick Edit .
2012-12-21 17:03:57 +00:00
* /
2013-12-04 05:18:34 +00:00
2013-11-13 15:06:05 +00:00
( function ( $ , Drupal ) {
2012-12-21 17:03:57 +00:00
2014-01-27 21:41:32 +00:00
"use strict" ;
2012-12-21 17:03:57 +00:00
2014-04-16 21:42:14 +00:00
Drupal . quickedit . util = Drupal . quickedit . util || { } ;
2012-12-21 17:03:57 +00:00
2014-04-16 21:42:14 +00:00
Drupal . quickedit . util . constants = { } ;
Drupal . quickedit . util . constants . transitionEnd = "transitionEnd.quickedit webkitTransitionEnd.quickedit transitionend.quickedit msTransitionEnd.quickedit oTransitionEnd.quickedit" ;
2013-12-04 05:18:34 +00:00
2012-12-21 17:03:57 +00:00
/ * *
2014-01-27 21:41:32 +00:00
* Converts a field id into a formatted url path .
2012-12-21 17:03:57 +00:00
*
2014-01-27 21:41:32 +00:00
* @ param String id
* The id of an editable field . For example , 'node/1/body/und/full' .
* @ param String urlFormat
* The Controller route for field processing . For example ,
2014-04-16 21:42:14 +00:00
* '/quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode' .
2012-12-21 17:03:57 +00:00
* /
2014-04-16 21:42:14 +00:00
Drupal . quickedit . util . buildUrl = function ( id , urlFormat ) {
2014-01-27 21:41:32 +00:00
var parts = id . split ( '/' ) ;
return Drupal . formatString ( decodeURIComponent ( urlFormat ) , {
'!entity_type' : parts [ 0 ] ,
'!id' : parts [ 1 ] ,
'!field_name' : parts [ 2 ] ,
'!langcode' : parts [ 3 ] ,
'!view_mode' : parts [ 4 ]
2012-12-21 17:03:57 +00:00
} ) ;
2014-01-27 21:41:32 +00:00
} ;
2012-12-21 17:03:57 +00:00
/ * *
2014-01-27 21:41:32 +00:00
* Shows a network error modal dialog .
2012-12-21 17:03:57 +00:00
*
2014-01-27 21:41:32 +00:00
* @ param String title
* The title to use in the modal dialog .
* @ param String message
* The message to use in the modal dialog .
2012-12-21 17:03:57 +00:00
* /
2014-04-16 21:42:14 +00:00
Drupal . quickedit . util . networkErrorModal = function ( title , message ) {
2014-01-27 21:41:32 +00:00
var $message = $ ( '<div>' + message + '</div>' ) ;
var networkErrorModal = Drupal . dialog ( $message . get ( 0 ) , {
title : title ,
2014-04-16 21:42:14 +00:00
dialogClass : 'quickedit-network-error' ,
2014-01-27 21:41:32 +00:00
buttons : [
{
text : Drupal . t ( 'OK' ) ,
click : function ( ) {
networkErrorModal . close ( ) ;
2014-06-08 23:30:42 +00:00
} ,
primary : true
2013-01-03 00:03:22 +00:00
}
2014-01-27 21:41:32 +00:00
] ,
create : function ( ) {
$ ( this ) . parent ( ) . find ( '.ui-dialog-titlebar-close' ) . remove ( ) ;
} ,
close : function ( event ) {
// Automatically destroy the DOM element that was used for the dialog.
$ ( event . target ) . remove ( ) ;
2013-01-03 00:03:22 +00:00
}
2014-01-27 21:41:32 +00:00
} ) ;
networkErrorModal . showModal ( ) ;
} ;
2014-04-16 21:42:14 +00:00
Drupal . quickedit . util . form = {
2014-01-27 21:41:32 +00:00
/ * *
* Loads a form , calls a callback to insert .
*
* Leverages Drupal . ajax ' ability to have scoped ( per - instance ) command
* implementations to be able to call a callback .
*
* @ param Object options
* An object with the following keys :
* - jQuery $el : ( required ) DOM element necessary for Drupal . ajax to
* perform AJAX commands .
* - String fieldID : ( required ) the field ID that uniquely identifies the
* field for which this form will be loaded .
* - Boolean nocssjs : ( required ) boolean indicating whether no CSS and JS
* should be returned ( necessary when the form is invisible to the user ) .
* - Boolean reset : ( required ) boolean indicating whether the data stored
* for this field ' s entity in TempStore should be used or reset .
* @ param Function callback
* A callback function that will receive the form to be inserted , as well as
* the ajax object , necessary if the callback wants to perform other AJAX
* commands .
* /
load : function ( options , callback ) {
var $el = options . $el ;
var fieldID = options . fieldID ;
// Create a Drupal.ajax instance to load the form.
var formLoaderAjax = new Drupal . ajax ( fieldID , $el , {
2014-04-16 21:42:14 +00:00
url : Drupal . quickedit . util . buildUrl ( fieldID , Drupal . url ( 'quickedit/form/!entity_type/!id/!field_name/!langcode/!view_mode' ) ) ,
event : 'quickedit-internal.quickedit' ,
2014-01-27 21:41:32 +00:00
submit : {
nocssjs : options . nocssjs ,
reset : options . reset
} ,
progress : { type : null } , // No progress indicator.
error : function ( xhr , url ) {
2014-04-16 21:42:14 +00:00
$el . off ( 'quickedit-internal.quickedit' ) ;
2014-01-27 21:41:32 +00:00
// Show a modal to inform the user of the network error.
2014-04-16 21:42:14 +00:00
var fieldLabel = Drupal . quickedit . metadata . get ( fieldID , 'label' ) ;
2014-01-27 21:41:32 +00:00
var message = Drupal . t ( 'Could not load the form for <q>@field-label</q>, either due to a website problem or a network connection problem.<br>Please try again.' , { '@field-label' : fieldLabel } ) ;
2014-04-16 21:42:14 +00:00
Drupal . quickedit . util . networkErrorModal ( Drupal . t ( 'Sorry!' ) , message ) ;
2014-01-27 21:41:32 +00:00
// Change the state back to "candidate", to allow the user to start
// in-place editing of the field again.
2014-04-16 21:42:14 +00:00
var fieldModel = Drupal . quickedit . app . model . get ( 'activeField' ) ;
2014-01-27 21:41:32 +00:00
fieldModel . set ( 'state' , 'candidate' ) ;
}
} ) ;
2014-04-16 21:42:14 +00:00
// Implement a scoped quickeditFieldForm AJAX command: calls the callback.
formLoaderAjax . commands . quickeditFieldForm = function ( ajax , response , status ) {
2014-01-27 21:41:32 +00:00
callback ( response . data , ajax ) ;
2014-04-16 21:42:14 +00:00
$el . off ( 'quickedit-internal.quickedit' ) ;
2014-01-27 21:41:32 +00:00
formLoaderAjax = null ;
} ;
2014-04-16 21:42:14 +00:00
// This will ensure our scoped quickeditFieldForm AJAX command gets called.
$el . trigger ( 'quickedit-internal.quickedit' ) ;
2014-01-27 21:41:32 +00:00
} ,
2012-12-21 17:03:57 +00:00
2014-01-27 21:41:32 +00:00
/ * *
* Creates a Drupal . ajax instance that is used to save a form .
*
* @ param Object options
* An object with the following keys :
* - nocssjs : ( required ) boolean indicating whether no CSS and JS should be
* returned ( necessary when the form is invisible to the user ) .
* - other _view _modes : ( required ) array containing view mode IDs ( of other
* instances of this field on the page ) .
* @ return Drupal . ajax
* A Drupal . ajax instance .
* /
ajaxifySaving : function ( options , $submit ) {
// Re-wire the form to handle submit.
var settings = {
url : $submit . closest ( 'form' ) . attr ( 'action' ) ,
setClick : true ,
2014-04-16 21:42:14 +00:00
event : 'click.quickedit' ,
2014-01-27 21:41:32 +00:00
progress : { type : null } ,
submit : {
nocssjs : options . nocssjs ,
other _view _modes : options . other _view _modes
} ,
// Reimplement the success handler to ensure Drupal.attachBehaviors() does
// not get called on the form.
success : function ( response , status ) {
for ( var i in response ) {
if ( response . hasOwnProperty ( i ) && response [ i ] . command && this . commands [ response [ i ] . command ] ) {
this . commands [ response [ i ] . command ] ( this , response [ i ] , status ) ;
}
}
}
} ;
2012-12-21 17:03:57 +00:00
2014-01-27 21:41:32 +00:00
return new Drupal . ajax ( $submit . attr ( 'id' ) , $submit [ 0 ] , settings ) ;
} ,
/ * *
* Cleans up the Drupal . ajax instance that is used to save the form .
*
* @ param Drupal . ajax ajax
2014-04-16 21:42:14 +00:00
* A Drupal . ajax instance that was returned by
* Drupal . quickedit . form . ajaxifySaving ( ) .
2014-01-27 21:41:32 +00:00
* /
unajaxifySaving : function ( ajax ) {
2014-04-16 21:42:14 +00:00
$ ( ajax . element ) . off ( 'click.quickedit' ) ;
2014-01-27 21:41:32 +00:00
}
2013-12-04 05:18:34 +00:00
2014-01-27 21:41:32 +00:00
} ;
2012-12-21 17:03:57 +00:00
2013-11-13 15:06:05 +00:00
} ) ( jQuery , Drupal ) ;