2
2
3
3
var hexSearch = {
4
4
state : {
5
- availableLibrariesDom : document . querySelector ( ".js-hex-search-available-libraries " ) ,
6
- aleLibrariesDom : document . querySelector ( ".js-elixir- ale-libraries " ) ,
5
+ availableLibrariesDom : document . querySelector ( ".js-hex-search-circuits-libs " ) ,
6
+ aleLibrariesDom : document . querySelector ( ".js-hex-search- ale-libs " ) ,
7
7
searchedLibs : [ "circuits_uart" , "circuits_i2c" , "circuits_gpio" , "circuits_spi" , "nerves_uart" , "elixir_ale" ] ,
8
8
i2cLibraries : [ ] ,
9
9
spiLibraries : [ ] ,
@@ -40,7 +40,7 @@ var hexSearch = {
40
40
} . bind ( this )
41
41
)
42
42
. catch ( function ( e ) {
43
- consoe . log ( e ) ;
43
+ console . log ( e ) ;
44
44
} ) ;
45
45
break ;
46
46
@@ -76,7 +76,7 @@ var hexSearch = {
76
76
77
77
searchLib : function ( libraryName ) {
78
78
return axios
79
- . get ( "https://hex.pm/api/packages?search=depends%3A" + libraryName )
79
+ . get ( "https://hex.pm/api/packages?search=depends%3A" + libraryName + "&sort=downloads" )
80
80
} ,
81
81
82
82
libsFromResponse : function ( responseData ) {
@@ -87,69 +87,72 @@ var hexSearch = {
87
87
name : responseData [ i ] . name ,
88
88
url : responseData [ i ] . html_url ,
89
89
description : responseData [ i ] . meta . description ,
90
+ downloads : responseData [ i ] . downloads . all
90
91
} ) ;
91
92
}
92
93
93
94
return libs ;
94
95
} ,
95
96
96
97
updateDom : function ( state ) {
97
- var ul = document . createElement ( "ul" ) ;
98
- ul . classList . add ( "flex-container" ) ;
99
- ul . id = "js-hex-lib-list" ;
98
+ var container_circuits_i2c = this . updateDomSection ( state . i2cLibraries , "circuits_i2c" ) ;
99
+ var container_circuits_gpio = this . updateDomSection ( state . gpioLibraries , "circuits_gpio" ) ;
100
+ var container_circuits_spi = this . updateDomSection ( state . spiLibraries , "circuits_spi" ) ;
101
+ var container_circuits_uart = this . updateDomSection ( state . uartLibraries , "circuits_uart" ) ;
102
+ var container_nerves_uart = this . updateDomSection ( state . nervesUartLibraries , "nerves_uart" ) ;
100
103
101
- var aleUl = document . createElement ( "ul" ) ;
102
- aleUl . classList . add ( "flex-container" ) ;
103
- aleUl . id = "js-hex-ale-list" ;
104
+ var container_elixir_ale = this . updateDomSection ( state . aleLibraries , "elixir_ale" ) ;
104
105
105
- for ( var i = 0 ; i < state . i2cLibraries . length ; i ++ ) {
106
- var li = this . buildLibListItem ( state . i2cLibraries [ i ] , "circuits_i2c" ) ;
107
- ul . appendChild ( li ) ;
108
- }
109
-
110
- for ( var i = 0 ; i < state . gpioLibraries . length ; i ++ ) {
111
- var li = this . buildLibListItem ( state . gpioLibraries [ i ] , "circuits_gpio" ) ;
112
- ul . appendChild ( li ) ;
113
- }
114
-
115
- for ( var i = 0 ; i < state . spiLibraries . length ; i ++ ) {
116
- var li = this . buildLibListItem ( state . spiLibraries [ i ] , "circuits_spi" ) ;
117
- ul . appendChild ( li ) ;
118
- }
106
+ state . availableLibrariesDom . innerHTML = container_circuits_i2c +
107
+ container_circuits_gpio +
108
+ container_circuits_spi +
109
+ container_circuits_uart +
110
+ container_nerves_uart ;
119
111
120
- for ( var i = 0 ; i < state . uartLibraries . length ; i ++ ) {
121
- var li = this . buildLibListItem ( state . uartLibraries [ i ] , "circuits_uart" ) ;
122
- ul . appendChild ( li ) ;
123
- }
112
+ state . aleLibrariesDom . innerHTML = container_elixir_ale ;
113
+ } ,
124
114
125
- for ( var i = 0 ; i < state . nervesUartLibraries . length ; i ++ ) {
126
- var li = this . buildLibListItem ( state . nervesUartLibraries [ i ] , "nerves_uart" ) ;
127
- ul . appendChild ( li ) ;
128
- }
115
+ updateDomSection : function ( libs , protocol ) {
116
+ var tbody = document . createElement ( "tbody" ) ;
129
117
130
- for ( var i = 0 ; i < state . aleLibraries . length ; i ++ ) {
131
- var li = this . buildLibListItem ( state . aleLibraries [ i ] , "elixir_ale" ) ;
132
- aleUl . appendChild ( li ) ;
118
+ for ( var i = 0 ; i < libs . length ; i ++ ) {
119
+ var tr = this . buildLibTRItem ( libs [ i ] , protocol ) ;
120
+ tbody . appendChild ( tr ) ;
133
121
}
134
122
135
- state . availableLibrariesDom . appendChild ( ul ) ;
136
- state . aleLibrariesDom . appendChild ( aleUl ) ;
123
+ const container_content = `
124
+ <div class="container">
125
+ <h3 class="header h3 text center">${ protocol } </h3>
126
+
127
+ <table class="search-table">
128
+ <thead>
129
+ <tr>
130
+ <th>Name</th>
131
+ <!-- <th>Protocol</th> -->
132
+ <th>Downloads</th>
133
+ <th>Description</th>
134
+ </tr>
135
+ <thead>
136
+ ${ tbody . outerHTML }
137
+ </table>
138
+ </div>
139
+ ` ;
140
+
141
+ return container_content ;
137
142
} ,
138
143
139
- buildLibListItem : function ( lib , protocol ) {
140
- var pName = this . aNode ( lib . name , lib . url ) ;
141
- var protocol = this . pNode ( protocol ) ;
142
- var description = this . pNode ( lib . description ) ;
143
- var li = document . createElement ( "li" ) ;
144
- li . className += " library-search-item" ;
145
- pName . className += " library-name" ;
146
- protocol . className += " protocol-name" ;
147
- description . className += "library-description" ;
148
- li . appendChild ( pName ) ;
149
- li . appendChild ( protocol ) ;
150
- li . appendChild ( description ) ;
151
-
152
- return li ;
144
+ buildLibTRItem : function ( lib , protocol ) {
145
+ var pName = this . tdaNode ( lib . name , lib . url ) ;
146
+ var protocol = this . tdNode ( protocol ) ;
147
+ var downloads = this . tdNode ( lib . downloads ) ;
148
+ var description = this . tdNode ( lib . description ) ;
149
+ var tr = document . createElement ( "tr" ) ;
150
+ tr . appendChild ( pName ) ;
151
+ //tr.appendChild(protocol);
152
+ tr . appendChild ( downloads ) ;
153
+ tr . appendChild ( description ) ;
154
+
155
+ return tr ;
153
156
} ,
154
157
155
158
pNode : function ( txt ) {
@@ -160,6 +163,26 @@ var hexSearch = {
160
163
return p ;
161
164
} ,
162
165
166
+ tdNode :function ( txt ) {
167
+ var txtNode = document . createTextNode ( txt ) ;
168
+ var td = document . createElement ( "td" ) ;
169
+ td . appendChild ( txtNode ) ;
170
+ return td ;
171
+ } ,
172
+
173
+ tdaNode :function ( txt , link ) {
174
+ var txtNode = document . createTextNode ( txt ) ;
175
+ var td = document . createElement ( "td" ) ;
176
+
177
+ var a = document . createElement ( "a" ) ;
178
+ a . appendChild ( txtNode ) ;
179
+ a . href = link ;
180
+ a . target = "blank" ;
181
+
182
+ td . appendChild ( a ) ;
183
+ return td ;
184
+ } ,
185
+
163
186
aNode : function ( txt , link ) {
164
187
var txtNode = document . createTextNode ( txt ) ;
165
188
var a = document . createElement ( "a" ) ;
0 commit comments