File indexing completed on 2024-12-22 05:34:45
0001 window.hpHelpers = (function(){ 0002 0003 function dechex(number) { 0004 // discuss at: http://locutus.io/php/dechex/ 0005 // original by: Philippe Baumann 0006 // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) 0007 // improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript 0008 // input by: pilus 0009 // example 1: dechex(10) 0010 // returns 1: 'a' 0011 // example 2: dechex(47) 0012 // returns 2: '2f' 0013 // example 3: dechex(-1415723993) 0014 // returns 3: 'ab9dc427' 0015 0016 if (number < 0) { 0017 number = 0xFFFFFFFF + number + 1 0018 } 0019 return parseInt(number, 10).toString(16) 0020 } 0021 0022 function calculateScoreColor(score){ 0023 let blue, red, green, defaultColor = 200; 0024 if (score > 50){ 0025 red = defaultColor - ((score-50)*4); 0026 green = defaultColor; 0027 blue = defaultColor - ((score-50)*4); 0028 } else if (score < 51){ 0029 red = defaultColor; 0030 green = defaultColor - ((score-50)*4); 0031 blue = defaultColor - ((score-50)*4); 0032 } 0033 0034 return "rgb("+red+","+green+","+blue+")"; 0035 } 0036 0037 return { 0038 dechex, 0039 calculateScoreColor 0040 } 0041 }()); 0042 0043 class App extends React.Component { 0044 constructor(props){ 0045 super(props); 0046 this.state = { 0047 loading:true, 0048 hpVersion:window.hpVersion 0049 }; 0050 this.initHomePage = this.initHomePage.bind(this); 0051 this.updateDimensions = this.updateDimensions.bind(this); 0052 } 0053 0054 0055 componentWillMount() { 0056 this.updateDimensions(); 0057 } 0058 0059 componentWillUnmount(){ 0060 window.removeEventListener("resize", this.updateDimensions); 0061 window.removeEventListener("orientationchange",this.updateDimensions); 0062 } 0063 0064 componentDidMount() { 0065 this.initHomePage(); 0066 } 0067 0068 initHomePage(){ 0069 0070 window.addEventListener("resize", this.updateDimensions); 0071 window.addEventListener("orientationchange",this.updateDimensions); 0072 0073 let env = "live"; 0074 if (location.hostname.endsWith('cc')) { 0075 env = "test"; 0076 } else if (location.hostname.endsWith('localhost')) { 0077 env = "test"; 0078 } 0079 0080 this.setState({env:env}); 0081 } 0082 0083 updateDimensions(){ 0084 0085 const width = window.innerWidth; 0086 let device; 0087 if (width >= 910){ 0088 device = "large"; 0089 } else if (width < 910 && width >= 610){ 0090 device = "mid"; 0091 } else if (width < 610){ 0092 device = "tablet"; 0093 } 0094 0095 this.setState({device:device}); 0096 0097 } 0098 0099 render(){ 0100 const featuredProduct = JSON.parse(window.data['featureProducts']); 0101 return ( 0102 <main id="opendesktop-homepage"> 0103 <SpotlightProduct 0104 env={this.state.env} 0105 device={this.state.device} 0106 featuredProduct={featuredProduct} 0107 /> 0108 </main> 0109 ) 0110 } 0111 } 0112 0113 class SpotlightProduct extends React.Component { 0114 constructor(props){ 0115 super(props); 0116 this.state = { 0117 featuredProduct:this.props.featuredProduct, 0118 type:"featured", 0119 featuredPage:0, 0120 loading:true 0121 }; 0122 this.onSpotlightMenuClick = this.onSpotlightMenuClick.bind(this); 0123 } 0124 0125 componentDidMount() { 0126 this.onSpotlightMenuClick('plinged'); 0127 } 0128 0129 onSpotlightMenuClick(val){ 0130 0131 this.setState({type:val,loading:true},function(){ 0132 0133 let url = "/home/showfeaturejson/page/"; 0134 let featuredPage = this.state.featuredPage; 0135 if (this.state.type === "plinged"){ 0136 url = "/home/getnewactiveplingedproductjson?limit=1&offset=" + this.state.featuredPage; 0137 featuredPage = this.state.featuredPage + 1; 0138 } else if (this.state.type === "random"){ 0139 url += "0"; 0140 } else { 0141 url += "1"; 0142 } 0143 const self = this; 0144 0145 $.ajax({url: url,cache: false}).done(function(response){ 0146 0147 let featuredProduct = response; 0148 if (self.state.type === "plinged"){ 0149 featuredProduct = response[0]; 0150 } 0151 0152 console.log(featuredProduct); 0153 0154 self.setState({ 0155 featuredProduct:featuredProduct, 0156 featuredPage:featuredPage, 0157 loading:false 0158 }); 0159 }); 0160 }); 0161 } 0162 0163 render(){ 0164 0165 let spotlightProductDisplay; 0166 if (this.state.loading){ 0167 spotlightProductDisplay = ( 0168 <SpotlightProductDummy /> 0169 ); 0170 } else { 0171 0172 let productImageUrl; 0173 if (this.state.type === "plinged"){ 0174 productImageUrl = this.state.featuredProduct.image_small; 0175 } else { 0176 let imageBaseUrl; 0177 if (this.props.env === 'live') { 0178 imageBaseUrl = 'cn.opendesktop.org'; 0179 } else { 0180 imageBaseUrl = 'cn.opendesktop.cc'; 0181 } 0182 productImageUrl = "https://" + imageBaseUrl + "/cache/300x230-1/img/" + this.state.featuredProduct.image_small; 0183 } 0184 0185 let description = this.state.featuredProduct.description; 0186 if (description && description.length > 295){ 0187 description = this.state.featuredProduct.description.substring(0,295) + "..."; 0188 } 0189 0190 let featuredLabelDisplay; 0191 if (this.state.type === "featured"){ 0192 featuredLabelDisplay = ( 0193 <span className="featured-label">featured</span> 0194 ); 0195 } else if (this.state.type === "plinged"){ 0196 featuredLabelDisplay = ( 0197 <span className="featured-label plinged">plinged</span> 0198 ); 0199 } 0200 0201 let cDate = new Date(this.state.featuredProduct.created_at); 0202 cDate = cDate.toString(); 0203 const createdDate = cDate.split(' ')[1] + " " + cDate.split(' ')[2] + " " + cDate.split(' ')[3]; 0204 // const productScoreColor = window.hpHelpers.calculateScoreColor(this.state.featuredProduct.laplace_score); 0205 0206 let commentCount; 0207 if (this.state.featuredProduct.count_comments){ 0208 commentCount = this.state.featuredProduct.count_comments; 0209 } else { 0210 commentCount = "0"; 0211 } 0212 0213 let categoryDisplay = this.state.featuredProduct.category; 0214 if (this.state.type === "plinged"){ 0215 categoryDisplay = this.state.featuredProduct.cat_title; 0216 } 0217 0218 spotlightProductDisplay = ( 0219 <div className="container"> 0220 <div className="spotlight-image"> 0221 <img className="product-image" src={productImageUrl}/> 0222 <figure className="user-avatar"> 0223 <img src={this.state.featuredProduct.profile_image_url}/> 0224 </figure> 0225 </div> 0226 <div className="spotlight-info"> 0227 <div className="spotlight-info-wrapper"> 0228 {featuredLabelDisplay} 0229 <div className="info-top"> 0230 <h2><a href={"/p/" + this.state.featuredProduct.project_id}>{this.state.featuredProduct.title}</a></h2> 0231 <h3>{categoryDisplay}</h3> 0232 <div className="user-info"> 0233 <img src={this.state.featuredProduct.profile_image_url}/> 0234 {this.state.featuredProduct.username} 0235 </div> 0236 <span>{commentCount} comments</span> 0237 <div className="score-info"> 0238 <div className="score-number"> 0239 score {this.state.featuredProduct.laplace_score + "%"} 0240 </div> 0241 <div className="score-bar-container"> 0242 <div className="score-bar" style={{"width":this.state.featuredProduct.laplace_score + "%"}}></div> 0243 </div> 0244 <div className="score-bar-date"> 0245 {createdDate} 0246 </div> 0247 </div> 0248 </div> 0249 <div className="info-description"> 0250 {description} 0251 </div> 0252 </div> 0253 <div className="spotlight-menu"> 0254 <a onClick={() => this.onSpotlightMenuClick('random')}>random</a> 0255 <a onClick={() => this.onSpotlightMenuClick('featured')}>featured</a> 0256 <a onClick={() => this.onSpotlightMenuClick('plinged')}>plinged</a> 0257 </div> 0258 </div> 0259 </div> 0260 ); 0261 } 0262 0263 return( 0264 <div id="spotlight-product"> 0265 <h2>In the Spotlight</h2> 0266 {spotlightProductDisplay} 0267 </div> 0268 ); 0269 } 0270 } 0271 0272 class SpotlightProductDummy extends React.Component { 0273 constructor(props){ 0274 super(props); 0275 this.state = {}; 0276 } 0277 0278 render(){ 0279 return( 0280 <div className="container dummy-product"> 0281 <div className="spotlight-image"> 0282 <figure className="user-avatar"> 0283 <div className="ajax-loader"></div> 0284 </figure> 0285 </div> 0286 <div className="spotlight-info"> 0287 <div className="spotlight-info-wrapper"> 0288 <div className="info-top"> 0289 <h2></h2> 0290 <h3></h3> 0291 <div className="user-info"> 0292 <figure><span className="glyphicon glyphicon-user"></span></figure> 0293 <span></span> 0294 </div> 0295 <span className="comments-count"></span> 0296 <div className="score-info"> 0297 <div className="score-number"></div> 0298 <div className="score-bar-container"> 0299 <div className="score-bar" style={{"width":"50%"}}></div> 0300 </div> 0301 <div className="score-bar-date"></div> 0302 </div> 0303 </div> 0304 <div className="info-description"> 0305 <span></span> 0306 <span></span> 0307 <span></span> 0308 <span className="half"></span> 0309 </div> 0310 </div> 0311 </div> 0312 </div> 0313 ); 0314 } 0315 } 0316 0317 class SpotlightUser extends React.Component { 0318 constructor(props){ 0319 super(props); 0320 this.state = { 0321 loading:true, 0322 version:2 0323 }; 0324 this.updateDimensions = this.updateDimensions.bind(this); 0325 this.getSpotlightUser = this.getSpotlightUser.bind(this); 0326 } 0327 0328 componentWillMount() { 0329 window.addEventListener("resize", this.updateDimensions); 0330 } 0331 0332 componentDidMount() { 0333 this.updateDimensions(); 0334 this.getSpotlightUser(); 0335 } 0336 0337 updateDimensions(){ 0338 const containerWidth = $('#main-content').width(); 0339 const userProductsPerRow = 4; 0340 const userProductsDimensions = containerWidth / userProductsPerRow; 0341 this.setState({itemWidth:userProductsDimensions,itemHeight:userProductsDimensions}); 0342 } 0343 0344 getSpotlightUser(page){ 0345 if (!page) { page = 0 } 0346 this.setState({loading:true,page: page},function(){ 0347 let url = "/home/showspotlightjson?page=" + this.state.page; 0348 const self = this; 0349 $.ajax({url: url,cache: false}).done(function(response){ 0350 self.setState({user:response,loading:false},function(){ 0351 const height = $('#user-container').height(); 0352 if (height > 0){ 0353 this.setState({containerHeight:height}); 0354 } 0355 }); 0356 }); 0357 }); 0358 } 0359 0360 render(){ 0361 0362 let spotlightUserDisplay; 0363 if (this.state.loading){ 0364 let loadingStyle; 0365 if (this.state.containerHeight){ 0366 loadingStyle = { 0367 "height":this.state.containerHeight 0368 } 0369 } 0370 spotlightUserDisplay = ( 0371 <div className="loading-container" style={loadingStyle}> 0372 <div className="ajax-loader"></div> 0373 </div> 0374 ); 0375 } else { 0376 let userProducts; 0377 if (this.state.itemWidth){ 0378 userProducts = this.state.user.products.map((p,index) => ( 0379 <SpotlightUserProduct 0380 key={index} 0381 itemHeight={this.state.itemHeight} 0382 itemWidth={this.state.itemWidth} 0383 product={p} 0384 /> 0385 )); 0386 } 0387 spotlightUserDisplay = ( 0388 <div id="spotlight-user"> 0389 <div className="user-container"> 0390 <figure> 0391 <img src={this.state.user.profile_image_url}/> 0392 </figure> 0393 <h2><a href={"/u/"+this.state.user.username}>{this.state.user.username}</a></h2> 0394 </div> 0395 <div className="products-container"> 0396 {userProducts} 0397 </div> 0398 </div> 0399 ); 0400 } 0401 0402 let prevButtonDisplay; 0403 if (this.state.page > 0){ 0404 prevButtonDisplay = ( 0405 <a onClick={() => this.getSpotlightUser(this.state.page - 1)} className="spotlight-user-next"> 0406 {"< Prev"} 0407 </a> 0408 ); 0409 } 0410 0411 let nextButtonDisplay; 0412 if (this.state.page < 8){ 0413 nextButtonDisplay = ( 0414 <a onClick={() => this.getSpotlightUser(this.state.page + 1)} className="spotlight-user-next"> 0415 {"Next >"} 0416 </a> 0417 ); 0418 } 0419 0420 let versionCssClass; 0421 if (this.state.version === 2){ 0422 versionCssClass = "v-two" 0423 } 0424 0425 return( 0426 <div id="spotlight-user-container" className={versionCssClass}> 0427 <h2>In the Spotlight</h2> 0428 {spotlightUserDisplay} 0429 <div className="spotlight-user-buttons"> 0430 {prevButtonDisplay} 0431 {nextButtonDisplay} 0432 </div> 0433 </div> 0434 ) 0435 } 0436 } 0437 0438 class SpotlightUserProduct extends React.Component { 0439 constructor(props){ 0440 super(props); 0441 this.state = {}; 0442 } 0443 0444 componentDidMount() { 0445 console.log(this.props); 0446 } 0447 0448 render(){ 0449 let userProductStyle; 0450 if (this.props.itemWidth){ 0451 userProductStyle = { 0452 "height":this.props.itemHeight, 0453 "width":this.props.itemWidth 0454 } 0455 } 0456 return ( 0457 <div style={userProductStyle} className="spotlight-user-product"> 0458 <figure> 0459 <img src={this.props.product.image_small}/> 0460 </figure> 0461 <div className="product-title-overlay"> 0462 <div className="product-title"> 0463 {this.props.product.title} 0464 </div> 0465 </div> 0466 <div className="product-plings-counter"> 0467 <img src="/images/system/pling-btn-active.png"/> 0468 <span>{this.props.product.sum_plings}</span> 0469 </div> 0470 </div> 0471 ) 0472 } 0473 } 0474 0475 ReactDOM.render( 0476 <App />, 0477 document.getElementById('main-content') 0478 );