Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit76f2619

Browse files
committed
Merge pull request#26 from VikramN/master
Enahcements
2 parents7fd4cf1 +f3e6b48 commit76f2619

File tree

4 files changed

+166
-8
lines changed

4 files changed

+166
-8
lines changed

‎README.rst‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,36 @@ you can see js/demo.js in this repo for an example or use it live at the
2828
output.appendChild(node);
2929

3030

31+
Additional options parameter can be supplied
32+
33+
::
34+
35+
var input = {
36+
"url" : [
37+
"www.google.com",
38+
"www.google.com",
39+
{
40+
"x" : "x-direction",
41+
"y" : "y-direction",
42+
"url" : "www.google.com"
43+
}
44+
]
45+
};
46+
47+
var node = JsonHuman.format(input, {
48+
// Show or hide Array-Indices in the output
49+
showArrayIndex: true,
50+
51+
// Hyperlinks Option
52+
// Enable <a> tag in the output html based on object keys
53+
// Supports only strings and arrays
54+
hyperlinks : {
55+
enable : true,
56+
keys: ['url'], // Keys which will be output as links
57+
target : '_blank' // 'target' attribute of a
58+
}
59+
});
60+
3161
To install it, if you're using `Bower<https://github.com/bower/bower>`_ you
3262
can just run::
3363

‎css/json.human.css‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,19 @@ th.jh-key{
7575
color:#999;
7676
font-size: small;
7777
}
78+
79+
.jh-a {
80+
text-decoration: none;
81+
}
82+
83+
.jh-a:hover{
84+
text-decoration: underline;
85+
}
86+
87+
.jh-aspan.jh-type-string {
88+
text-decoration: none;
89+
font-weight: bold;
90+
color:#268ddd;
91+
font-style: normal;
92+
}
93+

‎js/demo.no.requirejs.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
});
1212

