| 12 | | Ajax.Responders.register({ |
|---|
| 13 | | onComplete: function(request, json) { |
|---|
| 14 | | var token = request.getHeader('X-sfAjaxDebug-Token') || ''; |
|---|
| 15 | | // console.log('sfAjaxDebug: got token '+token); |
|---|
| 16 | | if (token) { |
|---|
| 17 | | var url = '<?php echo url_for('sfAjaxDebug/get?token=_placeholder_') ?>'; |
|---|
| 18 | | url = url.replace('_placeholder_', token); |
|---|
| 19 | | new Ajax.Request(url, { |
|---|
| 20 | | method: 'get', |
|---|
| 21 | | asynchronous: true, |
|---|
| 22 | | evalScripts: false, |
|---|
| 23 | | onSuccess: function(request, json) { |
|---|
| 24 | | var sfWebDebug = document.getElementById('sfWebDebug'); |
|---|
| 25 | | if (sfWebDebug) { |
|---|
| 26 | | sfWebDebug.replace(request.responseText); |
|---|
| 27 | | } |
|---|
| 28 | | } |
|---|
| 29 | | }); |
|---|
| | 12 | var sfAjaxDebug = { |
|---|
| | 13 | |
|---|
| | 14 | keepRequests: <?php echo sfConfig::get('sf_ajax_debug_requests') ?>, |
|---|
| | 15 | |
|---|
| | 16 | requestCount: 0, |
|---|
| | 17 | |
|---|
| | 18 | current: null, |
|---|
| | 19 | toplevel: { id: 'toplevel', loaded: true, title: '[000] initial request', content: '' }, |
|---|
| | 20 | toolbars: [], |
|---|
| | 21 | toolbarsById: {}, |
|---|
| | 22 | |
|---|
| | 23 | initialize: function() { |
|---|
| | 24 | var sfWebDebug = document.getElementById('sfWebDebug'); |
|---|
| | 25 | if (sfWebDebug) { |
|---|
| | 26 | this.toplevel.content = sfWebDebug; |
|---|
| | 27 | this.current = this.toplevel; |
|---|
| | 28 | this.toolbarsById['toplevel'] = this.toplevel; |
|---|
| | 29 | this.insertRequestMenu(); |
|---|
| 31 | | } |
|---|
| 32 | | }); |
|---|
| | 31 | }, |
|---|
| | 32 | |
|---|
| | 33 | newRequest: function(module, action, token) { |
|---|
| | 34 | if (this.toolbars.length >= this.keepRequests) { |
|---|
| | 35 | var oldReq = this.toolbars.shift(); |
|---|
| | 36 | delete this.toolbarsById[oldReq.id]; |
|---|
| | 37 | } |
|---|
| | 38 | this.requestCount++; |
|---|
| | 39 | var fmtReqCount = this.requestCount.toString(); |
|---|
| | 40 | while (fmtReqCount.length < 3) { |
|---|
| | 41 | fmtReqCount = '0'+fmtReqCount; |
|---|
| | 42 | } |
|---|
| | 43 | var newReq = { |
|---|
| | 44 | id: token, |
|---|
| | 45 | loaded: false, |
|---|
| | 46 | title: '['+fmtReqCount+'] M: '+module+' A: '+action, |
|---|
| | 47 | content: '' |
|---|
| | 48 | }; |
|---|
| | 49 | this.toolbars.push(newReq); |
|---|
| | 50 | this.toolbarsById[token] = newReq; |
|---|
| | 51 | this.insertRequestMenu(); |
|---|
| | 52 | |
|---|
| | 53 | var url = '<?php echo url_for('sfAjaxDebug/get?token=_placeholder_') ?>'; |
|---|
| | 54 | url = url.replace('_placeholder_', token); |
|---|
| | 55 | var self = this; |
|---|
| | 56 | new Ajax.Request(url, { |
|---|
| | 57 | method: 'get', |
|---|
| | 58 | asynchronous: true, |
|---|
| | 59 | evalScripts: false, |
|---|
| | 60 | onSuccess: function(request, json) { |
|---|
| | 61 | var toolbar = self.getToolbar(token); |
|---|
| | 62 | if (toolbar) { |
|---|
| | 63 | toolbar.content = request.responseText; |
|---|
| | 64 | toolbar.loaded = true; |
|---|
| | 65 | self.switchToolbar(token); |
|---|
| | 66 | } |
|---|
| | 67 | } |
|---|
| | 68 | }); |
|---|
| | 69 | }, |
|---|
| | 70 | |
|---|
| | 71 | getToolbar: function(id) { |
|---|
| | 72 | if (id == 'toplevel') { |
|---|
| | 73 | return this.toplevel; |
|---|
| | 74 | } |
|---|
| | 75 | for (var i = 0; i < this.toolbars.length; i++) { |
|---|
| | 76 | if (this.toolbars[i].id == id) { |
|---|
| | 77 | return this.toolbars[i]; |
|---|
| | 78 | } |
|---|
| | 79 | } |
|---|
| | 80 | return false; |
|---|
| | 81 | }, |
|---|
| | 82 | |
|---|
| | 83 | switchToolbar: function(id) { |
|---|
| | 84 | var toolbar = this.getToolbar(id); |
|---|
| | 85 | if (!toolbar) { |
|---|
| | 86 | alert('sfAjaxDebug: toolbar "'+id+'" not found'); |
|---|
| | 87 | return; |
|---|
| | 88 | } |
|---|
| | 89 | if (!toolbar.loaded) { |
|---|
| | 90 | alert('sfAjaxDebug: The requested toolbar hasn\'t been loaded yet!'); |
|---|
| | 91 | return; |
|---|
| | 92 | } |
|---|
| | 93 | this.current = toolbar; |
|---|
| | 94 | document.getElementById('sfWebDebug').replace(toolbar.content); |
|---|
| | 95 | this.insertRequestMenu(); |
|---|
| | 96 | }, |
|---|
| | 97 | |
|---|
| | 98 | insertRequestMenu: function() { |
|---|
| | 99 | if (!document.getElementById('sfWebDebug')) { |
|---|
| | 100 | alert('sfAjaxDebug: could not find the web debug toolbar'); |
|---|
| | 101 | return; |
|---|
| | 102 | } |
|---|
| | 103 | |
|---|
| | 104 | var oldNode; |
|---|
| | 105 | |
|---|
| | 106 | var menu = document.createElement('div'); |
|---|
| | 107 | menu.setAttribute('id', 'sfAjaxDebug'); |
|---|
| | 108 | menu.setAttribute('class', 'sfWebDebugTop'); |
|---|
| | 109 | menu.style.display = 'none'; |
|---|
| | 110 | |
|---|
| | 111 | var head = document.createElement('h1'); |
|---|
| | 112 | head.appendChild(document.createTextNode('Ajax Requests')); |
|---|
| | 113 | menu.appendChild(head); |
|---|
| | 114 | |
|---|
| | 115 | var list = document.createElement('li'); |
|---|
| | 116 | list.setAttribute('id', 'sfAjaxDebugMenu'); |
|---|
| | 117 | |
|---|
| | 118 | list.appendChild(this.makeRequestMenuEntry(this.toplevel)); |
|---|
| | 119 | for (var i = 0; i < this.toolbars.length; i++) { |
|---|
| | 120 | list.appendChild(this.makeRequestMenuEntry(this.toolbars[i])); |
|---|
| | 121 | } |
|---|
| | 122 | |
|---|
| | 123 | menu.appendChild(list); |
|---|
| | 124 | |
|---|
| | 125 | if (oldNode = document.getElementById('sfAjaxDebug')) { |
|---|
| | 126 | oldNode.replace(menu); |
|---|
| | 127 | } |
|---|
| | 128 | else { |
|---|
| | 129 | document.getElementById('sfWebDebugBar').insert({ after: menu }); |
|---|
| | 130 | } |
|---|
| | 131 | |
|---|
| | 132 | var toolbarBtn = document.createElement('li'); |
|---|
| | 133 | toolbarBtn.setAttribute('id', 'sfAjaxDebugButton'); |
|---|
| | 134 | var toolbarLink = document.createElement('a'); |
|---|
| | 135 | toolbarLink.setAttribute('href', '#'); |
|---|
| | 136 | toolbarLink.setAttribute('onclick', "sfWebDebugShowDetailsFor('sfAjaxDebug'); return false;"); |
|---|
| | 137 | toolbarLink.appendChild(document.createTextNode(this.current?this.current.title:'ajax requests')); |
|---|
| | 138 | toolbarBtn.appendChild(toolbarLink); |
|---|
| | 139 | |
|---|
| | 140 | if (oldNode = document.getElementById('sfAjaxDebugButton')) { |
|---|
| | 141 | oldNode.replace(toolbarBtn); |
|---|
| | 142 | } |
|---|
| | 143 | else { |
|---|
| | 144 | document.getElementById('sfWebDebugDetails').insert({ top: toolbarBtn }); |
|---|
| | 145 | } |
|---|
| | 146 | }, |
|---|
| | 147 | |
|---|
| | 148 | makeRequestMenuEntry: function(entry) { |
|---|
| | 149 | var liNode = document.createElement('li'); |
|---|
| | 150 | if (this.current == entry) { |
|---|
| | 151 | liNode.setAttribute('class', 'sfAjaxDebugCurrent'); |
|---|
| | 152 | } |
|---|
| | 153 | var aNode = document.createElement('a'); |
|---|
| | 154 | aNode.setAttribute('href', '#'); |
|---|
| | 155 | aNode.setAttribute('onclick', 'sfAjaxDebug.switchToolbar("'+entry.id+'"); return false;'); |
|---|
| | 156 | aNode.appendChild(document.createTextNode(entry.title)); |
|---|
| | 157 | liNode.appendChild(aNode); |
|---|
| | 158 | return liNode; |
|---|
| | 159 | } |
|---|
| | 160 | }; |
|---|
| | 161 | |
|---|
| | 162 | if (Ajax && Ajax.Responders) { |
|---|
| | 163 | document.observe('dom:loaded', function() { |
|---|
| | 164 | sfAjaxDebug.initialize(); |
|---|
| | 165 | }); |
|---|
| | 166 | |
|---|
| | 167 | Ajax.Responders.register({ |
|---|
| | 168 | onComplete: function(request, json) { |
|---|
| | 169 | var token = request.getHeader('X-sfAjaxDebug-Token') || ''; |
|---|
| | 170 | // console.log('sfAjaxDebug: got token '+token); |
|---|
| | 171 | if (token) { |
|---|
| | 172 | var module = request.getHeader('X-sfAjaxDebug-Module') || '[unknown]'; |
|---|
| | 173 | var action = request.getHeader('X-sfAjaxDebug-Action') || '[unknown]'; |
|---|
| | 174 | sfAjaxDebug.newRequest(module, action, token); |
|---|
| | 175 | } |
|---|
| | 176 | } |
|---|
| | 177 | }); |
|---|
| | 178 | } |
|---|