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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
elgg.provide('elgg.ui.widgets');
/**
* Widgets initialization
*
* @return void
*/
elgg.ui.widgets.init = function() {
// widget layout?
if ($(".widget_column").length == 0) {
return;
}
$(".widget_column").sortable({
items: 'div.widget',
connectWith: '.widget_column',
handle: 'div.drag_handle',
forcePlaceholderSize: true,
placeholder: 'widget_placeholder',
opacity: 0.8,
revert: 500,
stop: elgg.ui.widgets.move
});
$('#widget_add_button a').bind('click', function(event) {
$('.widgets_add_panel').slideToggle('medium');
event.preventDefault();
});
$('.widgets_add_panel li.widget_available').click(elgg.ui.widgets.add);
$('a.widget_delete_button').click(elgg.ui.widgets.remove);
$('a.widget_edit_button').click(elgg.ui.widgets.editToggle);
$('.widget_edit > form ').submit(elgg.ui.widgets.saveSettings);
$('a.widget_collapse_button').click(elgg.ui.widgets.collapseToggle);
elgg.ui.widgets.equalHeight(".widget_column");
};
/**
* Adds a new widget
*
* Makes Ajax call to persist new widget and inserts the widget html
*
* @param {Object} event
* @return void
*/
elgg.ui.widgets.add = function(event) {
// widget_type_<type>
var type = $(this).attr('id');
type = type.substr(type.indexOf('widget_type_') + "widget_type_".length);
// if multiple instances not allow, disable this widget type add button
var multiple = $(this).attr('class').indexOf('widget_multiple') != -1;
if (multiple == false) {
$(this).addClass('widget_unavailable');
$(this).removeClass('widget_available');
$(this).unbind('click', elgg.ui.widgets.add);
}
elgg.action('widgets/add', {
data: {
handler: type,
user_guid: elgg.get_loggedin_userid(),
context: $("input[name='widget_context']").val()
},
success: function(json) {
$('#widget_col_1').prepend(json.output);
var $widget = $('#widget_col_1').children(":first");
$widget.find('a.widget_delete_button').click(elgg.ui.widgets.remove);
$widget.find('a.widget_edit_button').click(elgg.ui.widgets.editToggle);
$widget.find('a.widget_collapse_button').click(elgg.ui.widgets.collapseToggle);
$widget.find('.widget_edit > form ').submit(elgg.ui.widgets.saveSettings);
}
});
event.preventDefault();
}
/**
* Persist the widget's new position
*
* @param {Object} event
* @param {Object} ui
*
* @return void
*/
elgg.ui.widgets.move = function(event, ui) {
// widget_<guid>
var guidString = ui.item.attr('id');
guidString = guidString.substr(guidString.indexOf('widget_') + "widget_".length);
// widget_col_<column>
var col = ui.item.parent().attr('id');
col = col.substr(col.indexOf('widget_col_') + "widget_col_".length);
elgg.action('widgets/move', {
data: {
guid: guidString,
column: col,
position: ui.item.index()
}
});
// @hack fixes jquery-ui/opera bug where draggable elements jump
ui.item.css('top', 0);
ui.item.css('left', 0);
}
/**
* Removes a widget from the layout
*
* Event callback the uses Ajax to delete the widget and removes its HTML
*
* @param {Object} event
* @return void
*/
elgg.ui.widgets.remove = function(event) {
var $widget = $(this).parent().parent();
// if widget type is single instance type, enable the add buton
var type = $widget.attr('class');
// widget_instance_<type>
type = type.substr(type.indexOf('widget_instance_') + "widget_instance_".length);
$button = $('#widget_type_' + type);
var multiple = $button.attr('class').indexOf('widget_multiple') != -1;
if (multiple == false) {
$button.addClass('widget_available');
$button.removeClass('widget_unavailable');
$button.unbind('click', elgg.ui.widgets.add); // make sure we don't bind twice
$button.click(elgg.ui.widgets.add);
}
$widget.remove();
// widget_delete_button_<guid>
var id = $(this).attr('id');
id = id.substr(id.indexOf('widget_delete_button_') + "widget_delete_button_".length);
elgg.action('widgets/delete', {
data: {
guid: id
}
});
event.preventDefault();
}
/**
* Toggle the edit panel of a widget
*
* Yes, I'm quite bad at selectors.
*
* @param {Object} event
* @return void
*/
elgg.ui.widgets.editToggle = function(event) {
$(this).parent().parent().find('.widget_edit').slideToggle('medium');
event.preventDefault();
}
/**
* Toogle the collapse state of the widget
*
* @param {Object} event
* @return void
*/
elgg.ui.widgets.collapseToggle = function(event) {
$(this).toggleClass('widget_collapsed');
$(this).parent().parent().find('.widget_container').slideToggle('medium');
event.preventDefault();
}
/**
* Save a widget's settings
*
* Uses Ajax to save the settings and updates the HTML.
*
* @param {Object} event
* @return void
*/
elgg.ui.widgets.saveSettings = function(event) {
$(this).parent().slideToggle('medium');
var $widgetContent = $(this).parent().parent().children('.widget_content');
// @todo - change to ajax loader
$widgetContent.html('loading');
elgg.action('widgets/save', {
data: $(this).serialize(),
success: function(json) {
$widgetContent.html(json.output);
}
});
event.preventDefault();
}
/**
* Make all elements have the same min-height
*
* This addresses the issue of trying to drag a widget into a column that does
* not have any widgets.
*
* @param {String} selector
* @return void
*/
elgg.ui.widgets.equalHeight = function(selector) {
var maxHeight = 0;
$(selector).each(function() {
if ($(this).height() > maxHeight) {
maxHeight = $(this).height();
}
})
$(selector).css('min-height', maxHeight);
}
elgg.register_event_handler('init', 'system', elgg.ui.widgets.init);
// @todo look into removing the below functions - maybe a compatibility plugin
//List active widgets for each page column
elgg.ui.widgets.outputList = function(forElement) {
return $("input[name='handler'], input[name='guid']", forElement).makeDelimitedList("value");
};
//Read each widgets collapsed/expanded state from cookie and apply
elgg.ui.widgets.state = function(forWidget) {
var thisWidgetState = elgg.session.cookie(forWidget);
if (thisWidgetState === 'collapsed') {
forWidget = "#" + forWidget;
$(forWidget).find("div.collapsable_box_content").hide();
$(forWidget).find("a.toggle_box_contents").html('+');
$(forWidget).find("a.toggle_box_edit_panel").fadeOut('medium');
}
};
//More info tooltip in widget gallery edit panel
elgg.ui.widgets.moreinfo = function() {
$("img.more_info").hover(function(e) {
var widgetdescription = $("input[name='description']", this.parentNode.parentNode.parentNode).val();
$("body").append("<p id='widget_moreinfo'><b>" + widgetdescription + " </b></p>");
if (e.pageX < 900) {
$("#widget_moreinfo")
.css("top", (e.pageY + 10) + "px")
.css("left", (e.pageX + 10) + "px")
.fadeIn("medium");
} else {
$("#widget_moreinfo")
.css("top", (e.pageY + 10) + "px")
.css("left", (e.pageX - 210) + "px")
.fadeIn("medium");
}
}, function() {
$("#widget_moreinfo").remove();
});
};
//Toggle widgets contents and save to a cookie
elgg.ui.widgets.toggleContent = function(e) {
var thisWidgetName,
targetContent = $('div.collapsable_box_content', this.parentNode.parentNode);
if (targetContent.css('display') === 'none') {
targetContent.slideDown(400);
$(this).html('-');
$(this.parentNode).children(".toggle_box_edit_panel").fadeIn('medium');
// set cookie for widget panel open-state
thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
elgg.session.cookie(thisWidgetName, 'expanded', {expires: 365});
} else {
targetContent.slideUp(400);
$(this).html('+');
$(this.parentNode).children(".toggle_box_edit_panel").fadeOut('medium');
// make sure edit pane is closed
$(this.parentNode.parentNode).children(".collapsable_box_editpanel").hide();
// set cookie for widget panel closed-state
thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
elgg.session.cookie(thisWidgetName, 'collapsed', {expires: 365});
}
return false;
};
/**
* @deprecated Use elgg.ui.widgets.*
*/
var toggleContent = elgg.ui.widgets.toggleContent,
widget_moreinfo = elgg.ui.widgets.moreinfo,
widget_state = elgg.ui.widgets.state,
outputWidgetList = elgg.ui.widgets.outputList;
|