1313
functionconvert(input,output){
14-
varnode=JsonHuman.format(input);
14+
varnode=JsonHuman.format(input,{
15+
showArrayIndex:false,
16+
hyperlinks :{
17+
enable :true,
18+
keys:['url','main'],
19+
target :'_blank'
20+
}
21+
});
1522

1623
output.innerHTML="";
1724
output.appendChild(node);

‎src/json.human.js‎

Lines changed: 112 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
}(this,function(){
1212
"use strict";
1313

14+
varindexOf=[].indexOf||function(item){for(vari=0,l=this.length;i<l;i++){if(iinthis&&this[i]===item)returni;}return-1;};
15+
1416
functionmakePrefixer(prefix){
1517
returnfunction(name){
1618
returnprefix+"-"+name;
@@ -47,6 +49,13 @@
4749
returnresult;
4850
}
4951

52+
functionlinkNode(child,href,target){
53+
vara=scn("a",HYPERLINK_CLASS_NAME,child);
54+
a.setAttribute('href',href);
55+
a.setAttribute('target',target);
56+
returna;
57+
}
58+
5059
vartoString=Object.prototype.toString,
5160
prefixer=makePrefixer("jh"),
5261
p=prefixer,
@@ -78,6 +87,8 @@
7887
ARRAY_CLASS_NAME=p("type-array"),
7988
ARRAY_EMPTY_CLASS_NAME=p("type-array")+" "+p("empty"),
8089

90+
HYPERLINK_CLASS_NAME=p('a'),
91+
8192
UNKNOWN_CLASS_NAME=p("type-unk");
8293

8394
functiongetType(obj){
@@ -103,12 +114,16 @@
103114
}
104115
}
105116

106-
function_format(data){
107-
varresult,container,key,keyNode,valNode,len,childs,tr,
117+
function_format(data,options,parentKey){
118+
119+
varresult,container,key,keyNode,valNode,len,childs,tr,value,
108120
isEmpty=true,
109121
accum=[],
110122
type=getType(data);
111123

124+
// Initialized & used only in case of objects & arrays
125+
varhyperlinksEnabled,aTarget,hyperlinkKeys;
126+
112127
switch(type){
113128
caseBOOL:
114129
result=data ?sn("span",BOOL_CLASS_NAME,"true")
@@ -129,11 +144,32 @@
129144
break;
130145
caseOBJECT:
131146
childs=[];
147+
148+
aTarget=options.hyperlinks.target;
149+
hyperlinkKeys=options.hyperlinks.keys;
150+
151+
// Is Hyperlink Key
152+
hyperlinksEnabled=
153+
options.hyperlinks.enable&&
154+
hyperlinkKeys&&
155+
hyperlinkKeys.length>0;
156+
132157
for(keyindata){
133158
isEmpty=false;
134159

160+
value=data[key];
161+
162+
valNode=_format(value,options,key);
135163
keyNode=sn("th",OBJ_KEY_CLASS_NAME,key);
136-
valNode=scn("td",OBJ_VAL_CLASS_NAME,_format(data[key]));
164+
165+
if(hyperlinksEnabled&&
166+
typeof(value)==='string'&&
167+
indexOf.call(hyperlinkKeys,key)>=0){
168+
169+
valNode=scn("td",OBJ_VAL_CLASS_NAME,linkNode(valNode,value,aTarget));
170+
}else{
171+
valNode=scn("td",OBJ_VAL_CLASS_NAME,valNode);
172+
}
137173

138174
tr=document.createElement("tr");
139175
tr.appendChild(keyNode);
@@ -154,12 +190,34 @@
154190
caseARRAY:
155191
if(data.length>0){
156192
childs=[];
193+
varshowArrayIndices=options.showArrayIndex;
194+
195+
aTarget=options.hyperlinks.target;
196+
hyperlinkKeys=options.hyperlinks.keys;
197+
198+
// Hyperlink of arrays?
199+
hyperlinksEnabled=parentKey&&options.hyperlinks.enable&&
200+
hyperlinkKeys&&
201+
hyperlinkKeys.length>0&&
202+
indexOf.call(hyperlinkKeys,parentKey)>=0;
203+
157204
for(key=0,len=data.length;key<len;key+=1){
205+
158206
keyNode=sn("th",ARRAY_KEY_CLASS_NAME,key);
159-
valNode=scn("td",ARRAY_VAL_CLASS_NAME,_format(data[key]));
207+
value=data[key];
208+
209+
if(hyperlinksEnabled&&typeof(value)==="string"){
210+
valNode=_format(value,options,key);
211+
valNode=scn("td",ARRAY_VAL_CLASS_NAME,linkNode(valNode,value,aTarget));
212+
}else{
213+
valNode=scn("td",ARRAY_VAL_CLASS_NAME,_format(value,options,key));
214+
}
160215

161216
tr=document.createElement("tr");
162-
tr.appendChild(keyNode);
217+
218+
if(showArrayIndices){
219+
tr.appendChild(keyNode);
220+
}
163221
tr.appendChild(valNode);
164222

165223
childs.push(tr);
@@ -179,15 +237,62 @@
179237
}
180238

181239
functionformat(data,options){
182-
options=options||{};
240+
options=validateOptions(options||{});
241+
183242
varresult;
184243

185-
result=_format(data);
244+
result=_format(data,options);
186245
result.className=result.className+" "+prefixer("root");
187246

188247
returnresult;
189248
}
190249

250+
251+
functionvalidateOptions(options){
252+
options=validateArrayIndexOption(options);
253+
options=validateHyperlinkOptions(options);
254+
255+
// Add any more option validators here
256+
257+
returnoptions;
258+
}
259+
260+
261+
functionvalidateArrayIndexOption(options){
262+
if(options.showArrayIndex===undefined){
263+
options.showArrayIndex=true;
264+
}else{
265+
// Force to boolean just in case
266+
options.showArrayIndex=options.showArrayIndex ?true:false;
267+
}
268+
269+
returnoptions;
270+
}
271+
272+
functionvalidateHyperlinkOptions(options){
273+
varhyperlinks={
274+
enable :false,
275+
keys :null,
276+
target :''
277+
};
278+
279+
if(options.hyperlinks&&options.hyperlinks.enable){
280+
hyperlinks.enable=true;
281+
282+
hyperlinks.keys=isArray(options.hyperlinks.keys) ?options.hyperlinks.keys :[];
283+
284+
if(options.hyperlinks.target){
285+
hyperlinks.target=''+options.hyperlinks.target;
286+
}else{
287+
hyperlinks.target='_blank';
288+
}
289+
}
290+
291+
options.hyperlinks=hyperlinks;
292+
293+
returnoptions;
294+
}
295+
191296
return{
192297
format:format
193298
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp