Skip to content

Commit 14c5d91

Browse files
committed
Added missing issue. Integrated issue manager script (needs to be uncommented to work)
1 parent 9cbf3a7 commit 14c5d91

5 files changed

Lines changed: 873 additions & 373 deletions

File tree

css3-regions/BugzillaTracker.js

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
(function(){
2+
3+
// Templating engine
4+
var TemplateManager = (function(){
5+
6+
/*
7+
Convert an HTML string into a valid node structure.
8+
9+
@param {String} string The string HTML to be converted to nodes
10+
@return {DocumentFragment} a DocumentFragment with the node structure
11+
*/
12+
var _convertStringToNode = function(string){
13+
// make a temporary container
14+
var temp = document.createElement("div");
15+
16+
// let the parser turn the template into a valid node structure
17+
temp.innerHTML = string;
18+
19+
// create document fragment as payload container
20+
var fragment = document.createDocumentFragment();
21+
22+
while(temp.firstChild){
23+
24+
// extract nodes from the temp container into the payload container
25+
fragment.appendChild(temp.firstChild)
26+
}
27+
28+
return fragment;
29+
}
30+
31+
var _createTemplateFn = function(replaceVars, template){
32+
33+
return function(data){
34+
35+
if (replaceVars.length && data !== null){
36+
var templateClone = template;
37+
38+
for (var i = 0, len = replaceVars.length; i < len; i++){
39+
40+
// extract just the variable name, without template markers
41+
var key = replaceVars[i].replace("{{","").replace("}}","");
42+
43+
// do we have a value for this template variable?
44+
if (data[key]){
45+
46+
// global regex pattern based on template variable
47+
var pattern = new RegExp(replaceVars[i], "g");
48+
49+
// replace template variable with real data
50+
templateClone = templateClone.replace(pattern, data[key]);
51+
}
52+
}
53+
}
54+
55+
// return a nicely wrapped node structure to be appended to the document
56+
return _convertStringToNode(templateClone)
57+
}
58+
}
59+
60+
61+
return {
62+
63+
/*
64+
Create a template function from a string of HTML markup
65+
Extract template variables for quick reuse.
66+
67+
@param {String} template A string of HTML with template variables marked as {{variable-name}}
68+
@return {Function} A function that expects a data object to populate the template
69+
*/
70+
compile: function(template){
71+
72+
if (typeof template !== "string"){
73+
throw new TypeError("Invalid templateString type. Expected 'String', got " + typeof templateString)
74+
}
75+
76+
// look for all the varialbes to be replaced. they look like {{varName}}
77+
var replaceVars = template.match(/{{[\w_-]*}}/g);
78+
79+
return _createTemplateFn(replaceVars, template)
80+
}
81+
}
82+
})();
83+
84+
// Bugzilla tracker
85+
var BugzillaTracker = (function(){
86+
87+
var _cache = {},
88+
_config = {
89+
bugSearchURL: "https://www.w3.org/Bugs/Public/buglist.cgi",
90+
scriptId: "BugzillaTracker",
91+
onSync: _onSync
92+
},
93+
_queryParams = {
94+
product: "CSS",
95+
component: "CSS Regions",
96+
ctype: "js"
97+
};
98+
99+
100+
function _getScript(url){
101+
var script = document.createElement("script"),
102+
oldScript = document.getElementById(_config.scriptId),
103+
target = document.getElementsByTagName("script")[0];
104+
105+
// Bugzilla returns results in the global namespace.
106+
// Remove old script references to avoid poluted results
107+
if (oldScript){
108+
oldScript.parentNode.removeChild(oldScript)
109+
}
110+
111+
script.type = "text/javascript";
112+
script.src = url;
113+
script.id = _config.scriptId;
114+
115+
return target.parentNode.insertBefore(script, target);
116+
}
117+
118+
/*
119+
Extend an object with the properties and value of other objects.
120+
Uses the function arguments as source objects.
121+
Same-name properties will be overwritten if found in subsequent source objects.
122+
123+
@param {Object} object The target object that will be extended
124+
@return {Object} an object containing all the keys and the values of the source objects
125+
126+
@example
127+
_extend({}, {value: "1"}, {isPublic: false })
128+
*/
129+
function _extend(object){
130+
131+
// make arguments an array and get all the arguments, except the first one
132+
var sources = Array.prototype.slice.call(arguments, 1);
133+
134+
if (sources && sources.length){
135+
sources.forEach(function(source){
136+
for (var property in source){
137+
object[property] = source[property]
138+
}
139+
})
140+
}
141+
142+
return object;
143+
}
144+
145+
/*
146+
Convert an object with key/value pairs into a query string.
147+
Value entities are encoded for use in HTTP requests.
148+
149+
@param {Object} object The object to be serialized.
150+
@return {String} The serialized object;
151+
*/
152+
function _serialize(object){
153+
var pairs = [];
154+
for (var key in object){
155+
pairs.push( key + "=" + encodeURIComponent(object[key]) )
156+
}
157+
158+
return pairs.join("&");
159+
}
160+
161+
/*
162+
Convert the Bugzilla bug data array into an object.
163+
To maintain consistency where Bugzilla didn't, the object keys are named as the ones returned by the Bugzilla API when requesting a single bug.
164+
165+
When requesting bugLists as JavaScript Bugzilla returns each bug's data as an array of 7 values in this order:
166+
167+
[
168+
bug severity,
169+
bug priority,
170+
operating system,
171+
username assigned to bug,
172+
bug status,
173+
bug resolution,
174+
bug description (title)
175+
]
176+
177+
@param {Array} array The bug details as values of a 7 items array;
178+
179+
@return {Object} An object with keys an values describing the bug ( The way we like it in the real world! )
180+
181+
*/
182+
function _createBugObject(array){
183+
if (array.length !== 7){
184+
throw new Error("Invalid bug data. Expected array of 7 values.")
185+
}
186+
187+
return {
188+
"bug_severity": array[0],
189+
"priority": array[1],
190+
"op_sys": array[2], // operating system
191+
"assigned_to": array[3],
192+
"bug_status": array[4],
193+
"resolution": array[5],
194+
"short_desc": array[6]
195+
}
196+
}
197+
198+
function _onSync(bugList){
199+
200+
console.log("onsync", bugList)
201+
}
202+
203+
function _getCallbackFn(){
204+
return function(bugList){
205+
206+
// No bugs; Silent failure
207+
if (!bugList || !bugList.length){
208+
return
209+
}
210+
211+
var bugObj,
212+
bugs = {};
213+
214+
bugList.forEach(function(bug, index){
215+
216+
var bugObj = _createBugObject(bug, index);
217+
218+
if (bugObj){
219+
bugObj["bug_id"] = index;
220+
bugs[index] = bugObj;
221+
}
222+
223+
})
224+
225+
// run user-provided callback with the bug list
226+
_config.onSync.call(BugzillaTracker, bugs)
227+
228+
// restore any previously buglistCallback function
229+
if (typeof _cache.buglistCallback == "function"){
230+
window.buglistCallback = _cache.buglistCallback
231+
}
232+
}
233+
}
234+
235+
return {
236+
237+
/*
238+
Request bugs from Bugzilla based on seach query parameters.
239+
240+
@param {Object} queryParams A map of query parameters for the Bugzilla API
241+
242+
@example
243+
{
244+
product: "CSS",
245+
component: "CSS Regions"
246+
}
247+
*/
248+
sync: function(queryParams, callbackFn){
249+
250+
var params = _extend({}, _queryParams, queryParams),
251+
queryString = _serialize(params),
252+
url = _config.bugSearchURL + "?" + queryString;
253+
254+
if (typeof callbackFn == "function"){
255+
_config.onSync = callbackFn;
256+
}
257+
258+
/*
259+
Bugzilla has a hardcoded callback function in the response when returning results as JavaScript (ctype=js).
260+
It is window.buglistCallback(array); It receives a single paramter: an array of objects describing bugs.
261+
262+
Cache the reference to this callback if it already exists.
263+
Avoid collisions, replace with our own callback method, then restore the old function it other scripts may need it.
264+
*/
265+
if (typeof window.buglistCallback == "function"){
266+
_cache.buglistCallback = window.buglistCallback;
267+
}
268+
269+
// Set our own handler. After running, it will replace itself with the original callback, if function was defined.
270+
window.buglistCallback = _getCallbackFn();
271+
272+
_getScript(url);
273+
},
274+
275+
// overwrite default configuration (ex: Bugzilla buglist.cgi URL)
276+
setOptions: function(options){
277+
_config = _extend({}, _config, options)
278+
},
279+
280+
setIssueTemplate: function(string){
281+
if (string && typeof string){
282+
_config.issueTemplate = TemplateManager.compile(string);
283+
284+
// usage
285+
// _config.issueTemplate({bug_id: "1234", short_desc: "oprea e aici!"})
286+
}
287+
},
288+
289+
renderIssue: function(bugData){
290+
return _config.issueTemplate(bugData)
291+
}
292+
}
293+
})();
294+
295+
window.BugzillaTracker = BugzillaTracker;
296+
})()

0 commit comments

Comments
 (0)