2019-09-25 16:19:42 +00:00
|
|
|
/**
|
2019-01-31 17:01:45 +00:00
|
|
|
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2021-03-31 10:15:40 +00:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
* @author npmbuildbot[bot] "npmbuildbot[bot]@users.noreply.github.com"
|
2019-01-31 17:01:45 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2021-03-31 10:15:40 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import _ from 'underscore'
|
2022-01-10 13:55:59 +00:00
|
|
|
/** @typedef {import('jquery')} jQuery */
|
2019-01-31 17:01:45 +00:00
|
|
|
import $ from 'jquery'
|
2020-10-21 14:49:18 +00:00
|
|
|
import { showMessage, TOAST_DEFAULT_TIMEOUT, TOAST_PERMANENT_TIMEOUT } from '@nextcloud/dialogs'
|
2019-01-31 17:01:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package instead
|
2019-01-31 17:01:45 +00:00
|
|
|
* @namespace OC.Notification
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
|
2019-03-01 12:02:12 +00:00
|
|
|
updatableNotification: null,
|
|
|
|
|
|
|
|
getDefaultNotificationFunction: null,
|
2019-01-31 17:01:45 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-25 16:19:42 +00:00
|
|
|
* @param {Function} callback callback function
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
setDefault(callback) {
|
2019-09-25 16:19:42 +00:00
|
|
|
this.getDefaultNotificationFunction = callback
|
2019-01-31 17:01:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides a notification.
|
|
|
|
*
|
|
|
|
* If a row is given, only hide that one.
|
|
|
|
* If no row is given, hide all notifications.
|
|
|
|
*
|
|
|
|
* @param {jQuery} [$row] notification row
|
|
|
|
* @param {Function} [callback] callback
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
hide($row, callback) {
|
2019-01-31 17:01:45 +00:00
|
|
|
if (_.isFunction($row)) {
|
|
|
|
// first arg is the callback
|
2019-09-25 16:19:42 +00:00
|
|
|
callback = $row
|
|
|
|
$row = undefined
|
2019-01-31 17:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$row) {
|
2019-09-25 16:19:42 +00:00
|
|
|
console.error('Missing argument $row in OC.Notification.hide() call, caller needs to be adjusted to only dismiss its own notification')
|
|
|
|
return
|
2019-01-31 17:01:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 12:02:12 +00:00
|
|
|
// remove the row directly
|
2019-09-25 16:19:42 +00:00
|
|
|
$row.each(function() {
|
2020-02-07 10:02:45 +00:00
|
|
|
if ($(this)[0].toastify) {
|
|
|
|
$(this)[0].toastify.hideToast()
|
|
|
|
} else {
|
|
|
|
console.error('cannot hide toast because object is not set')
|
|
|
|
}
|
2019-03-01 12:02:12 +00:00
|
|
|
if (this === this.updatableNotification) {
|
|
|
|
this.updatableNotification = null
|
2019-01-31 17:01:45 +00:00
|
|
|
}
|
2019-03-01 12:02:12 +00:00
|
|
|
})
|
|
|
|
if (callback) {
|
|
|
|
callback.call()
|
|
|
|
}
|
|
|
|
if (this.getDefaultNotificationFunction) {
|
|
|
|
this.getDefaultNotificationFunction()
|
|
|
|
}
|
2019-01-31 17:01:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a notification as HTML without being sanitized before.
|
|
|
|
* If you pass unsanitized user input this may lead to a XSS vulnerability.
|
|
|
|
* Consider using show() instead of showHTML()
|
|
|
|
*
|
|
|
|
* @param {string} html Message to display
|
2021-12-02 17:32:57 +00:00
|
|
|
* @param {object} [options] options
|
2019-01-31 17:01:45 +00:00
|
|
|
* @param {string} [options.type] notification type
|
2022-01-10 13:06:28 +00:00
|
|
|
* @param {number} [options.timeout=0] timeout value, defaults to 0 (permanent)
|
2021-12-02 17:32:57 +00:00
|
|
|
* @return {jQuery} jQuery element for notification row
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
showHtml(html, options) {
|
2019-03-01 12:02:12 +00:00
|
|
|
options = options || {}
|
2019-07-12 06:43:12 +00:00
|
|
|
options.isHTML = true
|
2020-10-21 14:49:18 +00:00
|
|
|
options.timeout = (!options.timeout) ? TOAST_PERMANENT_TIMEOUT : options.timeout
|
2020-02-07 10:02:45 +00:00
|
|
|
const toast = showMessage(html, options)
|
|
|
|
toast.toastElement.toastify = toast
|
2019-03-01 12:02:12 +00:00
|
|
|
return $(toast.toastElement)
|
2019-01-31 17:01:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a sanitized notification
|
|
|
|
*
|
|
|
|
* @param {string} text Message to display
|
2021-12-02 17:32:57 +00:00
|
|
|
* @param {object} [options] options
|
2019-01-31 17:01:45 +00:00
|
|
|
* @param {string} [options.type] notification type
|
2022-01-10 13:06:28 +00:00
|
|
|
* @param {number} [options.timeout=0] timeout value, defaults to 0 (permanent)
|
2021-12-02 17:32:57 +00:00
|
|
|
* @return {jQuery} jQuery element for notification row
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
show(text, options) {
|
2021-01-20 13:44:39 +00:00
|
|
|
const escapeHTML = function(text) {
|
|
|
|
return text.toString()
|
|
|
|
.split('&').join('&')
|
|
|
|
.split('<').join('<')
|
|
|
|
.split('>').join('>')
|
|
|
|
.split('"').join('"')
|
|
|
|
.split('\'').join(''')
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:19:42 +00:00
|
|
|
options = options || {}
|
2020-10-21 14:49:18 +00:00
|
|
|
options.timeout = (!options.timeout) ? TOAST_PERMANENT_TIMEOUT : options.timeout
|
2021-01-20 13:44:39 +00:00
|
|
|
const toast = showMessage(escapeHTML(text), options)
|
2020-02-07 10:02:45 +00:00
|
|
|
toast.toastElement.toastify = toast
|
2019-09-25 16:19:42 +00:00
|
|
|
return $(toast.toastElement)
|
2019-01-31 17:01:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates (replaces) a sanitized notification.
|
|
|
|
*
|
|
|
|
* @param {string} text Message to display
|
2021-12-02 17:32:57 +00:00
|
|
|
* @return {jQuery} JQuery element for notificaiton row
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
showUpdate(text) {
|
2019-03-01 12:02:12 +00:00
|
|
|
if (this.updatableNotification) {
|
2019-09-25 16:19:42 +00:00
|
|
|
this.updatableNotification.hideToast()
|
2019-01-31 17:01:45 +00:00
|
|
|
}
|
2020-10-21 14:49:18 +00:00
|
|
|
this.updatableNotification = showMessage(text, { timeout: TOAST_PERMANENT_TIMEOUT })
|
2020-02-07 10:02:45 +00:00
|
|
|
this.updatableNotification.toastElement.toastify = this.updatableNotification
|
2019-09-25 16:19:42 +00:00
|
|
|
return $(this.updatableNotification.toastElement)
|
2019-01-31 17:01:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a notification that disappears after x seconds, default is
|
|
|
|
* 7 seconds
|
|
|
|
*
|
|
|
|
* @param {string} text Message to show
|
2021-12-02 17:32:57 +00:00
|
|
|
* @param {Array} [options] options array
|
2022-01-10 13:06:28 +00:00
|
|
|
* @param {number} [options.timeout=7] timeout in seconds, if this is 0 it will show the message permanently
|
2019-01-31 17:01:45 +00:00
|
|
|
* @param {boolean} [options.isHTML=false] an indicator for HTML notifications (true) or text (false)
|
|
|
|
* @param {string} [options.type] notification type
|
2022-01-10 13:55:59 +00:00
|
|
|
* @return {JQuery} the toast element
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
showTemporary(text, options) {
|
2019-03-01 12:02:12 +00:00
|
|
|
options = options || {}
|
2020-10-21 14:49:18 +00:00
|
|
|
options.timeout = options.timeout || TOAST_DEFAULT_TIMEOUT
|
2020-02-07 10:02:45 +00:00
|
|
|
const toast = showMessage(text, options)
|
|
|
|
toast.toastElement.toastify = toast
|
2019-09-25 16:19:42 +00:00
|
|
|
return $(toast.toastElement)
|
2019-01-31 17:01:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a notification is hidden.
|
2021-12-02 17:32:57 +00:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
2020-02-07 10:02:45 +00:00
|
|
|
* @deprecated 17.0.0 use the `@nextcloud/dialogs` package
|
2019-01-31 17:01:45 +00:00
|
|
|
*/
|
2020-07-31 07:26:43 +00:00
|
|
|
isHidden() {
|
2019-09-25 16:19:42 +00:00
|
|
|
return !$('#content').find('.toastify').length
|
2019-11-13 12:05:10 +00:00
|
|
|
},
|
2019-01-31 17:01:45 +00:00
|
|
|
}
|