Merge branch 'improved-embed-and-download'

JI-92-path-fix
Paal Joergensen 2016-12-21 13:21:39 +01:00
commit 671c079eee
5 changed files with 312 additions and 145 deletions

View File

@ -558,6 +558,16 @@ interface H5PFrameworkInterface {
* Will trigger after the export file is created. * Will trigger after the export file is created.
*/ */
public function afterExportCreated(); public function afterExportCreated();
/**
* Check if user has permissions to an action
*
* @method hasPermission
* @param [H5PPermission] $permission Permission type, ref H5PPermission
* @param [int] $id Id need by platform to determine permission
* @return boolean
*/
public function hasPermission($permission, $id = NULL);
} }
/** /**
@ -1669,6 +1679,20 @@ Class H5PExport {
} }
} }
abstract class H5PPermission {
const DOWNLOAD_H5P = 0;
const EMBED_H5P = 1;
}
abstract class H5PDisplayOptionBehaviour {
const NEVER_SHOW = 0;
const CONTROLLED_BY_AUTHOR_DEFAULT_ON = 1;
const CONTROLLED_BY_AUTHOR_DEFAULT_OFF = 2;
const ALWAYS_SHOW = 3;
const CONTROLLED_BY_PERMISSIONS = 4;
}
/** /**
* Functions and storage shared by the other H5P classes * Functions and storage shared by the other H5P classes
*/ */
@ -1690,7 +1714,8 @@ class H5PCore {
'js/h5p-x-api-event.js', 'js/h5p-x-api-event.js',
'js/h5p-x-api.js', 'js/h5p-x-api.js',
'js/h5p-content-type.js', 'js/h5p-content-type.js',
'js/h5p-confirmation-dialog.js' 'js/h5p-confirmation-dialog.js',
'js/h5p-action-bar.js'
); );
public static $adminScripts = array( public static $adminScripts = array(
'js/jquery.js', 'js/jquery.js',
@ -1713,12 +1738,18 @@ class H5PCore {
const DISABLE_COPYRIGHT = 8; const DISABLE_COPYRIGHT = 8;
const DISABLE_ABOUT = 16; const DISABLE_ABOUT = 16;
const DISPLAY_OPTION_FRAME = 'frame';
const DISPLAY_OPTION_DOWNLOAD = 'export';
const DISPLAY_OPTION_EMBED = 'embed';
const DISPLAY_OPTION_COPYRIGHT = 'copyright';
const DISPLAY_OPTION_ABOUT = 'icon';
// Map flags to string // Map flags to string
public static $disable = array( public static $disable = array(
self::DISABLE_FRAME => 'frame', self::DISABLE_FRAME => self::DISPLAY_OPTION_FRAME,
self::DISABLE_DOWNLOAD => 'download', self::DISABLE_DOWNLOAD => self::DISPLAY_OPTION_DOWNLOAD,
self::DISABLE_EMBED => 'embed', self::DISABLE_EMBED => self::DISPLAY_OPTION_EMBED,
self::DISABLE_COPYRIGHT => 'copyright' self::DISABLE_COPYRIGHT => self::DISPLAY_OPTION_COPYRIGHT
); );
/** /**
@ -2442,43 +2473,28 @@ class H5PCore {
} }
/** /**
* * Create representation of display options as int
*/
public function getGlobalDisable() {
$disable = self::DISABLE_NONE;
// Allow global settings to override and disable options
if (!$this->h5pF->getOption('frame', TRUE)) {
$disable |= self::DISABLE_FRAME;
}
else {
if (!$this->h5pF->getOption('export', TRUE)) {
$disable |= self::DISABLE_DOWNLOAD;
}
if (!$this->h5pF->getOption('embed', TRUE)) {
$disable |= self::DISABLE_EMBED;
}
if (!$this->h5pF->getOption('copyright', TRUE)) {
$disable |= self::DISABLE_COPYRIGHT;
}
if (!$this->h5pF->getOption('icon', TRUE)) {
$disable |= self::DISABLE_ABOUT;
}
}
return $disable;
}
/**
* Determine disable state from sources.
* *
* @param array $sources * @param array $sources
* @param int $current * @param int $current
* @return int * @return int
*/ */
public function getDisable(&$sources, $current) { public function getStorableDisplayOptions(&$sources, $current) {
// Download - force setting it if always on or always off
$download = $this->h5pF->getOption(self::DISPLAY_OPTION_DOWNLOAD, H5PDisplayOptionBehaviour::ALWAYS_SHOW);
if ($download == H5PDisplayOptionBehaviour::ALWAYS_SHOW ||
$download == H5PDisplayOptionBehaviour::NEVER_SHOW) {
$sources[self::DISPLAY_OPTION_DOWNLOAD] = ($download == H5PDisplayOptionBehaviour::ALWAYS_SHOW);
}
// Embed - force setting it if always on or always off
$embed = $this->h5pF->getOption(self::DISPLAY_OPTION_EMBED, H5PDisplayOptionBehaviour::ALWAYS_SHOW);
if ($embed == H5PDisplayOptionBehaviour::ALWAYS_SHOW ||
$embed == H5PDisplayOptionBehaviour::NEVER_SHOW) {
$sources[self::DISPLAY_OPTION_EMBED] = ($embed == H5PDisplayOptionBehaviour::ALWAYS_SHOW);
}
foreach (H5PCore::$disable as $bit => $option) { foreach (H5PCore::$disable as $bit => $option) {
if ($this->h5pF->getOption(($bit & H5PCore::DISABLE_DOWNLOAD ? 'export' : $option), TRUE)) {
if (!isset($sources[$option]) || !$sources[$option]) { if (!isset($sources[$option]) || !$sources[$option]) {
$current |= $bit; // Disable $current |= $bit; // Disable
} }
@ -2486,10 +2502,123 @@ class H5PCore {
$current &= ~$bit; // Enable $current &= ~$bit; // Enable
} }
} }
}
return $current; return $current;
} }
/**
* Determine display options visibility and value on edit
*
* @param int $disable
* @return array
*/
public function getDisplayOptionsForEdit($disable = NULL) {
$display_options = [];
$current_display_options = $disable === NULL ? [] : $this->getDisplayOptionsAsArray($disable);
if ($this->h5pF->getOption(self::DISPLAY_OPTION_FRAME, TRUE)) {
$display_options[self::DISPLAY_OPTION_FRAME] =
isset($current_display_options[self::DISPLAY_OPTION_FRAME]) ?
$current_display_options[self::DISPLAY_OPTION_FRAME] :
TRUE;
// Download
$export = $this->h5pF->getOption(self::DISPLAY_OPTION_DOWNLOAD, H5PDisplayOptionBehaviour::ALWAYS_SHOW);
if ($export == H5PDisplayOptionBehaviour::CONTROLLED_BY_AUTHOR_DEFAULT_ON ||
$export == H5PDisplayOptionBehaviour::CONTROLLED_BY_AUTHOR_DEFAULT_OFF) {
$display_options[self::DISPLAY_OPTION_DOWNLOAD] =
isset($current_display_options[self::DISPLAY_OPTION_DOWNLOAD]) ?
$current_display_options[self::DISPLAY_OPTION_DOWNLOAD] :
($export == H5PDisplayOptionBehaviour::CONTROLLED_BY_AUTHOR_DEFAULT_ON);
}
// Embed
$embed = $this->h5pF->getOption(self::DISPLAY_OPTION_EMBED, H5PDisplayOptionBehaviour::ALWAYS_SHOW);
if ($embed == H5PDisplayOptionBehaviour::CONTROLLED_BY_AUTHOR_DEFAULT_ON ||
$embed == H5PDisplayOptionBehaviour::CONTROLLED_BY_AUTHOR_DEFAULT_OFF) {
$display_options[self::DISPLAY_OPTION_EMBED] =
isset($current_display_options[self::DISPLAY_OPTION_EMBED]) ?
$current_display_options[self::DISPLAY_OPTION_EMBED] :
($embed == H5PDisplayOptionBehaviour::CONTROLLED_BY_AUTHOR_DEFAULT_ON);
}
// Copyright
if ($this->h5pF->getOption(self::DISPLAY_OPTION_COPYRIGHT, TRUE)) {
$display_options[self::DISPLAY_OPTION_COPYRIGHT] =
isset($current_display_options[self::DISPLAY_OPTION_COPYRIGHT]) ?
$current_display_options[self::DISPLAY_OPTION_COPYRIGHT] :
TRUE;
}
}
return $display_options;
}
/**
* Helper function used to figure out embed & download behaviour
*
* @param string $option_name
* @param H5PPermission $permission
* @param int $id
* @param bool &$value
*/
private function setDisplayOptionOverrides($option_name, $permission, $id, &$value) {
$behaviour = $this->h5pF->getOption($option_name, H5PDisplayOptionBehaviour::ALWAYS_SHOW);
// If never show globally, force hide
if ($behaviour == H5PDisplayOptionBehaviour::NEVER_SHOW) {
$value = false;
}
elseif ($behaviour == H5PDisplayOptionBehaviour::ALWAYS_SHOW) {
// If always show or permissions say so, force show
$value = true;
}
elseif ($behaviour == H5PDisplayOptionBehaviour::CONTROLLED_BY_PERMISSIONS) {
$value = $this->h5pF->hasPermission($permission, $id);
}
}
/**
* Determine display option visibility when viewing H5P
*
* @param int $display_options
* @param int $id Might be content id or user id.
* Depends on what the platform needs to be able to determine permissions.
* @return array
*/
public function getDisplayOptionsForView($disable, $id) {
$display_options = $this->getDisplayOptionsAsArray($disable);
if ($this->h5pF->getOption(self::DISPLAY_OPTION_FRAME, TRUE) == FALSE) {
$display_options[self::DISPLAY_OPTION_FRAME] = false;
}
else {
$this->setDisplayOptionOverrides(self::DISPLAY_OPTION_DOWNLOAD, H5PPermission::DOWNLOAD_H5P, $id, $display_options[self::DISPLAY_OPTION_DOWNLOAD]);
$this->setDisplayOptionOverrides(self::DISPLAY_OPTION_EMBED, H5PPermission::EMBED_H5P, $id, $display_options[self::DISPLAY_OPTION_EMBED]);
if ($this->h5pF->getOption(self::DISPLAY_OPTION_COPYRIGHT, TRUE) == FALSE) {
$display_options[self::DISPLAY_OPTION_COPYRIGHT] = false;
}
}
return $display_options;
}
/**
* Convert display options as single byte to array
*
* @param int $disable
* @return array
*/
private function getDisplayOptionsAsArray($disable) {
return array(
self::DISPLAY_OPTION_FRAME => !($disable & H5PCore::DISABLE_FRAME),
self::DISPLAY_OPTION_DOWNLOAD => !($disable & H5PCore::DISABLE_DOWNLOAD),
self::DISPLAY_OPTION_EMBED => !($disable & H5PCore::DISABLE_EMBED),
self::DISPLAY_OPTION_COPYRIGHT => !($disable & H5PCore::DISABLE_COPYRIGHT),
self::DISPLAY_OPTION_ABOUT => $this->h5pF->getOption(self::DISPLAY_OPTION_ABOUT, TRUE),
);
}
/** /**
* Small helper for getting the library's ID. * Small helper for getting the library's ID.
* *
@ -3151,7 +3280,6 @@ class H5PContentValidator {
break; break;
} }
} }
// Using a library in content that is not present at all in semantics // Using a library in content that is not present at all in semantics
if ($message === NULL) { if ($message === NULL) {
$message = $this->h5pF->t('The H5P library %library used in the content is not valid', array( $message = $this->h5pF->t('The H5P library %library used in the content is not valid', array(

View File

@ -1,19 +0,0 @@
(function ($) {
$(document).ready(function () {
var $inputs = $('.h5p-action-bar-settings input');
var $frame = $inputs.filter('input[name="frame"], input[name="h5p_frame"]');
var $others = $inputs.filter(':not(input[name="frame"], input[name="h5p_frame"])');
var toggle = function () {
if ($frame.is(':checked')) {
$others.attr('disabled', false);
}
else {
$others.attr('disabled', true);
}
};
$frame.change(toggle);
toggle();
});
})(H5P.jQuery);

89
js/h5p-action-bar.js Normal file
View File

@ -0,0 +1,89 @@
H5P.ActionBar = (function ($, EventDispatcher) {
"use strict";
function ActionBar(displayOptions) {
EventDispatcher.call(this);
var self = this;
var hasActions = false;
// Create action bar
var $actions = H5P.jQuery('<ul class="h5p-actions"></ul>');
/**
* Helper for creating action bar buttons.
*
* @private
* @param {string} type
* @param {string} customClass Instead of type class
*/
var addActionButton = function (type, customClass) {
var handler = function () {
self.trigger(type);
};
H5P.jQuery('<li/>', {
'class': 'h5p-button h5p-' + (customClass ? customClass : type),
role: 'button',
tabindex: 0,
title: H5P.t(type + 'Description'),
html: H5P.t(type),
on: {
click: handler,
keypress: function (e) {
if (e.which === 32) {
handler();
e.preventDefault(); // (since return false will block other inputs)
}
}
},
appendTo: $actions
});
hasActions = true;
};
// Register action bar buttons
if (displayOptions.export) {
// Add export button
addActionButton('download', 'export');
}
if (displayOptions.copyright) {
addActionButton('copyrights');
}
if (displayOptions.embed) {
addActionButton('embed');
}
if (displayOptions.icon) {
// Add about H5P button icon
H5P.jQuery('<li><a class="h5p-link" href="http://h5p.org" target="_blank" title="' + H5P.t('h5pDescription') + '"></a></li>').appendTo($actions);
hasActions = true;
}
/**
* Returns a reference to the dom element
*
* @method getDOMElement
* @return {H5P.jQuery}
*/
self.getDOMElement = function () {
return $actions;
};
/**
* Does the actionbar contain actions?
*
* @method hasActions
* @return {Boolean}
*/
self.hasActions = function () {
return hasActions;
}
};
ActionBar.prototype = Object.create(EventDispatcher.prototype);
ActionBar.prototype.constructor = ActionBar;
return ActionBar;
})(H5P.jQuery, H5P.EventDispatcher);

