Skip to content

Commit 4651e9b

Browse files
author
Sean Harvey
committed
BUG Fixing ToggleField to work correctly with jQuery
1 parent 77337ae commit 4651e9b

File tree

2 files changed

+15
-37
lines changed

2 files changed

+15
-37
lines changed

forms/ToggleField.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/**
33
* ReadonlyField with added toggle-capabilities - will preview the first sentence of the contained text-value,
44
* and show the full content by a javascript-switch.
5+
*
6+
* @deprecated 3.1 Use custom javascript with a ReadonlyField.
57
*
68
* Caution: Strips HTML-encoding for the preview.
79
* @package forms
@@ -48,9 +50,8 @@ public function __construct($name, $title = "", $value = "") {
4850

4951
public function Field($properties = array()) {
5052
$content = '';
51-
52-
Requirements::javascript(FRAMEWORK_DIR . "/thirdparty/prototype/prototype.js");
53-
Requirements::javascript(FRAMEWORK_DIR . "/thirdparty/behaviour/behaviour.js");
53+
54+
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
5455
Requirements::javascript(FRAMEWORK_DIR . "/javascript/ToggleField.js");
5556

5657
if($this->startClosed) $this->addExtraClass('startClosed');
@@ -60,7 +61,7 @@ public function Field($properties = array()) {
6061

6162
if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum);
6263
else $reducedVal = DBField::create_field('Text',$rawInput)->{$this->truncateMethod}();
63-
64+
6465
// only create togglefield if the truncated content is shorter
6566
if(strlen($reducedVal) < strlen($rawInput)) {
6667
$content = <<<HTML

javascript/ToggleField.js

+10-33
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
1-
// Shortcut-function (until we update to Prototye v1.5)
2-
if(typeof $$ != "Function") $$ = document.getElementsBySelector;
1+
var field = $('div.toggleField');
32

4-
var ToggleField = Class.create();
5-
ToggleField.prototype = {
6-
initialize: function() {
7-
var rules = {};
8-
rules['#' + this.id + ' .triggerMore'] = {
9-
onclick: function(e) {
10-
Element.toggle(this);
11-
Event.stop(e); return false;
12-
}.bind(this)
13-
};
14-
rules['#' + this.id + ' .triggerLess'] = {
15-
onclick: function(e) {
16-
Element.toggle(this);
17-
Event.stop(e); return false;
18-
}.bind(this)
19-
};
20-
Behaviour.register(rules);
21-
22-
if(Element.hasClassName(this, 'startClosed')) {
23-
Element.toggle(this);
24-
}
25-
},
26-
27-
toggle: function() {
28-
var lessDivs = $$('#' + this.id + ' .contentLess');
29-
if(lessDivs) Element.toggle(lessDivs[0]);
30-
31-
var moreDivs = $$('#' + this.id + ' .contentMore');
32-
if(moreDivs) Element.toggle(moreDivs[0]);
33-
}
3+
if(field.hasClass('startClosed')) {
4+
field.find('div.contentMore').hide();
5+
field.find('div.contentLess').show();
346
}
35-
ToggleField.applyTo('div.toggleField');
7+
8+
$('div.toggleField .triggerLess, div.toggleField .triggerMore').click(function() {
9+
field.find('div.contentMore').toggle();
10+
field.find('div.contentLess').toggle();
11+
});
12+

0 commit comments

Comments
 (0)