Skip to content

Commit b9eca4e

Browse files
committed
initial commit & readme
0 parents  commit b9eca4e

13 files changed

+481
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/nbproject/private/

Controller/DialogController.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Xi\Bundle\DialogBundle\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
7+
/**
8+
* Dialog contains common dialog functionality
9+
*
10+
*/
11+
class DialogController extends Controller
12+
{
13+
14+
public function deleteAction()
15+
{
16+
return $this->render('XiDialogBundle:Dialog:delete.html.twig');
17+
}
18+
19+
}

DependencyInjection/Configuration.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Xi\Bundle\DialogBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* This is the class that validates and merges configuration from your app/config files
10+
*
11+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12+
*/
13+
class Configuration implements ConfigurationInterface
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function getConfigTreeBuilder()
19+
{
20+
$treeBuilder = new TreeBuilder();
21+
$rootNode = $treeBuilder->root('xi_dialog');
22+
23+
// Here you should define the parameters that are allowed to
24+
// configure your bundle. See the documentation linked above for
25+
// more information on that topic.
26+
27+
return $treeBuilder;
28+
}
29+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Xi\Bundle\DialogBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use Symfony\Component\DependencyInjection\Loader;
9+
10+
/**
11+
* This is the class that loads and manages your bundle configuration
12+
*
13+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14+
*/
15+
class XiDialogExtension extends Extension
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function load(array $configs, ContainerBuilder $container)
21+
{
22+
$configuration = new Configuration();
23+
$config = $this->processConfiguration($configuration, $configs);
24+
25+
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26+
$loader->load('services.yml');
27+
}
28+
}

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Dialog functionality for Symfony2
2+
3+
DialogBundle provides basic jQueryUI dialog with internal ajax functionality. It has dependency on xi-bundle-ajax that
4+
provides ajax interface and error handling. Dialogbundle dialogs can gracefully handle backend errors thanks to xi-bundle-ajax.
5+
6+
7+
8+
9+
## Dependencies
10+
11+
xi-bundle-ajax
12+
* https://github.com/xi-project/xi-bundle-ajax
13+
14+
15+
16+
## Dialog
17+
18+
Dialog is basic jQueryUI dialog with internal ajax functionality. On opening it displays loading animation, sends ajax request to your backend
19+
and displays content in dialog after receiving response. Script will load its content according to your element href attribute.
20+
21+
All jQueryUI dialog options are available and also there are also following parameters that you can use:
22+
23+
refreshDialogOnOpening:
24+
Should dialog content be refreshed if you open it again after you have closed it. (boolean) Default: false
25+
26+
headerAttribute:
27+
what attribute should be used as header for your dialog. (string) Default: 'title'
28+
29+
30+
The following example is written in Coffeescript.
31+
32+
```coffeescript
33+
34+
new App.Dialog('.dialog', {
35+
width: 600,
36+
position: [null, 100]
37+
headerAttribute: 'header'
38+
refreshDialogOnOpening: false
39+
}, optionalYourOwnLoaderClass, optionalYourOwnErrorizerClass)
40+
41+
```
42+
43+
## Confirmdialog
44+
45+
Confirmdialog is a simple ajax dialog for accepting changes that user has made.
46+
Confirmdialog is rendered by Twig extension that accept two arguments: url to target and (array) options.
47+
Options are rendered as attributes of element that launches confirmation dialog.
48+
If you want to set header for your dialog in default configuration please set `header` as one of the options.
49+
`header` also sets elements title if `title` option is not set.
50+
`linkText` is optional, the value is shown like this: <a href="#">linkText</a>
51+
Confirm button copies it's href attribure from the element that launches the dialog, and executes the default behaviour when clicked.
52+
53+
```twig
54+
55+
{{ sba_confirmdialog_render('url_to_what_shoud_be_deleted), {'header': 'header_of_yours', 'linkText': 'text_for_link' 'some_attribute': 'some_attribute_of_yours'}}
56+
57+
```

