Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 607 posts 2405 karma points
    Mar 18, 2021 @ 08:05
    Bo Jacobsen
    0

    How to add html with a controller directly into the html body when loading backoffice

    Hi all.

    using Umbraco 8.12.1

    I want to add some custom html with an angular controller into the backoffice in the body of the html, that are avaiable where ever you are in the backoffice.

    I have tried with $httpProvider interceptor and here i can fire some functions or even replace views, but i dunno how to add views?

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Mar 18, 2021 @ 09:09
    Kevin Jump
    0

    Hi,

    you want to use a controller that runs as part of the umbraco startup and listens for the "app.ready" event (this is fired when someone is logged into the back office.

    (function() {
        'use strict';
    
        function loader(eventsService, backchatManager) {
    
            // fired when a user is authenticated and logged in
            // so we can load anything into the back office here.
            eventsService.on('app.ready', function (event, data) {
                loadChat();
            });
    
            function loadChat() {
                console.info('loading chat UI');
                backchatManager.loadChat();
            }
        }
    
        angular.module('umbraco').run(loader);
    
    })();
    

    In this example loadChat() calls a findHeaderAction that appends a html element of <backchat-button /> which for this example is an angular component to handle the code/html injected for some chat functionality)

    function findHeaderActions() {
    
        var actionsList = angular.element(document)
            .find('.umb-app-header__actions');
    
        if (actionsList != null && actionsList.length == 1) {
    
            // inject a new item into the list ? 
            // actionsList[0]
    
    
            var item = document.createElement('backchat-button');
            actionsList[0].appendChild(item);
            $compile(item)($rootScope);
        }
    }
    
  • Bo Jacobsen 607 posts 2405 karma points
    Mar 18, 2021 @ 09:30
    Bo Jacobsen
    0

    Hi Kevin.

    Thanks for a quick reply.

    So i could just make a directive for the backchat-buttonangular.module("umbraco.directives").directive("backchatButton", ['assetsService', 'localizationService', function backchatButton(...) {...} ]); and inject it via the package.manifest.

    Do eventsService.on need to be set as a variable in order to clean up? https://our.umbraco.com/documentation/Reference/Angular/Services/eventsService/

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Mar 18, 2021 @ 09:33
    Kevin Jump
    0

    Hi,

    I would create a component : (this one opens an appDraw (which is the same as the help draw on the right hand side).

    (function () {
        'use strict';
    
        var backchatButtonComponent = {
            templateUrl: '/app_plugins/backchat/components/chatButton.html',
            bindings: {},
            controllerAs: 'vm',
            controller: backchatButtonController
        };
    
        function backchatButtonController($scope, appState) {
    
            var vm = this;
            vm.openChat = openChat;
    
            ///////
            function openChat() {
                console.info('open chat');
    
                var showDrawer = appState.getDrawerState("showDrawer");
                var drawer = {
                    view: "/app_plugins/backchat/chatDraw.html",
                    show: !showDrawer
                };
                appState.setDrawerState("view", drawer.view);
                appState.setDrawerState("showDrawer", drawer.show);
            }
    
        }
    
        angular.module('umbraco')
            .component('backchatButton', backchatButtonComponent);
    })();
    

    the view (its a list item because we are appending into the list of existing buttons)

    <li data-element="global-help" class="umb-app-header__action">
        <button type="button" class="umb-app-header__button btn-reset" hotkey="ctrl+shift+c" ng-click="vm.openChat()">
            <i class="umb-app-header__action-icon icon-chat" aria-hidden="true"></i>
        </button>
    </li>
    

    yeah there really should be an variable on the eventService to clean it up. although as this is a function that runs on load, so I am not sure if it does get cleaned up in browser or if its always there while the user is logged in.

  • Bo Jacobsen 607 posts 2405 karma points
    Mar 18, 2021 @ 09:46
    Bo Jacobsen
    0

    Thanks alot, now i think i understand it.

    So a user would have to click on the icon/button for the chat to be loaded.

    I will try play around with it, in my case i just want to show some notifications about everything :)

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Mar 18, 2021 @ 09:49
    Kevin Jump
    0

    yeah - in this example

    but you can replace the html in that component with anything and as the component is loaded you can run JavaScript in the controller to manipulate the view just as you would for a controller in a content app or property editor.

  • Bo Jacobsen 607 posts 2405 karma points
    Mar 18, 2021 @ 10:03
    Bo Jacobsen
    0

    Understood and thanks again :)

    I might come back here, if i get some new questions about this.

Please Sign in or register to post replies

Write your reply to:

Draft