Skip to content

Commit

Permalink
Merge branch 'feature/ui' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
flxqr2 committed Mar 5, 2018
2 parents 9720845 + b3209ad commit 32f54dd
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 176 deletions.
102 changes: 86 additions & 16 deletions code/GridFieldPageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class GridFieldPageSectionsExtension implements
"handleRemove",
"handleDelete",
"handleReorder",
"handleMoveToPage"
);


Expand All @@ -38,7 +37,6 @@ public function getURLHandlers($grid) {
"POST remove" => "handleRemove",
"POST delete" => "handleDelete",
"POST reorder" => "handleReorder",
"POST movetopage" => "handleMoveToPage"
);
}

Expand All @@ -50,6 +48,7 @@ public function getHTMLFragments($field) {
$moduleDir = self::getModuleDir();
Requirements::css($moduleDir . "/css/GridFieldPageSectionsExtension.css");
Requirements::javascript($moduleDir . "/javascript/GridFieldPageSectionsExtension.js");
Requirements::add_i18n_javascript($moduleDir . '/javascript/lang', false, true);

$id = rand(1000000, 9999999);
$field->addExtraClass("ss-gridfield-pagesections");
Expand All @@ -73,20 +72,20 @@ public function augmentColumns($gridField, &$columns) {
}

if (!in_array("Actions", $columns)) {
array_push($columns, "Actions");
array_splice($columns, 2, 0, "Actions");
}

// Insert grid state initial data
$state = $gridField->getState();
if (!isset($state->open)) {
$state->open = array();

// Open all elements by default
// Open all elements by default if has children
$list = array();
$newList = $gridField->getManipulatedList();
while (count($list) < count($newList)) {
foreach ($newList as $item) {
if ($item->isOpenByDefault()) {
if ($item->isOpenByDefault() && $item->Children()->Count()) {
$this->openElement($state, $item);
}
}
Expand All @@ -99,8 +98,8 @@ public function augmentColumns($gridField, &$columns) {
public function getColumnsHandled($gridField) {
return array(
"Reorder",
"TreeNav",
"Actions",
"TreeNav",
);
}

Expand All @@ -125,12 +124,23 @@ public function getColumnAttributes($gridField, $record, $columnName) {
$elems[$class] = $class::$singular_name;
}

// if element has no parent we need to
// know the allowed elements of the page
if (!$record->_Parent) {
$parentClasses = $this->page->getAllowedPageElements();
$parentElems = array();
foreach ($parentClasses as $class) {
$parentElems[$class] = $class::$singular_name;
}
}

return array(
"class" => "col-treenav",
"data-class" => $record->ClassName,
"data-level" => strval($record->_Level),
"data-parent" => $record->_Parent ? strval($record->_Parent->ID) : "",
"data-allowed-elements" => json_encode($elems, JSON_UNESCAPED_UNICODE),
"class" => "col-treenav",
"data-class" => $record->ClassName,
"data-level" => strval($record->_Level),
"data-parent" => $record->_Parent ? strval($record->_Parent->ID) : "",
"data-allowed-parent-elements" => !$record->_Parent ? json_encode($parentElems, JSON_UNESCAPED_UNICODE) : "",
"data-allowed-elements" => json_encode($elems, JSON_UNESCAPED_UNICODE),
);
}

Expand Down Expand Up @@ -158,10 +168,10 @@ public function getColumnContent($gridField, $record, $columnName) {
$field = null;

if ($record->Children() && $record->Children()->Count() > 0) {
$icon = ($open === true ? '<span class="is-open">▼</span>'
: '<span class="is-closed">▶</span>');
$icon = ($open === true ? '<svg width="9" height="7" xmlns="http://www.w3.org/2000/svg"><path d="M1.907.678h5.5a1 1 0 0 1 .817 1.576l-2.75 3.9a1 1 0 0 1-1.634 0l-2.75-3.9A1 1 0 0 1 1.907.678z" fill="#4A4A4A" fill-rule="evenodd"/></svg>'
: '<svg width="7" height="9" xmlns="http://www.w3.org/2000/svg"><path d="M.428 7.406V1.907a1 1 0 0 1 1.576-.817l3.9 2.75a1 1 0 0 1 0 1.634l-3.9 2.75a1 1 0 0 1-1.576-.818z" fill="#4A4A4A" fill-rule="evenodd"/></svg>');
} else {
$icon = '<span class="is-end">◼</span>';
$icon = '<svg width="6" height="6" xmlns="http://www.w3.org/2000/svg"><circle fill="#4A4A4A" cx="3" cy="3" r="3" fill-rule="evenodd"/></svg>';
}

$field = GridField_FormAction::create(
Expand All @@ -172,12 +182,19 @@ public function getColumnContent($gridField, $record, $columnName) {
array("element" => $record)
);
$field->addExtraClass("level".$level . ($open ? " is-open" : " is-closed"));
if (!$record->Children()->Count()) {
$field->addExtraClass(" is-end");
$field->setDisabled(true);
}
$field->setButtonContent($icon);
$field->setForm($gridField->getForm());

return ViewableData::create()->customise(array(
"ButtonField" => $field,
"Title" => $record->i18n_singular_name(),
"ID" => $record->ID,
"UsedCount" => $record->Parents()->Count() + $record->Pages()->Count(),
"ClassName" => $record->i18n_singular_name(),
"Title" => $record->Title,
))->renderWith("GridFieldPageElement");
}

Expand All @@ -191,7 +208,60 @@ public function getColumnContent($gridField, $record, $columnName) {
);
}
$link = Controller::join_links($gridField->link(), $link);
return "<a href='$link'>Edit</a>";
$data = new ArrayData(array(
'Link' => $link
));
$editButton = $data->renderWith('GridFieldEditButton');

$classes = $record->getAllowedPageElements();
$elems = array();
foreach ($classes as $class) {
$elems[$class] = $class::$singular_name;
}
$addButton = GridField_FormAction::create(
$gridField,
"AddAction".$record->ID,
null,
null,
null
);
$addButton->setAttribute("data-allowed-elements", json_encode($elems, JSON_UNESCAPED_UNICODE));
$addButton->addExtraClass("col-actions__button add-button");
if (!count($elems)) {
$addButton->setDisabled(true);
}
$addButton->setButtonContent('<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg">
<g fill="none" fill-rule="evenodd">
<circle fill="#488304" cx="8" cy="8" r="8"/>
<path d="M8 4v8M4 8h8" stroke="#FFF" stroke-width="2" stroke-linecap="square"/>
</g>
</svg>');

$deleteButton = GridField_FormAction::create(
$gridField,
"DeleteAction".$record->ID,
null,
null,
null
);
$deleteButton->setAttribute("data-used-count", $record->Parents()->Count() + $record->Pages()->Count());
$deleteButton->addExtraClass("col-actions__button delete-button");

$deleteButton->setButtonContent('<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg">
<g fill="none" fill-rule="evenodd">
<circle fill="#880919" cx="8" cy="8" r="8"/>
<path d="M4 8h8" stroke="#FFF" stroke-width="2" stroke-linecap="square"/>
</g>
</svg>');

return ViewableData::create()->customise(array(
"EditButton" => $editButton,
"AddButton" => $addButton,
"DeleteButton" => $deleteButton,
"ParentID" => $record->_Parent ? $record->_Parent->ID : $this->page->ID,
))->renderWith("GridFieldPageSectionsActionColumn");

return $ret;
}
}

Expand Down
6 changes: 4 additions & 2 deletions code/PageElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public function onBeforeWrite() {
}

public function getChildrenGridField() {
$allowedElements = $this->getAllowedPageElements();
$addNewButton = new GridFieldAddNewMultiClass();
$addNewButton->setClasses($this->getAllowedPageElements());
$addNewButton->setClasses($allowedElements);

$autoCompl = new GridFieldAddExistingAutocompleter('buttons-before-right');
$autoCompl->setResultsFormat('$Title ($ID)');
Expand All @@ -77,7 +78,8 @@ public function getChildrenGridField() {
->addComponent($autoCompl)
->addComponent($addNewButton)
->addComponent(new GridFieldPageSectionsExtension($this->owner))
->addComponent(new GridFieldDetailForm());
->addComponent(new GridFieldDetailForm())
->addComponent(new GridFieldFooter());
$dataColumns->setFieldCasting(array('GridFieldPreview' => 'HTMLText->RAW'));

return new GridField("Children", "Children", $this->Children(), $config);
Expand Down
8 changes: 5 additions & 3 deletions code/PageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function extraStatics($class = null, $extensionClass = null) {
return array();
}

public static function getAllowedPageElements() {
public static function getAllowedPageElements($sectionName = "Main") {
$classes = array_values(ClassInfo::subclassesFor("PageElement"));
$classes = array_diff($classes, ["PageElement"]);
return $classes;
Expand Down Expand Up @@ -61,8 +61,9 @@ public function updateCMSFields(FieldList $fields) {
$fields->removeByName($name);

if ($this->owner->ID) {
$allowedElements = $this->owner->getAllowedPageElements($section);
$addNewButton = new GridFieldAddNewMultiClass();
$addNewButton->setClasses($this->owner->getAllowedPageElements());
$addNewButton->setClasses($allowedElements);

$autoCompl = new GridFieldAddExistingAutocompleter('buttons-before-right');
$autoCompl->setResultsFormat('$Title ($ID)');
Expand All @@ -74,7 +75,8 @@ public function updateCMSFields(FieldList $fields) {
->addComponent($autoCompl)
->addComponent($addNewButton)
->addComponent(new GridFieldPageSectionsExtension($this->owner))
->addComponent(new GridFieldDetailForm());
->addComponent(new GridFieldDetailForm())
->addComponent(new GridFieldFooter());
$dataColumns = $config->getComponentByType('GridFieldDataColumns');
$dataColumns->setFieldCasting(array('GridFieldPreview' => 'HTMLText->RAW'));

Expand Down
Loading

0 comments on commit 32f54dd

Please sign in to comment.