Copied to clipboard

Flag this post as spam?

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


  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 11, 2015 @ 15:31
    Dave Woestenborghs
    0

    Intercepting unpublish button click

    Hi,

    For a package I'm developing I need to intercept the click on the unpublish button and display a custom notification to the user, letting them decide if they want to precede with the unpublish.

    I have following code that works :

    angular.module('umbraco').controller('Interceptor', function ($scope,notificationsService) {
    
        // get scope of content editing form
        var contentFormScope = angular.element('form[name=contentForm]').scope();
    
        // function to store old unpublish handler
        $scope.oldHanlder = function() {};
    
        // custom unpublish handler to display custom notification
        $scope.customUnpublish = function(oldHandler) {
            notificationsService.success('intercepted unpublish');
            $scope.oldHandler();
        };
    
        // function to intercept current unpublish action and replace it with our own
        $scope.InterceptUnPublish = function() {        
            for(var i=0;i<contentFormScope.subButtons.length;i++)    
            {
                console.log(contentFormScope.subButtons[i]);
    
                if(contentFormScope.subButtons[i].labelKey=='content_unPublish') {
                    $scope.oldHandler = contentFormScope.subButtons[i].handler;
                    contentFormScope.subButtons[i].handler = $scope.customUnpublish;
                }
            }
        };
    
    
        // put a watch on the buttons, to trigger the intercepting 
        contentFormScope.$watch('subButtons', function() {
            $scope.InterceptUnPublish();
        },true);     
    });

    This is triggered from a property editor.

    But I'm looking to find a cleaner solution. Maybe do the interception at a another level. Is this possible ?

    Dave

  • Pawel Bres 39 posts 160 karma points c-trib
    May 11, 2015 @ 19:12
    Pawel Bres
    2

    Hi Dave,

    I think you can overrinde performAction method from edit.controller.js

    You just need to keep reference to original performAction method e.g. like this

    angular.module('umbraco').controller('Interceptor', function ($scope,notificationsService) {
    
        // get scope of content editing form
        var contentFormScope = angular.element('form[name=contentForm]').scope();
    
        // function to store old unpublish handler
        $scope.parentPerformAction = $scope.performAction;
    
        // custom performAction to display custom notification
        $scope.performAction= function(btn) {
            if (btn.labelKey=='content_unPublish'){
                notificationsService.success('intercepted unpublish');
            }
            $scope.parentPerformAction.call(this,btn);
        };  
    });
    

    Check out simple example I've prepared http://plnkr.co/edit/ekPeQkq2g3b9YnWbWoaZ?p=preview

    Cheers

    Pawel

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 12, 2015 @ 08:16
    Dave Woestenborghs
    0

    Hi Pawel,

    I needed to modify your code a little bit to get it to work :

    angular.module('umbraco').controller('Interceptor', function ($scope,notificationsService) {
    
        // get scope of content editing form
        var contentFormScope = angular.element('form[name=contentForm]').scope();
    
        // function to store old unpublish handler
        $scope.parentPerformAction = contentFormScope.performAction;
    
        // custom performAction to display custom notification
        contentFormScope.performAction= function(btn) {
            if (btn.labelKey=='content_unPublish'){
                notificationsService.success('intercepted unpublish');
            }
            $scope.parentPerformAction.call(this,btn);
        };  
    
    
    
    });

     

    This is a bit cleaner than my code. But what I'm looking for is to intercept it at a higher level. But I think this needs core modifications.

    Now I have to create a property to a documenttype to get this to work. But I think it's the only way at the moment,

    Dave

  • Pawel Bres 39 posts 160 karma points c-trib
    May 12, 2015 @ 12:44
    Pawel Bres
    0

    Dave, you are right, now it's correct.

    I think it would be hard to intercept it on another level, but if you will find any way to do that please post it here :)

    Maybe it is possible to override API method and return info that there is confirmation needed, but I'm not sure if it is something you are aiming for.

    Cheers!

Please Sign in or register to post replies

Write your reply to:

Draft