2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.6 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2015 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
26 * Copyright (C) 2009 Bevan Rudge
27 * Licensed to CiviCRM under the Academic Free License version 3.0.
29 * @file Defines the jQuery.dashboard() plugin.
31 * Uses jQuery 1.3, jQuery UI 1.6 and several jQuery UI extensions, most of all Sortable
32 * http://visualjquery.com/
33 * http://docs.jquery.com/UI/Sortable
34 * http://ui.jquery.com/download
41 (function($) { // Create closure.
42 // Constructor for dashboard object.
43 $.fn
.dashboard = function(options
) {
44 // Public properties of dashboard.
46 dashboard
.element
= this.empty();
47 dashboard
.ready
= false;
48 dashboard
.columns
= Array();
49 dashboard
.widgets
= Array();
50 // End of public properties of dashboard.
53 * Public methods of dashboard.
56 // Saves the order of widgets for all columns including the widget.minimized status to options.ajaxCallbacks.saveColumns.
57 dashboard
.saveColumns = function() {
58 // Update the display status of the empty placeholders.
59 for (var c
in dashboard
.columns
) {
60 var col
= dashboard
.columns
[c
];
61 if ( typeof col
== 'object' ) {
62 // Are there any visible children of the column (excluding the empty placeholder)?
63 if (col
.element
.children(':visible').not(col
.emptyPlaceholder
).length
> 0) {
64 col
.emptyPlaceholder
.hide();
67 col
.emptyPlaceholder
.show();
72 // Don't save any changes to the server unless the dashboard has finished initiating.
73 if (!dashboard
.ready
) {
77 // Build a list of params to post to the server.
81 for (var c
in dashboard
.columns
) {
83 // IDs of the sortable elements in this column.
84 if( typeof dashboard
.columns
[c
] == 'object' ) var ids
= dashboard
.columns
[c
].element
.sortable('toArray');
88 // Chop 'widget-' off of the front so that we have the real widget id.
89 if( typeof ids
[w
] == 'string' ) var id
= ids
[w
].substring('widget-'.length
);
91 // Add one flat property to the params object that will look like an array element to the PHP server.
92 // Unfortunately jQuery doesn't do this for us.
93 if ( typeof dashboard
.widgets
[id
] == 'object' ) params
['columns[' + c
+ '][' + id
+ ']'] = (dashboard
.widgets
[id
].minimized
? '1' : '0');
97 // The ajaxCallback settings overwrite any duplicate properties.
98 $.extend(params
, opts
.ajaxCallbacks
.saveColumns
.data
);
99 $.post(opts
.ajaxCallbacks
.saveColumns
.url
, params
, function(response
, status
) {
100 invokeCallback(opts
.callbacks
.saveColumns
, dashboard
);
104 // Puts the dashboard into full screen mode, saving element for when the user exits full-screen mode.
105 // Does not add element to the DOM – this is the caller's responsibility.
106 // Does show and hide element though.
107 dashboard
.enterFullscreen = function(element
) {
109 for (var c
in dashboard
.columns
) {
110 if ( typeof dashboard
.columns
[c
] == 'object' ) dashboard
.columns
[c
].element
.hide();
113 if (!dashboard
.fullscreen
) {
115 var markup
= '<a id="full-screen-header" class="full-screen-close-icon">' + opts
.fullscreenHeaderInner
+ '</a>';
116 dashboard
.fullscreen
= {
117 headerElement
: $(markup
).prependTo(dashboard
.element
).click(dashboard
.exitFullscreen
).hide()
121 dashboard
.fullscreen
.headerElement
.slideDown();
122 dashboard
.fullscreen
.currentElement
= element
.show();
123 dashboard
.fullscreen
.displayed
= true;
124 invokeCallback(opts
.callbacks
.enterFullscreen
, dashboard
, dashboard
.fullscreen
.currentElement
);
127 // Takes the dashboard out of full screen mode, hiding the active fullscreen element.
128 dashboard
.exitFullscreen = function() {
129 if (!dashboard
.fullscreen
.displayed
) {
133 dashboard
.fullscreen
.headerElement
.slideUp();
134 dashboard
.fullscreen
.currentElement
.hide();
135 dashboard
.fullscreen
.displayed
= false;
138 for (var c
in dashboard
.columns
) {
139 if ( typeof dashboard
.columns
[c
] == 'object' ) dashboard
.columns
[c
].element
.show();
142 invokeCallback(opts
.callbacks
.exitFullscreen
, dashboard
, dashboard
.fullscreen
.currentElement
);
144 // End of public methods of dashboard.
147 * Private properties of dashboard.
150 // Used to determine whether there are any incomplete ajax requests pending initialization of the dashboard.
151 var asynchronousRequestCounter
= 0;
153 // Used to determine whether two resort events are resulting from the same UI event.
154 var currentReSortEvent
= null;
156 // Merge in the caller's options with the defaults.
157 var opts
= $.extend({}, $.fn
.dashboard
.defaults
, options
);
159 // Execution 'forks' here and restarts in init(). Tell the user we're busy with a throbber.
160 var throbber
= $(opts
.throbberMarkup
).appendTo(dashboard
.element
);
161 $.getJSON(opts
.ajaxCallbacks
.getWidgetsByColumn
.url
, opts
.ajaxCallbacks
.getWidgetsByColumn
.data
, init
);
162 asynchronousRequestCounter
++;
164 // End of constructor and private properties for dashboard object.
167 * Private methods of dashboard.
170 // Ajax callback for getWidgetsByColumn.
171 function init(widgets
, status
) {
172 asynchronousRequestCounter
--;
174 var markup
= '<li class="empty-placeholder">' + opts
.emptyPlaceholderInner
+ '</li>';
176 // Build the dashboard in the DOM. For each column...
177 // (Don't iterate on widgets since this will break badly if the dataset has empty columns.)
178 var emptyDashboard
= true;
179 for (var c
= 0; c
< opts
.columns
; c
++) {
180 // Save the column to both the public scope for external accessibility and the local scope for readability.
181 var col
= dashboard
.columns
[c
] = {
182 initialWidgets
: Array(),
183 element
: $('<ul id="column-' + c
+ '" class="column column-' + c
+ '"></ul>').appendTo(dashboard
.element
)
186 // Add the empty placeholder now, hide it and save it.
187 col
.emptyPlaceholder
= $(markup
).appendTo(col
.element
).hide();
189 // For each widget in this column.
190 for (var id
in widgets
[c
]) {
191 var widgetID
= id
.split('-');
192 // Build a new widget object and save it to various publicly accessible places.
193 col
.initialWidgets
[id
] = dashboard
.widgets
[widgetID
[1]] = widget({
195 element
: $('<li class="widget"></li>').appendTo(col
.element
),
197 minimized
: ( widgets
[c
][widgetID
[1]] > 0 ? true : false )
200 //set empty Dashboard to false
201 emptyDashboard
= false;
205 if ( emptyDashboard
) {
206 emptyDashboardCondition( );
209 invokeCallback(opts
.callbacks
.init
, dashboard
);
212 // function that is called when dashboard is empty
213 function emptyDashboardCondition( ) {
214 cj(".show-refresh").hide( );
215 cj("#empty-message").show( );
218 // Contructors for each widget call this when initialization has finished so that dashboard can complete it's intitialization.
219 function completeInit() {
220 // Don't do anything if any widgets are waiting for ajax requests to complete in order to finish initialization.
221 if (asynchronousRequestCounter
> 0) {
225 // Make widgets sortable across columns.
226 dashboard
.sortableElement
= $('.column').sortable({
227 connectWith
: ['.column'],
229 // The class of the element by which widgets are draggable.
230 handle
: '.widget-header',
232 // The class of placeholder elements (the 'ghost' widget showing where the dragged item would land if released now.)
233 placeholder
: 'placeholder',
234 activate: function(event
, ui
) {
235 var h
= cj(ui
.item
).height();
236 $('.placeholder').css('height', h
+'px'); },
240 // Maks sure that only widgets are sortable, and not empty placeholders.
243 forcePlaceholderSize
: true,
245 // Callback functions.
247 start
: hideEmptyPlaceholders
250 // Update empty placeholders.
251 dashboard
.saveColumns();
252 dashboard
.ready
= true;
253 invokeCallback(opts
.callbacks
.ready
, dashboard
);
256 // Callback for when any list has changed (and the user has finished resorting).
257 function resorted(e
, ui
) {
258 // Only do anything if we haven't already handled resorts based on changes from this UI DOM event.
259 // (resorted() gets invoked once for each list when an item is moved from one to another.)
260 if (!currentReSortEvent
|| e
.originalEvent
!= currentReSortEvent
) {
261 currentReSortEvent
= e
.originalEvent
;
262 dashboard
.saveColumns();
266 // Callback for when a user starts resorting a list. Hides all the empty placeholders.
267 function hideEmptyPlaceholders(e
, ui
) {
268 for (var c
in dashboard
.columns
) {
269 if( typeof dashboard
.columns
[c
] == 'object ' ) dashboard
.columns
[c
].emptyPlaceholder
.hide();
273 // @todo use an event library to register, bind to and invoke events.
274 // @param callback is a function.
275 // @param theThis is the context given to that function when it executes. It becomes 'this' inside of that function.
276 function invokeCallback(callback
, theThis
, parameterOne
) {
278 callback
.call(theThis
, parameterOne
);
284 * Private sub-class of dashboard
287 function widget(widget
) {
288 // Merge default options with the options defined for this widget.
289 widget
= $.extend({}, $.fn
.dashboard
.widget
.defaults
, widget
);
292 * Public methods of widget.
295 // Toggles the minimize() & maximize() methods.
296 widget
.toggleMinimize = function() {
297 if (widget
.minimized
) {
304 widget
.hideSettings();
305 dashboard
.saveColumns();
307 widget
.minimize = function() {
308 $('.widget-content', widget
.element
).slideUp(opts
.animationSpeed
);
309 $(widget
.controls
.minimize
.element
).addClass( 'maximize-icon' );
310 $(widget
.controls
.minimize
.element
).removeClass( 'minimize-icon' );
311 widget
.minimized
= true;
313 widget
.maximize = function() {
314 $('.widget-content', widget
.element
).slideDown(opts
.animationSpeed
);
315 $(widget
.controls
.minimize
.element
).removeClass( 'maximize-icon' );
316 $(widget
.controls
.minimize
.element
).addClass( 'minimize-icon' );
317 widget
.minimized
= false;
320 // Toggles whether the widget is in settings-display mode or not.
321 widget
.toggleSettings = function() {
322 if (widget
.settings
.displayed
) {
323 // Widgets always exit settings into maximized state.
325 widget
.hideSettings();
326 invokeCallback(opts
.widgetCallbacks
.hideSettings
, widget
);
330 widget
.showSettings();
331 invokeCallback(opts
.widgetCallbacks
.showSettings
, widget
);
334 widget
.showSettings = function() {
335 if (widget
.settings
.element
) {
336 widget
.settings
.element
.show();
338 // Settings are loaded via AJAX. Only execute the script if the settings have been loaded.
339 if (widget
.settings
.ready
) {
340 getJavascript(widget
.settings
.script
);
344 // Settings have not been initialized. Do so now.
347 widget
.settings
.displayed
= true;
349 widget
.hideSettings = function() {
350 if (widget
.settings
.element
) {
351 widget
.settings
.element
.hide();
353 widget
.settings
.displayed
= false;
355 widget
.saveSettings = function() {
356 // Build list of parameters to POST to server.
358 // serializeArray() returns an array of objects. Process it.
359 var fields
= widget
.settings
.element
.serializeArray();
360 for (var i
in fields
) {
361 var field
= fields
[i
];
362 // Put the values into flat object properties that PHP will parse into an array server-side.
363 // (Unfortunately jQuery doesn't do this)
364 params
['settings[' + field
.name
+ ']'] = field
.value
;
367 // Things get messy here.
368 // @todo Refactor to use currentState and targetedState properties to determine what needs
369 // to be done to get to any desired state on any UI or AJAX event – since these don't always
371 // E.g. When a user starts a new UI event before the Ajax event handler from a previous
372 // UI event gets invoked.
374 // Hide the settings first of all.
375 widget
.toggleSettings();
376 // Save the real settings element so that we can restore the reference later.
377 var settingsElement
= widget
.settings
.element
;
378 // Empty the settings form.
379 widget
.settings
.innerElement
.empty();
381 // So that showSettings() and hideSettings() can do SOMETHING, without showing the empty settings form.
382 widget
.settings
.element
= widget
.throbber
.hide();
383 widget
.settings
.ready
= false;
385 // Save the settings to the server.
386 $.extend(params
, opts
.ajaxCallbacks
.widgetSettings
.data
, { id
: widget
.id
});
387 $.post(opts
.ajaxCallbacks
.widgetSettings
.url
, params
, function(response
, status
) {
388 // Merge the response into widget.settings.
389 $.extend(widget
.settings
, response
);
390 // Restore the reference to the real settings element.
391 widget
.settings
.element
= settingsElement
;
392 // Make sure the settings form is empty and add the updated settings form.
393 widget
.settings
.innerElement
.empty().append(widget
.settings
.markup
);
394 widget
.settings
.ready
= true;
396 // Did the user already jump back into settings-display mode before we could finish reloading the settings form?
397 if (widget
.settings
.displayed
) {
398 // Ooops! We had better take care of hiding the throbber and showing the settings form then.
399 widget
.throbber
.hide();
400 widget
.showSettings();
401 invokeCallback(opts
.widgetCallbacks
.saveSettings
, dashboard
);
405 // Don't let form submittal bubble up.
409 widget
.enterFullscreen = function() {
410 // Make sure the widget actually supports full screen mode.
411 if (!widget
.fullscreenUrl
) {
414 CRM
.loadPage(widget
.fullscreenUrl
);
417 // Exit fullscreen mode.
418 widget
.exitFullscreen = function() {
419 // This is just a wrapper for dashboard.exitFullscreen() which does the heavy lifting.
420 dashboard
.exitFullscreen();
423 // Adds controls to a widget. id is for internal use and image file name in images/dashboard/ (a .gif).
424 widget
.addControl = function(id
, control
) {
425 var markup
= '<a class="widget-icon ' + id
+ '-icon" alt="' + control
.description
+ '" title="' + control
.description
+ '"></a>';
426 control
.element
= $(markup
).prependTo($('.widget-controls', widget
.element
)).click(control
.callback
);
429 // An external method used only by and from external scripts to reload content. Not invoked or used internally.
430 // The widget must provide the script that executes this, as well as the script that invokes it.
431 widget
.reloadContent = function() {
432 getJavascript(widget
.reloadContentScript
);
433 invokeCallback(opts
.widgetCallbacks
.reloadContent
, widget
);
436 // Removes the widget from the dashboard, and saves columns.
437 widget
.remove = function() {
438 if ( confirm( 'Are you sure you want to remove "' + widget
.title
+ '"?') ) {
439 invokeCallback(opts
.widgetCallbacks
.remove
, widget
);
440 widget
.element
.fadeOut(opts
.animationSpeed
, function() {
442 dashboard
.saveColumns();
446 // End public methods of widget.
449 * Public properties of widget.
452 // Default controls. External script can add more with widget.addControls()
455 description
: ts('Configure this dashlet'),
456 callback
: widget
.toggleSettings
459 description
: ts('Collapse or expand'),
460 callback
: widget
.toggleMinimize
463 description
: ts('View fullscreen'),
464 callback
: widget
.enterFullscreen
467 description
: ts('Remove from dashboard'),
468 callback
: widget
.remove
471 // End public properties of widget.
474 * Private properties of widget.
477 // We're gonna 'fork' execution again, so let's tell the user to hold with us till the AJAX callback gets invoked.
478 var throbber
= $(opts
.throbberMarkup
).appendTo(widget
.element
);
479 var params
= $.extend({}, opts
.ajaxCallbacks
.getWidget
.data
, {id
: widget
.id
});
480 $.getJSON(opts
.ajaxCallbacks
.getWidget
.url
, params
, init
);
482 // Help dashboard track whether we've got any outstanding requests on which initialization is pending.
483 asynchronousRequestCounter
++;
485 // End of private properties of widget.
488 * Private methods of widget.
491 // Ajax callback for widget initialization.
492 function init(data
, status
) {
493 asynchronousRequestCounter
--;
494 $.extend(widget
, data
);
496 // Delete controls that don't apply to this widget.
497 if (!widget
.settings
) {
498 delete widget
.controls
.settings
;
500 if (!widget
.fullscreenUrl
) {
501 delete widget
.controls
.fullscreen
;
504 widget
.element
.attr('id', 'widget-' + widget
.id
).addClass(widget
.classes
);
506 // Build and add the widget's DOM element.
507 $(widget
.element
).append(widgetHTML()).trigger('crmLoad');
508 // Save the content element so that external scripts can reload it easily.
509 widget
.contentElement
= $('.widget-content', widget
.element
);
510 $.each(widget
.controls
, widget
.addControl
);
512 // Switch the initial state so that it initializes to the correct state.
513 widget
.minimized
= !widget
.minimized
;
514 widget
.toggleMinimize();
515 getJavascript(widget
.initScript
);
516 invokeCallback(opts
.widgetCallbacks
.get, widget
);
518 // completeInit() is a private method of the dashboard. Let it complete initialization of the dashboard.
522 // Builds inner HTML for widgets.
523 function widgetHTML() {
525 html
+= '<div class="widget-wrapper">';
526 html
+= ' <div class="widget-controls"><h3 class="widget-header">' + widget
.title
+ '</h3></div>';
527 html
+= ' <div class="widget-content crm-ajax-container">' + widget
.content
+ '</div>';
532 // Initializes a widgets settings pane.
533 function initSettings() {
534 // Overwrite widget.settings (boolean).
537 element
: widget
.throbber
.show(),
541 // Get the settings markup and script executables for this widget.
542 var params
= $.extend({}, opts
.ajaxCallbacks
.widgetSettings
.data
, { id
: widget
.id
});
543 $.getJSON(opts
.ajaxCallbacks
.widgetSettings
.url
, params
, function(response
, status
) {
544 $.extend(widget
.settings
, response
);
545 // Build and add the settings form to the DOM. Bind the form's submit event handler/callback.
546 widget
.settings
.element
= $(widgetSettingsHTML()).appendTo($('.widget-wrapper', widget
.element
)).submit(widget
.saveSettings
);
547 // Bind the cancel button's event handler too.
548 widget
.settings
.cancelButton
= $('.widget-settings-cancel', widget
.settings
.element
).click(cancelEditSettings
);
549 // Build and add the inner form elements from the HTML markup provided in the AJAX data.
550 widget
.settings
.innerElement
= $('.widget-settings-inner', widget
.settings
.element
).append(widget
.settings
.markup
);
551 widget
.settings
.ready
= true;
553 if (widget
.settings
.displayed
) {
554 // If the user hasn't clicked away from the settings pane, then display the form.
555 widget
.throbber
.hide();
556 widget
.showSettings();
559 getJavascript(widget
.settings
.initScript
);
563 // Builds HTML for widget settings forms.
564 function widgetSettingsHTML() {
566 html
+= '<form class="widget-settings">';
567 html
+= ' <div class="widget-settings-inner"></div>';
568 html
+= ' <div class="widget-settings-buttons">';
569 html
+= ' <input id="' + widget
.id
+ '-settings-save" class="widget-settings-save" value="Save" type="submit" />';
570 html
+= ' <input id="' + widget
.id
+ '-settings-cancel" class="widget-settings-cancel" value="Cancel" type="submit" />';
576 // Initializes a generic widget content throbber, for use by settings form and external scripts.
577 function initThrobber() {
578 if (!widget
.throbber
) {
579 widget
.throbber
= $(opts
.throbberMarkup
).appendTo($('.widget-wrapper', widget
.element
));
583 // Event handler/callback for cancel button clicks.
584 // @todo test this gets caught by all browsers when the cancel button is 'clicked' via the keyboard.
585 function cancelEditSettings() {
586 widget
.toggleSettings();
590 // Helper function to execute external script on the server.
591 // @todo It would be nice to provide some context to the script. How?
592 function getJavascript(url
) {
600 // Public static properties of dashboard. Default settings.
601 $.fn
.dashboard
.defaults
= {
603 emptyPlaceholderInner
: ts('There are no dashlets in this column of your dashboard.'),
604 fullscreenHeaderInner
: ts('Back to dashboard mode'),
605 throbberMarkup
: '<div class="crm-loading-element">' + ts('Loading') + '...</div>',
611 // Default widget settings.
612 $.fn
.dashboard
.widget
= {