         else if req.model_id in @needReboot
             data.target = "top"
 
-        source = @get "source"
-        source.set "page", req.model_id
+        this.source.page = req.model_id
         @sendAjax
             data: data
         .then (doc) ->
-            source
+            this.source
 
     doPing: (cfg) ->
-        @set "source.ping", Em.run.later(@, =>
-            Em.$.ajax
+        this.source.ping = later @, =>
+            $.ajax
                 url: cfg.href
             @doPing cfg
-        , cfg.timeout)
+        , cfg.timeout
 
 
     sendAjax: (req) ->
         req.dataType = "json"
         req.type ?= if req?.data?.action then "POST" else "GET"
-        req.url ?= @controllerFor("config").get "url"
+        req.url ?= @config.url
         req.data._ = new Date().getTime()
         $(".loading").addClass "in-progress"
 
-        source = @get "source"
-
         if req.type is "POST"
-            req.data._rtoken = source.rtoken
+            req.data._rtoken = this.source.rtoken
 
         target = req.data.target or "self"
         if target is "self"
-            if source.get "modal"
+            if this.source.modal
                 target = "modal"
-            else if source.get("tabs.length") > 1
+            else if this.source.tabs.length > 1
                 target = "active"
             else
                 target = "top"
 
-        if source.get "refresh"
-            Em.run.cancel source.get "refresh"
-            source.set "refresh", null
+        if this.source.refresh
+            cancel this.source.refresh
+            this.source.refresh = null
             $(".refresh").removeClass "in-progress"
 
-        new Em.RSVP.Promise (resolve, reject) =>
-            Em.$.ajax(req).then (doc) =>
-                source.beginPropertyChanges()
+        new Promise (resolve, reject) =>
+            $.ajax(req).then (doc) =>
+                this.source.beginPropertyChanges()
 
-                source.set "status", doc.status
-                source.set "modal", null
+                this.source.status = doc.status
+                this.source.modal = null
 
                 if doc.ping
-                    if source.get "ping"
-                        Em.run.cancel source.get "ping"
+                    cancel this.source.ping if this.source.ping
                     @doPing doc.ping
 
                 if doc.refresh
-                    source.set "refresh", Em.run.later(@, ->
+                    this.source.refresh = later(@, ->
                         @sendAjax data:
                             page: doc.refresh.href
                     , doc.refresh.timeout)
-                    Em.run.scheduleOnce "afterRender", =>
+                    scheduleOnce "afterRender", =>
                         $(".refresh").addClass "in-progress"
 
                 if doc.goto

        force:
            refreshModel: true

    setupAjax: Em.on "init", ->
        Em.$.ajaxSetup
            beforeSend: (xhr) ->
                xhr.setRequestHeader "X-OPENXPKI-Client", "1"

    needReboot: [ "login", "logout", "login!logout", "welcome" ]

    source: Em.computed -> Em.Object.create
        page: null
        ping: null
        refresh: null
        structure: null
        rtoken: null
        status: null
        tabs: []
        navEntries: []

    beforeModel: (req) ->
        if req.queryParams.force
            delete req.queryParams.force

        source = @get "source"
        model_id = req.params.openxpki.model_id

        if not source.get("navEntries.length") or model_id in @needReboot
            @sendAjax data:
                page: "bootstrap!structure"
                baseurl: window.location.pathname

    model: (req) ->
        navEntries = @get "source.navEntries"
        data = page: req.model_id
        data.limit = req.limit if req.limit
        data.startat = req.startat if req.startat

        entries = navEntries.reduce (p, n) ->
            p.concat(n, n.entries||[])
        , []


import Route from '@ember/routing/route'
import EmberObject, { computed } from '@ember/object'
import { tracked } from '@glimmer/tracking'
import { A } from '@ember/array'
import { later, scheduleOnce, cancel } from '@ember/runloop'
import { inject as injectCtrl } from '@ember/controller'
import Promise from 'rsvp'

export default Route.extend
    config: injectCtrl()        # injects ConfigController

    queryParams:
        startat:
            refreshModel: true
