Copied to clipboard

Flag this post as spam?

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


  • Nik 1611 posts 7237 karma points MVP 7x c-trib
    Jun 02, 2016 @ 16:14
    Nik
    0

    Custom property editor - That's not really an editor!

    Hi All,

    So, I'm trying to build what is effectively a custom property editor that doesn't save it's information in the related document.

    In simple, I have a document type "My Doc Type". It has a tab "Announcements"

    On said tab, I am putting a custom property editor. The editor is available once the document is saved. It has several fields (title [text], message [rte] and duration [ numeric]) and a custom "post" button.

    The post button sends the information along with the id of the page to a custom api controller (well it will when I get to that last stage).

    However, after the post event has run, I want to reset the fields back to default values.

    This is working for the title filed and the duration filed, however the message filed isn't resetting in the RTE on screen.

    My editor view:

    <div ng-controller="My.CustomEditorController">
        <div ng-if="isNew">
            <p>Announcements are only available once the page has been saved.</p>
        </div>
        <div ng-if="isNew == false">
            <ng-form class="row-fluid">
                <div class="span7">
                    <div class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label">Title</label>
                            <div class="controls">
                                <input class="umb-editor umb-textstring textstring" type="text" ng-model="announcement.title.value"/>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Message</label>
    
                            <div class="controls">
                                <umb-editor model="messageBodyEditor"></umb-editor>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Duration (minutes)</label>
    
                            <div class="controls">
                                <input name="integerField" type="number" class="umb-editor umb-number  ng-pristine ng-valid ng-valid-number" ng-model="announcement.duration.value" val-server="value" fix-number="" min="1" max="1440" step="1">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="span4">
                    <a class="btn btn-success" href ng-click="postAnnouncement(announcement)">Click to post</a>
                </div>
            </ng-form>
    
            <div class="span8">
                <table class="table table-condensed">
                    <thead>
                    <tr>
                        <th style="width: 35px"></th>
                        <th>Title</th>
                        <th style="width: 100px">Expires at</th>
    
                    </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="a in announcements" ng-class="a.exp.getTime() < clock.getTime() ? 'expired' : ''">
                            <td class="text-center"><i class="icon icon-megaphone"></i></td>
                            <td>{{a.title}}</td>
                            <td ng-if="a.exp.getTime() < clock.getTime()">Expired</td>
                            <td ng-if="a.exp.getTime() > clock.getTime()">
                                {{a.exp | date:'HH:mm'}}
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
    
        </div>
    </div>
    

    My controller code:

    angular.module("umbraco")
        .controller("My.CustomEditorController",
            function($scope, $routeParams, editorState, $timeout) {
                $scope.isNew = editorState.current.id === 0;
                $scope.announcement = {};
                $scope.announcement.messageBody = "";
                $scope.announcement.title = { value: "" };
                $scope.announcement.duration = { value: 60 };
    
                $scope.messageBodyEditor ={
                    label: 'bodyText',
                    description: 'Load some stuff here',
                    view: 'rte',
                    value: $scope.announcement.messageBody,
                    config: {
                        editor: {
                                toolbar: ["undo", "redo", "cut", "bold", "italic", "link"],
                                stylesheets: [],
                                dimensions: { height: 150, width: 450 }
                        }
                    }
                };
    
                //sample information
                $scope.announcements = [
                    {
                        title: "test",
                        exp: new Date(2016, 5, 2, 12, 46)
                    },
                    {
                        title: "test2",
                        exp: new Date(2016, 5, 2, 13, 46)
                    },
                    {
                        title: "test3",
                        exp: new Date(2016, 5, 2, 15, 30)
                    }
                ];
    
                $scope.postAnnouncement = function (testProperty) {
                    if (testProperty.duration.value == undefined || testProperty.duration.value <= 0)
                        testProperty.duration.value = 60;
                    $scope.announcements.push({
                        title: testProperty.title.value,
                        exp: new Date(Date.now() + testProperty.duration.value*60000)
                    });
                    console.log(testProperty);
    
                    $scope.announcement.title.value = "";
                    $scope.announcement.duration.value = 60;
                    ///This doesn't reset the value in the RTE ?!?!
                    $scope.announcement.messageBody = "";
                    $scope.messageBodyEditor.value = "";
                    //$scope.announcement.messageBody.value = "";
    
                };
    
                $scope.tickInterval = 1000; //ms
    
                var tick = function () {
                    $scope.clock = new Date; // get the current time
                    $timeout(tick, $scope.tickInterval); // reset the timer
                }
    
                // Start the timer
                $timeout(tick, $scope.tickInterval);
    
    
    
                $scope.$watch('messageBodyEditor.value', function (newValue, oldValue) {
    
                    $scope.announcement.messageBody = newValue;
                });
    
            });
    

    My reset code exists in this function: $scope.postAnnouncement

    Yet, for some reason it doesn't impact the RTE. I've tried adding a watch a watch as per information on : http://tooorangey.co.uk/posts/editor-notes-for-umbraco-7/ and http://24days.in/umbraco/2014/umbraco-angular-tips/ with no effect.

    I don't want the editor to have to "save" the page each time in order to post the announcement as this are short lived items and aren't document types. (I could do the whole thing with document types, but I think that would be inefficient).

    Does anyone have any ideas/suggestions as to what I might be doing wrong/am missing?

    Nik

  • Nik 1611 posts 7237 karma points MVP 7x c-trib
    Jun 03, 2016 @ 14:00
    Nik
    100

    For anyone that is interested, the way I've had to go about this is (in my opinion) a bit of a hack. Saying that it is similar to how the RTE PE works in the core.

    Step 1)

    set an alias in the RTE configuration:

    I did the following to make it unique:

    var d = new Date();
    var n = d.getTime();
    var rteAlias = 'announcement_' + n;
    

    Step 2)

    In my post announcement method where I want to do the reset the value I've had to do the following:

    if (tinyMCE !== undefined) {
         tinyMCE.editors.forEach(function(item, index) {
             if (item.id.startsWith(rteAlias)) {
                 item.setContent('');
                 item.fire('LoadContent', null);
             }
        });
    }
    

    Effectively, check if tinyMCE has been loaded into the page. If it has, for each editor that exists in tinyMCE, check to see if its id starts with your alias (hence the reason for making it unique). If it does, call setConent and fire the LoadContent event.

    I've had to take this approach as I couldn't find a way to obtain the id of the RTE that Umbraco generates.

Please Sign in or register to post replies

Write your reply to:

Draft