Resources/coffee/dialog.coffee

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#
2+
# Usage example: new App.Dialog('.dialog', {width: 500})
3+
# Now every element that has class "dialog" will launch a new dialog.
4+
# You must also set href and title to elements to get dialog work properly
5+
#
6+
# dialog
7+
class App.Dialog
8+
# element to be bound with, jquery ui dialog options, loader class
9+
constructor: (@element, options, @loader, @errorizers) ->
10+
11+
settings = {
12+
headerAttribute: 'title'
13+
refreshDialogOnOpening: false
14+
}
15+
$.extend(settings, options);
16+
17+
basedialog = $('<div></div>')
18+
if !loader
19+
loader = new App.AjaxLoader.Default()
20+
21+
if !errorizers or !errorizers.length
22+
@errorizers = [new App.FatalErrorizer.Default]
23+
else
24+
@errorizers.push(new App.FatalErrorizer.Default)
25+
26+
self = @
27+
$(@element).live('click', (event) ->
28+
dialog = basedialog.clone()
29+
loader.start()
30+
31+
settings.title = self.getHeader($(this), settings)
32+
settings.close = () =>
33+
self.onClose(dialog, settings, this)
34+
35+
if $(this).data('dialog-binded')
36+
self.reopenDialog($('#'+$(this).data('dialog-binded')))
37+
loader.stop()
38+
else
39+
$(dialog).load(self.getUri($(this)), (response, status, xhr) =>
40+
loader.stop()
41+
if !self.checkForErrors(response, status, xhr, dialog)
42+
self.loadComplete(dialog, this)
43+
self.bindDialogToElement(dialog, this)
44+
45+
).dialog(settings)
46+
return false
47+
)
48+
49+
checkForErrors: (response, status, xhr, dialog) =>
50+
if status == "error"
51+
this.displayErrors($(dialog), {'error':{'xhr': xhr}})
52+
else if response == '' or (typeof(response) == 'string' and response.search(/Fatal error:/i) != -1) # you really don't want to get empty string as response
53+
this.displayErrors($(dialog), response) #errorizer should know what empty or fatal error response means
54+
else
55+
return false
56+
57+
return true
58+
59+
# Calls errorizers to show errors
60+
displayErrors: ($element, response) =>
61+
for errorizer in @errorizers
62+
if errorizer.errorize $element, response
63+
return true
64+
65+
loadComplete: (dialog, element) ->
66+
67+
onClose: (dialog, settings, element) ->
68+
if(settings.refreshDialogOnOpening) #remove old dialogs if we want to load new content
69+
$(dialog).remove()
70+
if $(element).data('dialog-binded')
71+
$(element).removeData('dialog-binded')
72+
73+
getUri: (element) ->
74+
$(element).attr('href')
75+
76+
getHeader: (element, settings) ->
77+
$(element).attr(settings.headerAttribute)
78+
79+
bindDialogToElement: (dialog, element) ->
80+
uniqId = 'dialog-'+ new Date().getTime()
81+
$(element).data('dialog-binded', uniqId)
82+
$(dialog).attr('id', uniqId)
83+
84+
reopenDialog: (dialogContent) ->
85+
dialogContent.closest('.ui-dialog').show()
86+
87+
88+
#
89+
# Usage example: new App.ConfirmDialog('.confirm-dialog', { width: 300, modal: true})
90+
# You must also set href, data-href and title to elements to get dialog work properly
91+
#
92+
# - data-href should point to your service that provides confirmation message
93+
# - href should point to place where data is really being deleted
94+
# - title is used as dialog header
95+
#
96+
# dialog
97+
class App.ConfirmDialog extends App.Dialog
98+
99+
# uses data-href so user can remove objects if if javascript is not being used
100+
getUri: (element) ->
101+
$(element).data('href')
102+
103+
loadComplete: (dialog, element) ->
104+
$confirmBtn = $('[data-id|="confirm-delete"] > [data-id|="confirm"]')
105+
$confirmBtn.attr('href', $(element).attr('href'))
106+
107+
$('[data-id|="confirm-delete"] > [data-id|="cancel"]').live('click', =>
108+
$(dialog).dialog('close')
109+
return false
110+
)
111+
112+

Resources/config/routing.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
xi_dialog_delete:
2+
pattern: /dialog/delete
3+
defaults: { _controller: XiDialogBundle:Dialog:delete }

Resources/config/services.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
confirmdialog.twig.extension:
3+
class: Xi\Bundle\DialogBundle\Twig\Extensions\ConfirmDialog
4+
arguments: ["@twig", "@router", "xi_dialog_delete", "XiDialogBundle:Dialog:confirm.html.twig", {class: 'confirm-dialog'}]
5+
tags:
6+
- { name: twig.extension }

Resources/doc/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# DialogBundle
2+
3+
Simple dialog inteface.
4+
5+
Requires: XiAjaxBundle and Jquery to work.
6+
7+
8+
To use the dialog functionality you must setup your dialog and bind it to certain Jquery selector.
9+
10+
examples:
11+
new App.Dialog('.dialog', {width: 500})
12+
new App.Dialog('.modal', {width: 500, modal: true})
13+
14+
15+
16+
17+
## Delete confirmation dialog twig extesion
18+
19+
Creates delete button with confirmation functionality
20+
21+
Setup:
22+
git
23+
To use the confirmation dialog functionality you must setup your dialog and bind it to certain Jquery selector ('.confirm-dialog' is a default value).
24+
25+
For default behaviour add this to your JS: new App.ConfirmDialog('.confirm-dialog', { width: 300, modal: true})
26+
27+
To use delete confirmation dialog you just have to call following in your twig tempalte
28+
29+
{{ xi_confirmdialog_render('TITLE', 'URL', OPTIONS}}
30+
31+
TITLE can be any string. This string is your buttons tooltip and shows up as header in dialog box.
32+
URL is an url string. This should point to action that handles the deletion. Ie: url('xxx_youritemtobedeleted_delete', { 'id': tobedeleted.id }))
33+
OPTIONS are an array that contains key value pairs. There are injected to element to be created as attributes.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% block confirmDialogbutton %}
2+
{% spaceless %}
3+
<a
4+
{% block confirmDialogButtonAttributes %}
5+
{% for key, arg in arguments %}
6+
{{ key }}="{{ arg }}"
7+
{% endfor %}
8+
{% endblock %}
9+
>{{ arguments.linkText }}</a>
10+
{% endspaceless %}
11+
{% endblock %}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div data-id="confirm-delete" class="confirm-delete-dialog">
2+
<p>
3+
{{'xi.confirmdialog.delete.are_you_sure'|trans}}
4+
</p>
5+
<button data-id="confirm" class="ajax-link">{{'xi.confirmdialog.delete.im_sure'|trans}}</button>
6+
<button data-id="cancel">{{'xi.confirmdialog.delete.cancel'|trans}}</button>
7+
</div>

0 commit comments

Comments
 (0)