Skip to content
Browse files

BUG Fixing ToggleField to work correctly with jQuery

  • Loading branch information...
1 parent 77337ae commit 4651e9bbc91c0b540c25b26e09da777dd9e733a6 @halkyon halkyon committed
Showing with 15 additions and 37 deletions.
  1. +5 −4 forms/ToggleField.php
  2. +10 −33 javascript/ToggleField.js
View
9 forms/ToggleField.php
@@ -2,6 +2,8 @@
/**
* ReadonlyField with added toggle-capabilities - will preview the first sentence of the contained text-value,
* and show the full content by a javascript-switch.
+ *
+ * @deprecated 3.1 Use custom javascript with a ReadonlyField.
*
* Caution: Strips HTML-encoding for the preview.
* @package forms
@@ -48,9 +50,8 @@ public function __construct($name, $title = "", $value = "") {
public function Field($properties = array()) {
$content = '';
-
- Requirements::javascript(FRAMEWORK_DIR . "/thirdparty/prototype/prototype.js");
- Requirements::javascript(FRAMEWORK_DIR . "/thirdparty/behaviour/behaviour.js");
+
+ Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript(FRAMEWORK_DIR . "/javascript/ToggleField.js");
if($this->startClosed) $this->addExtraClass('startClosed');
@@ -60,7 +61,7 @@ public function Field($properties = array()) {
if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum);
else $reducedVal = DBField::create_field('Text',$rawInput)->{$this->truncateMethod}();
-
+
// only create togglefield if the truncated content is shorter
if(strlen($reducedVal) < strlen($rawInput)) {
$content = <<<HTML
View
43 javascript/ToggleField.js
@@ -1,35 +1,12 @@
-// Shortcut-function (until we update to Prototye v1.5)
-if(typeof $$ != "Function") $$ = document.getElementsBySelector;
+var field = $('div.toggleField');
-var ToggleField = Class.create();
-ToggleField.prototype = {
- initialize: function() {
- var rules = {};
- rules['#' + this.id + ' .triggerMore'] = {
- onclick: function(e) {
- Element.toggle(this);
- Event.stop(e); return false;
- }.bind(this)
- };
- rules['#' + this.id + ' .triggerLess'] = {
- onclick: function(e) {
- Element.toggle(this);
- Event.stop(e); return false;
- }.bind(this)
- };
- Behaviour.register(rules);
-
- if(Element.hasClassName(this, 'startClosed')) {
- Element.toggle(this);
- }
- },
-
- toggle: function() {
- var lessDivs = $$('#' + this.id + ' .contentLess');
- if(lessDivs) Element.toggle(lessDivs[0]);
-
- var moreDivs = $$('#' + this.id + ' .contentMore');
- if(moreDivs) Element.toggle(moreDivs[0]);
- }
+if(field.hasClass('startClosed')) {
+ field.find('div.contentMore').hide();
+ field.find('div.contentLess').show();
}
-ToggleField.applyTo('div.toggleField');
+
+$('div.toggleField .triggerLess, div.toggleField .triggerMore').click(function() {
+ field.find('div.contentMore').toggle();
+ field.find('div.contentLess').toggle();
+});
+

0 comments on commit 4651e9b

Please sign in to comment.
Something went wrong with that request. Please try again.