From 021e508ca957a258f10ce13721164f4446c136a9 Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Tue, 5 Jun 2012 11:30:13 +1200 Subject: [PATCH] ENHANCEMENT: refactor the IFramePage, add tests --- code/IFramePage.php | 87 ++++++++++++++++++++++++---------- javascript/iframe_page.js | 34 +++++++++++++ templates/Layout/IFramePage.ss | 7 ++- tests/IFramePageTest.php | 36 ++++++++++++++ 4 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 javascript/iframe_page.js create mode 100644 tests/IFramePageTest.php diff --git a/code/IFramePage.php b/code/IFramePage.php index 36a30ff..e2826d0 100644 --- a/code/IFramePage.php +++ b/code/IFramePage.php @@ -1,49 +1,86 @@ 'Text', - 'DynamicHeight' => 'Boolean', - 'FixedHeight' => 'Int', + 'IFrameURL' => 'Text', + 'AutoHeight' => 'Boolean(1)', + 'AutoWidth' => 'Boolean(1)', + 'FixedHeight' => 'Int(500)', + 'FixedWidth' => 'Int(0)', 'AlternateContent' => 'HTMLText', 'BottomContent' => 'HTMLText' ); + + static $defaults = array( + 'AutoHeight' => '1', + 'AutoWidth' => '1', + 'FixedHeight' => '500', + 'FixedWidth' => '0' + ); + + static $description = 'Embeds an iframe into the body of the page.'; function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeFieldFromTab('Root.Main', 'Content'); - $fields->addFieldToTab('Root.Main', new TextField('IFrameUrl', 'IFrame URL')); - $fields->addFieldToTab('Root.Main', new CheckboxField('DynamicHeight', 'Dynamically resize the IFrame height (this doesn\'t work if IFrame URL is on a different domain)')); - $fields->addFieldToTab('Root.Main', new NumericField('FixedHeight', 'Fixed Height (in pixels)')); - $fields->addFieldToTab('Root.Main', new HtmlEditorField('Content', 'Content (appears above IFrame)')); - $fields->addFieldToTab('Root.Main', new HtmlEditorField('BottomContent', 'Content (appears below IFrame)')); - $fields->addFieldToTab('Root.Main', new HtmlEditorField('AlternateContent', 'Alternate Content (appears when user has IFrames disabled)')); + $fields->addFieldToTab('Root.Main', $url = new TextField('IFrameURL', 'Iframe URL')); + $url->setRightTitle('Can be absolute (http://silverstripe.com) or relative to this site (about-us).'); + $fields->addFieldToTab('Root.Main', new CheckboxField('AutoHeight', 'Auto height (only works with same domain URLs)')); + $fields->addFieldToTab('Root.Main', new CheckboxField('AutoWidth', 'Auto width (100% of the available space)')); + $fields->addFieldToTab('Root.Main', new NumericField('FixedHeight', 'Fixed height (in pixels)')); + $fields->addFieldToTab('Root.Main', new NumericField('FixedWidth', 'Fixed width (in pixels)')); + $fields->addFieldToTab('Root.Main', new HtmlEditorField('Content', 'Content (appears above iframe)')); + $fields->addFieldToTab('Root.Main', new HtmlEditorField('BottomContent', 'Content (appears below iframe)')); + $fields->addFieldToTab('Root.Main', new HtmlEditorField('AlternateContent', 'Alternate Content (appears when user has iframes disabled)')); return $fields; } + + /** + * Compute class from the size parameters. + */ + function getClass() { + $class = ''; + if ($this->AutoHeight) { + $class .= 'iframepage-height-auto'; + } + + return $class; + } + + /** + * Compute style from the size parameters. + */ + function getStyle() { + $style = ''; + + // Always add fixed height as a fallback if autosetting or JS fails. + $height = $this->FixedHeight; + if (!$height) $height = 800; + $style .= "height: {$height}px; "; + + if ($this->AutoWidth) { + $style .= "width: 100%; "; + } + else if ($this->FixedWidth) { + $style .= "width: {$this->FixedWidth}px; "; + } + + return $style; + } } class IFramePage_Controller extends Page_Controller { function init() { parent::init(); - if ($this->DynamicHeight) { - Requirements::customScript(<<