UNPKG

4.36 kBJavaScriptView Raw
1/**
2 * The map plugin for auto responder
3 */
4define('seajs/plugin-debug', [], function() {
5
6 var util = seajs.pluginSDK.util
7 var loc = this.location
8 var search = loc.search
9 var config = getConfig()
10
11
12 // Forces debug to true when url contains `?seajs-debug`
13 if (search.indexOf('seajs-debug') > -1) {
14 config.debug = 1
15 config.console = 1
16 saveConfig(config)
17 }
18
19 // Loads the map file
20 if (config.mapfile) {
21 document.title = '[seajs debug mode] - ' + document.title
22
23 // Adds the `mapfile` to preload config
24 seajs.config({
25 preload: config.mapfile
26 })
27 }
28
29
30 // Restores the use function
31 seajs.use = seajs._use
32 delete seajs._use
33
34 // Calls pre-called `seajs.use`
35 var args = seajs._useArgs
36 if (args) {
37 for (var i = 0; i < args.length; i++) {
38 seajs.use(args[i][0], args[i][1])
39 }
40 delete seajs._useArgs
41 }
42
43
44 // Shows console
45 if (config.console) {
46 showConsole(config.mapfile)
47 }
48
49
50 // Loads firebug-lite
51 if (search.indexOf('firebug-lite') > -1) {
52 util.fetch('https://getfirebug.com/firebug-lite.js')
53 }
54
55
56 // Helpers
57 // -------
58
59 function showConsole(mapfile) {
60 var style =
61 '#seajs-debug-console { ' +
62 ' position: fixed; bottom: 10px; ' +
63 ' *position: absolute; *top: 10px; *width: 465px; ' +
64 ' right: 10px; z-index: 999999999;' +
65 ' background: #fff; color: #000; font: 12px arial;' +
66 ' border: 2px solid #000; padding: 0 10px 10px;' +
67 '}' +
68 '#seajs-debug-console h3 {' +
69 ' margin: 3px 0 6px -6px; padding: 0;' +
70 ' font-weight: bold; font-size: 14px;' +
71 '}' +
72 '#seajs-debug-console input {' +
73 ' width: 400px; margin-left: 10px;' +
74 '}' +
75 '#seajs-debug-console button {' +
76 ' float: right; margin: 6px 0 0 10px;' +
77 ' box-shadow: #ddd 0 1px 2px;' +
78 ' font-size: 14px; padding: 4px 10px;' +
79 ' color: #211922; background: #f9f9f9;' +
80 ' text-shadow: 0 1px #eaeaea;' +
81 ' border: 1px solid #bbb; border-radius: 3px;' +
82 ' cursor: pointer; opacity: .8' +
83 '}' +
84 '#seajs-debug-console button:hover {' +
85 ' background: #e8e8e8; text-shadow: none; opacity: 1' +
86 '}' +
87 '#seajs-debug-console a {' +
88 ' position: relative; top: 10px; text-decoration: none;' +
89 '}'
90
91 var html =
92 '<div id="seajs-debug-console">' +
93 ' <h3>SeaJS Debug Console</h3>' +
94 ' <label>Map file: <input value="' + mapfile + '"/></label><br/>' +
95 ' <button>Exit</button>' +
96 ' <button>Hide</button>' +
97 ' <button>Refresh</button>' +
98 '</div>'
99
100 var div = document.createElement('div')
101 div.innerHTML = html
102
103 seajs.importStyle(style)
104 appendToBody(div)
105
106 var buttons = div.getElementsByTagName('button')
107
108 // hide
109 buttons[1].onclick = function() {
110 config.console = 0
111 saveConfig(config)
112 loc.replace(loc.href.replace('seajs-debug', ''))
113 }
114
115 // refresh
116 buttons[2].onclick = function() {
117 var link = div.getElementsByTagName('input')[0].value || ''
118 if (link) {
119 link = util.id2Uri(link)
120 }
121
122 config.mapfile = link
123 saveConfig(config)
124 loc.reload()
125 }
126
127 // exit debug mode
128 buttons[0].onclick = function() {
129 config.debug = 0
130 saveConfig(config)
131 loc.replace(loc.href.replace('seajs-debug', ''))
132 }
133 }
134
135 function getConfig() {
136 var cookie = '', m
137
138 if ((m = document.cookie.match(
139 /(?:^| )seajs-debug(?:(?:=([^;]*))|;|$)/))) {
140 cookie = m[1] ? decodeURIComponent(m[1]) : ''
141 }
142
143 var parts = cookie.split('`')
144 return {
145 debug: Number(parts[0]) || 0,
146 mapfile: parts[1] || '',
147 console: Number(parts[2]) || 0
148 }
149 }
150
151 function saveConfig(o) {
152 var date = new Date()
153 date.setTime(date.getTime() + 30 * 86400000) // 30 days
154
155 document.cookie = 'seajs-debug=' + o.debug + '`' + o.mapfile + '`' +
156 o.console + '; path=/; expires=' + date.toUTCString()
157 }
158
159
160 var MAX_TRY = 100
161 var pollCount = 0
162
163 function appendToBody(div) {
164 pollCount++
165
166 if (document.body) {
167 document.body.appendChild(div)
168 }
169 else if (pollCount < MAX_TRY) {
170 setTimeout(function() {
171 appendToBody(div)
172 }, 200)
173 }
174 }
175
176});
177