23
js/h5p-display-options.js Normal file
View File

@ -0,0 +1,23 @@
/**
* Utility that makes it possible to hide fields when a checkbox is unchecked
*/
(function ($) {
function setupHiding () {
var $toggler = $(this);
// Getting the field which should be hidden:
var $subject = $($toggler.data('h5p-visibility-subject-selector'));
var toggle = function () {
$subject.toggle($toggler.is(':checked'));
};
$toggler.change(toggle);
toggle();
}
$(document).ready(function () {
// Get the checkboxes making other fields being hidden:
$('.h5p-visibility-toggler').each(setupHiding);
});
})(H5P.jQuery);

View File

@ -47,24 +47,6 @@ else if (document.documentElement.msRequestFullscreen) {
H5P.fullScreenBrowserPrefix = 'ms'; H5P.fullScreenBrowserPrefix = 'ms';
} }
/** @const {number} */
H5P.DISABLE_NONE = 0;
/** @const {number} */
H5P.DISABLE_FRAME = 1;
/** @const {number} */
H5P.DISABLE_DOWNLOAD = 2;
/** @const {number} */
H5P.DISABLE_EMBED = 4;
/** @const {number} */
H5P.DISABLE_COPYRIGHT = 8;
/** @const {number} */
H5P.DISABLE_ABOUT = 16;
/** /**
* Keep track of when the H5Ps where started. * Keep track of when the H5Ps where started.
* *
@ -156,82 +138,46 @@ H5P.init = function (target) {
}); });
} }
// Create action bar
var $actions = H5P.jQuery('<ul class="h5p-actions"></ul>');
/** /**
* Helper for creating action bar buttons. * Create action bar
*
* @private
* @param {string} type
* @param {function} handler
* @param {string} customClass Instead of type class
*/ */
var addActionButton = function (type, handler, customClass) { var displayOptions = contentData.displayOptions;
H5P.jQuery('<li/>', { var displayFrame = false;
'class': 'h5p-button h5p-' + (customClass ? customClass : type), if (displayOptions.frame) {
role: 'button', // Special handling of copyrights
tabindex: 0, if (displayOptions.copyright) {
title: H5P.t(type + 'Description'), var copyrights = H5P.getCopyrights(instance, library.params, contentId);
html: H5P.t(type), if (!copyrights) {
on: { displayOptions.copyright = false;
click: handler,
keypress: function (e) {
if (e.which === 32) {
handler();
e.preventDefault(); // (since return false will block other inputs)
} }
} }
},
appendTo: $actions
});
};
// Register action bar buttons // Create action bar
if (!(contentData.disable & H5P.DISABLE_DOWNLOAD)) { var actionBar = new H5P.ActionBar(displayOptions);
// Add export button var $actions = actionBar.getDOMElement();
addActionButton('download', function () {
// Use button for download to avoid people linking directly to the .h5p actionBar.on('download', function () {
window.location.href = contentData.exportUrl; window.location.href = contentData.exportUrl;
}, 'export'); });
} actionBar.on('copyrights', function () {
if (!(contentData.disable & H5P.DISABLE_COPYRIGHT)) { var dialog = new H5P.Dialog('copyrights', H5P.t('copyrightInformation'), copyrights, $container);
var copyright = H5P.getCopyrights(instance, library.params, contentId);
if (copyright) {
// Add copyright dialog button
addActionButton('copyrights', function () {
// Open dialog with copyright information
var dialog = new H5P.Dialog('copyrights', H5P.t('copyrightInformation'), copyright, $container);
dialog.open(); dialog.open();
}); });
} actionBar.on('embed', function () {
}
if (!(contentData.disable & H5P.DISABLE_EMBED)) {
// Add embed button
addActionButton('embed', function () {
// Open dialog with embed information
H5P.openEmbedDialog($actions, contentData.embedCode, contentData.resizeCode, { H5P.openEmbedDialog($actions, contentData.embedCode, contentData.resizeCode, {
width: $element.width(), width: $element.width(),
height: $element.height() height: $element.height()
}); });
}); });
}
if (!(contentData.disable & H5P.DISABLE_ABOUT)) { if (actionBar.hasActions()) {
// Add about H5P button icon displayFrame = true;
H5P.jQuery('<li><a class="h5p-link" href="http://h5p.org" target="_blank" title="' + H5P.t('h5pDescription') + '"></a></li>').appendTo($actions);
}
// Insert action bar if it has any content
if (!(contentData.disable & H5P.DISABLE_FRAME) && $actions.children().length) {
$actions.insertAfter($container); $actions.insertAfter($container);
$element.addClass('h5p-frame');
} }
else {
$element.addClass('h5p-no-frame');
} }
$element.addClass(displayFrame ? 'h5p-frame' : 'h5p-no-frame');
// Keep track of when we started // Keep track of when we started
H5P.opened[contentId] = new Date(); H5P.opened[contentId] = new Date();