forked from ajshort/silverstripe-itemsetfield
/
ItemSetField.js
131 lines (110 loc) · 3.09 KB
/
ItemSetField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
;(function($){
/**
* Performs a request, and either pops up a dialog or reloads the form depending
* on the result.
*/
var request = function(element, title, params, callback) {
$.ajax($.extend(params, {
dataType: 'html',
success: function(data) {
var holder = $('<div></div>').html(data);
var el = holder.children();
// If we get an ItemSetField back, then replace it
if (el.length == 1 && el.is('.itemsetfield')) {
var field = $('#' + el.attr('id'));
if (field.data('itemsetfield-dialog')) {
field.data('itemsetfield-dialog').dialog('close');
}
field.replaceWith(el);
} else {
var dialog = dialogFor(element, title);
dialog.empty().append(el).dialog('open');
dialog.find("form").submit(formHandler);
}
},
error: function() {
alert(ss.i18n._t('ItemSetField.ERROR', 'Couldn\'t execute action'));
},
complete: function() {
callback();
}
}));
}
var dialogFor = function(element, title) {
// If we're already in a dialog, just return it.
var existing = element.parents('.itemsetfield-dialog');
if (existing.length) {
return existing;
}
// Otherwise spawn a dialog off the ItemSetField
var field = element.parents('.itemsetfield').eq(0);
var dialog = field.data('itemsetfield-dialog');
if (!dialog) {
dialog = $('<div class="itemsetfield-dialog"></div>')
.appendTo('body')
.dialog({
autoOpen: false,
title: title,
modal: true,
width: parseInt(field.attr("data-popup-width")),
height: parseInt(field.attr("data-popup-height")),
draggable: true,
resizable: false
});
field.data('itemsetfield-dialog', dialog);
} else {
dialog.dialog('option', 'title', title);
}
return dialog;
}
/**
* Handles the submission of a form in an item set field dialog.
*/
var formHandler = function() {
var form = $(this);
var actions = $('input.action', form).eq(0);
var text = actions.text();
actions.val(ss.i18n._t('ItemSetField.LOADING', 'Loading...'));
actions.attr('disabled', true);
var params = {
data: form.serialize(),
url: form.attr('action'),
type: form.attr('method') || 'GET'
};
request(form, text, params, function() {
actions.text(text).removeAttr('disabled');
});
return false;
};
$('.itemsetfield-sortable').live('hover', function() {
$('.itemsetfield-items', this).sortable({
axis: 'y',
containment: this,
opacity: .7
});
});
$('a.itemsetfield-action').live('click', function() {
var link = $(this);
var text = link.text();
if (link.hasClass('ui-state-disabled')) {
return false;
}
if (link.hasClass("ss-itemsetfield-confirmed")) {
if (!confirm(ss.i18n._t("ItemSetField.AREYOUSURE", "Are you sure?"))) {
return false;
}
}
link.text(ss.i18n._t('ItemSetField.LOADING', 'Loading...'));
link.addClass('ui-state-disabled');
request(link, text, { url: $(this).attr('href') }, function() {
link.text(text).removeClass('ui-state-disabled');
});
return false;
});
$('.itemsetfield-dialog form').live('reset', function() {
$('input, select', this)
.not('.action, :hidden')
.val('')
.removeAttr('checked').removeAttr('selected');
});
})(jQuery);