Skip to content

Commit c66040b

Browse files
committed
feat: Deprecate OC.dialogs and replace generic dialogs with @nextcloud/dialogs alternative
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent e1f257c commit c66040b

2 files changed

Lines changed: 131 additions & 131 deletions

File tree

core/src/OC/dialogs.js

Lines changed: 131 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ import IconMove from '@mdi/svg/svg/folder-move.svg?raw'
5151
import IconCopy from '@mdi/svg/svg/folder-multiple.svg?raw'
5252

5353
import OC from './index.js'
54-
import { FilePickerType, getFilePickerBuilder, spawnDialog } from '@nextcloud/dialogs'
54+
import { DialogBuilder, FilePickerType, getFilePickerBuilder, spawnDialog } from '@nextcloud/dialogs'
55+
import { translate as t } from '@nextcloud/l10n'
5556
import { basename } from 'path'
5657
import { defineAsyncComponent } from 'vue'
5758

@@ -60,7 +61,9 @@ import { defineAsyncComponent } from 'vue'
6061
*/
6162
const Dialogs = {
6263
// dialog button types
64+
/** @deprecated use `@nextcloud/dialogs` */
6365
YES_NO_BUTTONS: 70,
66+
/** @deprecated use `@nextcloud/dialogs` */
6467
OK_BUTTONS: 71,
6568

6669
/** @deprecated use FilePickerType from `@nextcloud/dialogs` */
@@ -74,15 +77,14 @@ const Dialogs = {
7477
/** @deprecated use FilePickerType from `@nextcloud/dialogs` */
7578
FILEPICKER_TYPE_CUSTOM: 5,
7679

77-
// used to name each dialog
78-
dialogsCounter: 0,
79-
8080
/**
8181
* displays alert dialog
8282
* @param {string} text content of dialog
8383
* @param {string} title dialog title
8484
* @param {function} callback which will be triggered when user presses OK
8585
* @param {boolean} [modal] make the dialog modal
86+
*
87+
* @deprecated 30.0.0 Use `@nextcloud/dialogs` instead or build your own with `@nextcloud/vue` NcDialog
8688
*/
8789
alert: function(text, title, callback, modal) {
8890
this.message(
@@ -94,12 +96,15 @@ const Dialogs = {
9496
modal
9597
)
9698
},
99+
97100
/**
98101
* displays info dialog
99102
* @param {string} text content of dialog
100103
* @param {string} title dialog title
101104
* @param {function} callback which will be triggered when user presses OK
102105
* @param {boolean} [modal] make the dialog modal
106+
*
107+
* @deprecated 30.0.0 Use `@nextcloud/dialogs` instead or build your own with `@nextcloud/vue` NcDialog
103108
*/
104109
info: function(text, title, callback, modal) {
105110
this.message(text, title, 'info', Dialogs.OK_BUTTON, callback, modal)
@@ -112,6 +117,8 @@ const Dialogs = {
112117
* @param {function} callback which will be triggered when user presses OK (true or false would be passed to callback respectively)
113118
* @param {boolean} [modal] make the dialog modal
114119
* @returns {Promise}
120+
*
121+
* @deprecated 30.0.0 Use `@nextcloud/dialogs` instead or build your own with `@nextcloud/vue` NcDialog
115122
*/
116123
confirm: function(text, title, callback, modal) {
117124
return this.message(
@@ -131,16 +138,34 @@ const Dialogs = {
131138
* @param {function} callback which will be triggered when user presses OK (true or false would be passed to callback respectively)
132139
* @param {boolean} [modal] make the dialog modal
133140
* @returns {Promise}
141+
*
142+
* @deprecated 30.0.0 Use `@nextcloud/dialogs` instead or build your own with `@nextcloud/vue` NcDialog
134143
*/
135-
confirmDestructive: function(text, title, buttons, callback, modal) {
136-
return this.message(
137-
text,
138-
title,
139-
'none',
140-
buttons,
141-
callback,
142-
modal === undefined ? true : modal
143-
)
144+
confirmDestructive: function(text, title, buttons = Dialogs.OK_BUTTONS, callback = () => {}, modal) {
145+
return (new DialogBuilder())
146+
.setName(title)
147+
.setText(text)
148+
.setButtons(
149+
buttons === Dialogs.OK_BUTTONS
150+
? [
151+
{
152+
label: t('core', 'Yes'),
153+
type: 'error',
154+
callback: () => {
155+
callback.clicked = true
156+
callback(true)
157+
},
158+
}
159+
]
160+
: Dialogs._getLegacyButtons(buttons, callback)
161+
)
162+
.build()
163+
.show()
164+
.then(() => {
165+
if (!callback.clicked) {
166+
callback(false)
167+
}
168+
})
144169
},
145170
/**
146171
* displays confirmation dialog
@@ -149,17 +174,35 @@ const Dialogs = {
149174
* @param {function} callback which will be triggered when user presses OK (true or false would be passed to callback respectively)
150175
* @param {boolean} [modal] make the dialog modal
151176
* @returns {Promise}
177+
*
178+
* @deprecated 30.0.0 Use `@nextcloud/dialogs` instead or build your own with `@nextcloud/vue` NcDialog
152179
*/
153180
confirmHtml: function(text, title, callback, modal) {
154-
return this.message(
155-
text,
156-
title,
157-
'notice',
158-
Dialogs.YES_NO_BUTTONS,
159-
callback,
160-
modal,
161-
true
162-
)
181+
return (new DialogBuilder())
182+
.setName(title)
183+
.setText('')
184+
.setButtons([
185+
{
186+
label: t('core', 'No'),
187+
callback: () => {},
188+
},
189+
{
190+
label: t('core', 'Yes'),
191+
type: 'primary',
192+
callback: () => {
193+
callback.clicked = true
194+
callback(true)
195+
},
196+
},
197+
])
198+
.build()
199+
.setHTML(text)
200+
.show()
201+
.then(() => {
202+
if (!callback.clicked) {
203+
callback(false)
204+
}
205+
})
163206
},
164207
/**
165208
* displays prompt dialog
@@ -320,105 +363,81 @@ const Dialogs = {
320363
/**
321364
* Displays raw dialog
322365
* You better use a wrapper instead ...
366+
*
367+
* @deprecated 30.0.0 Use `@nextcloud/dialogs` instead or build your own with `@nextcloud/vue` NcDialog
323368
*/
324-
message: function(content, title, dialogType, buttons, callback, modal, allowHtml) {
325-
return $.when(this._getMessageTemplate()).then(function($tmpl) {
326-
var dialogName = 'oc-dialog-' + Dialogs.dialogsCounter + '-content'
327-
var dialogId = '#' + dialogName
328-
var $dlg = $tmpl.octemplate({
329-
dialog_name: dialogName,
330-
title: title,
331-
message: content,
332-
type: dialogType
333-
}, allowHtml ? { escapeFunction: '' } : {})
334-
if (modal === undefined) {
335-
modal = false
369+
message: function(content, title, dialogType, buttons, callback = () => {}, modal, allowHtml) {
370+
const builder = (new DialogBuilder())
371+
.setName(title)
372+
.setText(allowHtml ? '' : content)
373+
.setButtons(Dialogs._getLegacyButtons(buttons, callback))
374+
375+
switch (dialogType) {
376+
case 'alert':
377+
builder.setSeverity('warning')
378+
break
379+
case 'notice':
380+
builder.setSeverity('info')
381+
break
382+
default:
383+
break
384+
}
385+
386+
const dialog = builder.build()
387+
388+
if (allowHtml) {
389+
dialog.setHTML(content)
390+
}
391+
392+
return dialog.show().then(() => {
393+
if(!callback._clicked) {
394+
callback(false)
336395
}
337-
$('body').append($dlg)
338-
var buttonlist = []
339-
switch (buttons) {
396+
})
397+
},
398+
399+
/**
400+
* Helper for legacy API
401+
* @deprecated
402+
*/
403+
_getLegacyButtons(buttons, callback) {
404+
const buttonList = []
405+
406+
switch (typeof buttons === 'object' ? buttons.type : buttons) {
340407
case Dialogs.YES_NO_BUTTONS:
341-
buttonlist = [{
342-
text: t('core', 'No'),
343-
click: function() {
344-
if (callback !== undefined) {
345-
callback(false)
346-
}
347-
$(dialogId).ocdialog('close')
348-
}
349-
},
350-
{
351-
text: t('core', 'Yes'),
352-
click: function() {
353-
if (callback !== undefined) {
354-
callback(true)
355-
}
356-
$(dialogId).ocdialog('close')
408+
buttonList.push({
409+
label: buttons?.cancel ?? t('core', 'No'),
410+
callback: () => {
411+
callback._clicked = true
412+
callback(false)
357413
},
358-
defaultButton: true
359-
}]
414+
})
415+
buttonList.push({
416+
label: buttons?.confirm ?? t('core', 'Yes'),
417+
type: 'primary',
418+
callback: () => {
419+
callback._clicked = true
420+
callback(true)
421+
},
422+
})
360423
break
361-
case Dialogs.OK_BUTTON:
362-
var functionToCall = function() {
363-
$(dialogId).ocdialog('close')
364-
if (callback !== undefined) {
365-
callback()
366-
}
367-
}
368-
buttonlist[0] = {
369-
text: t('core', 'OK'),
370-
click: functionToCall,
371-
defaultButton: true
372-
}
424+
case Dialogs.OK_BUTTONS:
425+
buttonList.push({
426+
label: buttons?.confirm ?? t('core', 'OK'),
427+
type: 'primary',
428+
callback: () => {
429+
callback._clicked = true
430+
callback(true)
431+
},
432+
})
373433
break
374434
default:
375-
if (typeof(buttons) === 'object') {
376-
switch (buttons.type) {
377-
case Dialogs.YES_NO_BUTTONS:
378-
buttonlist = [{
379-
text: buttons.cancel || t('core', 'No'),
380-
click: function() {
381-
if (callback !== undefined) {
382-
callback(false)
383-
}
384-
$(dialogId).ocdialog('close')
385-
}
386-
},
387-
{
388-
text: buttons.confirm || t('core', 'Yes'),
389-
click: function() {
390-
if (callback !== undefined) {
391-
callback(true)
392-
}
393-
$(dialogId).ocdialog('close')
394-
},
395-
defaultButton: true,
396-
classes: buttons.confirmClasses
397-
}]
398-
break
399-
}
400-
}
435+
console.error('Invalid call to OC.dialogs')
401436
break
402-
}
403-
404-
$(dialogId).ocdialog({
405-
closeOnEscape: true,
406-
closeCallback: () => { callback && callback(false) },
407-
modal: modal,
408-
buttons: buttonlist
409-
})
410-
Dialogs.dialogsCounter++
411-
})
412-
.fail(function(status, error) {
413-
// If the method is called while navigating away from
414-
// the page, we still want to deliver the message.
415-
if (status === 0) {
416-
alert(title + ': ' + content)
417-
} else {
418-
alert(t('core', 'Error loading message template: {error}', { error: error }))
419-
}
420-
})
437+
}
438+
return buttonList
421439
},
440+
422441
_fileexistsshown: false,
423442
/**
424443
* Displays file exists dialog
@@ -786,22 +805,6 @@ const Dialogs = {
786805
return dialogDeferred.promise()
787806
},
788807

789-
_getMessageTemplate: function() {
790-
var defer = $.Deferred()
791-
if (!this.$messageTemplate) {
792-
var self = this
793-
$.get(OC.filePath('core', 'templates', 'message.html'), function(tmpl) {
794-
self.$messageTemplate = $(tmpl)
795-
defer.resolve(self.$messageTemplate)
796-
})
797-
.fail(function(jqXHR, textStatus, errorThrown) {
798-
defer.reject(jqXHR.status, errorThrown)
799-
})
800-
} else {
801-
defer.resolve(this.$messageTemplate)
802-
}
803-
return defer.promise()
804-
},
805808
_getFileExistsTemplate: function() {
806809
var defer = $.Deferred()
807810
if (!this.$fileexistsTemplate) {

core/templates/message.html

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)