Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Louis Ferreira 69 posts 265 karma points
    Jul 26, 2018 @ 15:13
    Louis Ferreira
    0

    GridLayout custom editor

    Hi,

    I am creating a custom editor for the grid layout control in that I want to apply Padding and Margin to the columns and cells. I have the editor working - both saving and loading the model.value... so all is good in the back-office. What I need now is to figure out how to apply these settings to the font-end.

    My settings are as follows:

    {
        "label": "Padding",
        "description": "Specify the padding for this component.",
        "key": "styles",
        "modifier": "padding: {0}",
        "view": "/App_Plugins/GridSettingsPaddingEditor/view.html",
        "defaultConfig": {
          "padding": {
            "top": {
              "icon": "icon-arrow-up",
              "text": "Top",
              "value": "0"
            },
            "right": {
              "icon": "icon-arrow-right",
              "text": "Right",
              "value": "0"
            },
            "bottom": {
              "icon": "icon-arrow-down",
              "text": "Bottom",
              "value": "0"
            },
            "left": {
              "icon": "icon-arrow-left",
              "text": "Left",
              "value": "0"
            }
          }
        }
      }
    

    The editor looks like this:

    enter image description here

    Now when it comes to rendering the markup, it does some weird stuff: enter image description here

    So, my question is: how can I get it to add the correct value to the styles attribute, and how do I go about adding multiple values into the style attribute (I'd like to also create and add a 'Margin' Editor later)

    Cheers

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 26, 2018 @ 19:49
    Søren Gregersen
    0

    first of all, why not create the 4 inputs in a grid? inspiration: https://codepen.io/radibit/pen/LVzgvP

    https://our.umbraco.com/documentation/getting-started/backoffice/property-editors/built-in-property-editors/grid-layout/settings-and-styles

    The documentation says you don't need the key attributes - that is what cause your padding not to be applied. The next thing is that you need to save the unit also (the px).

  • Louis Ferreira 69 posts 265 karma points
    Jul 27, 2018 @ 06:00
    Louis Ferreira
    0

    Hi Søren,

    Thanks for reply. Not really wanting to use flexbox as editors use OSx & iOS for the back-office, and support in Safari is not great.

    The documentation is not really clear about how attributes and keys are applied. I did read through it and to my understanding of the documentation it was that the key becomes the attribute... hence why I used 'style' for the key.

    "key defines the alias the configuration is stored under and by default the alias of the attribute will also be the attribute on the rendered html element."

    And with regards to saving the model.value with 'px'... yeah, I already got that :)

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jul 27, 2018 @ 07:39
    Søren Gregersen
    100

    Not really wanting to use flexbox as editors use OSx & iOS for the back-office

    The backoffice uses flexbox all over the place. All browsers, except IE, has full support for flexbox :)

    Have a look at how the grid is rendered: https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web.UI/Views/Partials/Grid/Bootstrap3.cshtml

    Your key should be the css-property you want to set, in this case "padding". The modifier should not be needed (had it the other way round in my last reply).

  • Louis Ferreira 69 posts 265 karma points
    Jul 28, 2018 @ 05:44
    Louis Ferreira
    0

    Hi Søren,

    About 30 mins after my last post, I actually discovered that file in the project, and once I spotted the helper functions that render the attribute, it all made perfect sense! So your last answer was spot on :)

    I got the editor working great now. I even went on to create a back ground editor that allows the user to change settings like background image, position, colour, crop, width, colour, etc... very cool extensiblity point!

    I will post my solution here shortly to help the community incase there are others wanting to do the same/similar thing.

  • Louis Ferreira 69 posts 265 karma points
    Jul 28, 2018 @ 06:27
    Louis Ferreira
    1

    For anyone interested in how to do this...

    Create the plugin folder & files as follows: App plugins folder

    Next, create a new data type from the Grid Property editor in developer section, then click the edit link below 'Styles': enter image description here

    Add the following snippet in the dialogue, and save it...

     {
        "label": "Padding",
        "description": "Specify the padding for this component.",
        "key": "padding",
        "view": "/App_Plugins/GridSettingsPaddingEditor/view.html",
        "defaultConfig": {
          "padding": {
            "top": {
              "icon": "icon-arrow-up",
              "text": "Top",
              "value": 0
            },
            "right": {
              "icon": "icon-arrow-right",
              "text": "Right",
              "value": 0
            },
            "bottom": {
              "icon": "icon-arrow-down",
              "text": "Bottom",
              "value": 0
            },
            "left": {
              "icon": "icon-arrow-left",
              "text": "Left",
              "value": 0
            }
          }
        }
      }
    

    Next, we add the controller code to the gridSettingsPaddingEditor.controller.js file:

    angular.module('umbraco').controller('grid.settings.paddingeditor.controller', 
        function ($scope, $http, $routeParams, assetsService, contentResource, notificationsService) {
    
            assetsService.loadCss('/App_Plugins/GridSettingsPaddingEditor/styles.css');
            $scope.padding = $scope.model.defaultConfig ? $scope.model.defaultConfig.padding : $scope.model.config.padding;
    
    
    
        if ($scope.model.value) {
            var tempValues = '0 0 0 0';
            if ($scope.model.value.length === 0) {
                $scope.model.value = tempValues;
            } else {
                tempValues = $scope.model.value.replace(/px/g, '');
            }
            var values = tempValues.split(' ');
    
            $scope.padding.top.value = isNaN(values[0]) ? 0 : Number(values[0]);
            $scope.padding.right.value = isNaN(values[1]) ? 0 : Number(values[1]);
            $scope.padding.bottom.value = isNaN(values[2]) ? 0 : Number(values[2]);
            $scope.padding.left.value = isNaN(values[3]) ? 0 : Number(values[3]);
    
        }
    
    
    
        $scope.change = function () {
            $scope.model.value = '';
            if ($scope.padding.top.value === 0 && $scope.padding.right.value === 0 && $scope.padding.bottom.value === 0 && $scope.padding.left.value === 0) {
                return;
            } else {
                $scope.model.value = 
                    $scope.padding.top.value + 'px ' + 
                    $scope.padding.right.value + 'px ' + 
                    $scope.padding.bottom.value + 'px ' + 
                    $scope.padding.left.value + 'px';
            }
        };
    });
    

    Then, we code up the view....

    <div ng-controller="grid.settings.paddingeditor.controller">
    
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td style="text-align: right; padding-right: 20px;">
                    <label>
                        <i class="{{padding.top.icon}}" ng-show="padding.top.icon"></i>{{padding.top.text}}
                        <input style="width: 60px;" type="number" ng-model="padding.top.value" ng-change="change()"/>px
                    </label>
    
                </td>
                <td style="text-align:right">
                    <label>
                        <i class="{{padding.right.icon}}" ng-show="padding.right.icon"></i>{{padding.right.text}}
                        <input style="width: 60px;" type="number" ng-model="padding.right.value" ng-change="change()"/>px
                    </label>
    
                </td>
            </tr>
            <tr>
                <td style="text-align: right; padding-right: 20px;">
                    <label>
                        <i class="{{padding.bottom.icon}}" ng-show="padding.bottom.icon"></i>{{padding.bottom.text}}
                        <input style="width: 60px;" type="number" ng-model="padding.bottom.value" ng-change="change()"/>px
                    </label>
    
                </td>
                <td style="text-align:right">
                    <label>
                        <i class="{{padding.left.icon}}" ng-show="padding.left.icon"></i>{{padding.left.text}}
                        <input style="width: 60px;" type="number" ng-model="padding.left.value" ng-change="change()"/>px
                    </label>
    
                </td>
            </tr>
        </table>
    
    </div>
    

    Finally, we fillout the package.manisfest file...

    {
      "gridEditors": [
        {
          "name": "Grid Settings Padding Editor",
          "alias": "gridSettingsPaddingEditor",
          "view": "~/App_Plugins/GridSettingsPaddingEditor/view.html",
          "icon": "",
          "editor": {
            "valueType": "string",
            "view": "~/App_Plugins/GridSettingsPaddingEditor/view.html"
          }
        }
      ],
      "javascript": [
        "~/App_Plugins/GridSettingsPaddingEditor/gridsettingspaddingeditor.controller.js"
      ],
      "css": [
        "~/App_Plugins/GridSettingsPaddingEditor/styles.css"
      ]
    }
    

    Now you can add your new grid layout data type to your doc type, and after clicking the gear in the top right of the row/column, you will see your new settings editor....

    enter image description here enter image description here

    And the rendered output looks like this... enter image description here

    I get that the view could be done better (not using tables... I know, I was pressed for time) Hope this helps others too.

    .

    .

    (For SEO)

    Custom Grid Editor

    Multiple Style editors

Please Sign in or register to post replies

Write your reply to:

Draft