-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenful.js
executable file
·98 lines (93 loc) · 3.6 KB
/
screenful.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var Screenful={
createEnvelope: function(scrollable){
var html="<div id='envelope' class='"+(scrollable?"scrollable":"")+"'></div>";
if(window.parent==window || (window.parent!=window && !window.parent.Screenful)) html+="<div id='statusbar'><span class='statusmessage'></span></div>";
if($("#footer").length>0) $("#footer").before(html); else $("body").append(html);
Screenful.resize();
$(window).on("resize", Screenful.resize);
$("body").append("<div id='curtain' style='display: none'></div>")
},
resize: function(){
var headerHeight=$("#header").outerHeight() | 0;
var footerHeight=$("#footer").outerHeight() | 0;
var statusbarHeight=$("#statusbar").outerHeight() | 0;
$("#statusbar").css("bottom", (footerHeight+1)+"px");
$("#envelope").css("top", headerHeight+"px");
$("#envelope").css("bottom", (footerHeight+statusbarHeight+2)+"px");
},
status: function(str, style){
if(window.parent!=window && window.parent.Screenful) window.parent.Screenful.status(str, style);
else {
if(style=="wait") str="<span class='wait'></span>"+str;
if(style=="warn") str="<span class='warn'></span>"+str;
$("#statusbar .statusmessage").html(str);
}
},
wycLastID: 0,
wycCache: {},
wycQueue: [],
wycIsRunning: false,
wyc: function(url, callback){ //a "when-you-can" function for delayed rendering: gets json from url, passes it to callback, and delayed-returns html-as-string from callback
Screenful.wycLastID++;
var wycID="screenful_wyc_"+Screenful.wycLastID;
if(Screenful.wycCache[url]) return callback(Screenful.wycCache[url]);
Screenful.wycQueue.push(function(){ //push job to WYC queue
Screenful.wycIsRunning=true;
Screenful.wycQueue.shift(); //remove myself from the WYC queue
$.ajax({url: url, dataType: "json", method: "POST"}).done(function(data){
$("#"+wycID).replaceWith(callback(data));
if(Screenful.wycCache.length>1000) Screenful.wycCache.length=[];
Screenful.wycCache[url]=data;
if(Screenful.wycQueue.length>0) Screenful.wycQueue[0](); else Screenful.wycIsRunning=false; //run the next WYC job, or say that WYC has finished running
})
});
if(!Screenful.wycIsRunning && Screenful.wycQueue.length>0) Screenful.wycQueue[0]();
return "<span class='wyc' id='"+wycID+"'></span>";
},
formatXml: function(xml) { //stolen from https://gist.github.com/sente/1083506
var formatted = '';
var reg = /(>)(<)(\/*)/g;
xml = xml.replace(reg, '$1\r\n$2$3');
var pad = 0;
jQuery.each(xml.split('\r\n'), function(index, node) {
var indent = 0;
if (node.match( /.+<\/\w[^>]*>$/ )) {
indent = 0;
} else if (node.match( /^<\/\w/ )) {
if (pad != 0) {
pad -= 1;
}
} else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
indent = 1;
} else {
indent = 0;
}
var padding = '';
for (var i = 0; i < pad; i++) {
padding += ' ';
}
formatted += padding + node + '\r\n';
pad += indent;
});
return formatted;
},
cleanupXml: function(xml){
return xml.trim().replace(/\>[\r\n]+\s*\</g, "><");
},
isWellFormedXml: function(xml){
var doc=null;
try{doc=$.parseXML(xml);} catch(e){}
if(doc) return true;
return false;
},
isWellFormedJson: function(json){
var obj=null;
try{obj=JSON.parse(json);} catch(e){}
if(obj) return true;
return false;
},
formatJson: function(json){
if(typeof(json)=="string") json=JSON.parse(json);
return JSON.stringify(json, null, " ");
},
};