(function( window, document, $, _ ) { /** * @namespace window.TDR.RECOMMENDCONTENTS * @file recommendcontents.js * @return none */ "use strict"; var TDR = window.TDR = window.TDR || {}; TDR.RECOMMENDCONTENTS = TDR.RECOMMENDCONTENTS || {}; /** * @param {string} str_id Parameter. * @param {string} display_block_id Display block id, it does not display processing. * @param {string} parent_block_id Parent block id. If null, it does not display processing. * @param {number} max Max number. If null, the I mean the maximum. * @param {string} fqdn Hostname. * @return {jQuery.Deferred} */ function RecommendContents( str_id, display_block_id, parent_block_id, max, fqdn ) { try{ if( typeof str_id !== 'string' ) throw 'initialize_error'; if( typeof display_block_id !== 'string' ) throw 'initialize_error'; if( typeof parent_block_id !== 'string' && parent_block_id !== null ) throw 'initialize_error'; if( typeof max !== 'number' && max !== null ) throw 'initialize_error'; if( typeof fqdn !== 'string' ) throw 'initialize_error'; } catch(e) { $('body').trigger( $.Event( 'onLoadRecommendcontents' ), [ '', { result:false, total_count:0 } ] ); return false; } this.protocol = '//'; //this.protocol = 'http://'; this.fqdn = fqdn; this.pathname = '/api/v1/wapi_recommendcontents/lists/'; this.str_id = str_id; this.total_count = 0; this.max = max; // cache variable this.parsed_data = null; this.$body = $( 'body' ); this.$display_block = $( '#' + display_block_id ); this.$parent_block = $( '#' + parent_block_id ); this.requestURL = [ this.protocol, this.fqdn, this.pathname, 'str_id:', this.str_id, '/' ].join( '' ); this.template = [ '{{each( index, value ) list}}', '
', ' ', ' {{if thum_url_pc != ""}}{{/if}}', '
', ' {{if name != ""}}
{{html name}}
{{/if}}', ' {{if text != ""}}
{{html text}}
{{/if}}', '
', '
', '
', '{{/each}}' ].join( '' ); var $dfd = $.Deferred(); this._request( $dfd ); return $dfd.promise(); } RecommendContents.prototype = { _request : function($dfd) { var _this = this; $.ajax( { url: this.requestURL, type: 'GET', dataType: 'jsonp', data: { _: Math.floor(new Date().getTime() / TDR.CONFIG.CacheTime) }, cache : true, beforeSend : function( xhr ) { xhr.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT"); }, jsonpCallback: 'TDR_RecommendContents_' + this.str_id }).done(function( res, status, xhr ) { try{ if( res.result_code !== '0' ) throw 'request error.'; _this.total_count = res.total_count; _this._parser( res ); _this._render(); $dfd.resolve( _this ); } catch( e ) { _this.$body.trigger( $.Event( 'onLoadRecommendcontents' ), [ _this.requestURL, { result:false, total_count:0 } ] ); $dfd.reject(); return false; } }).fail(function( xhr, status, error ) { _this.$body.trigger( $.Event( 'onLoadRecommendcontents' ), [ _this.requestURL, { result:false, total_count:0 } ] ); $dfd.reject(); return false; }).always(function( xhr, status ) { }); }, _parser : function( data ) { var _this = this, target_data_list = data.entries, parsed_data_list = []; _( target_data_list ).forEach(function( data ) { var target_data = data; if( typeof _this.parsed_data_extends === 'function' ) { target_data = _this.parsed_data_extends( _this, target_data ); } parsed_data_list.push( target_data ); }); if( this.max !== null ) parsed_data_list = parsed_data_list.slice( 0, this.max ); this.parsed_data = { list:parsed_data_list }; }, _render : function() { var html = $.tmpl( this.template, this.parsed_data ); this.$display_block.html( html ); if( this.$parent_block.length !== 0 && this.parsed_data.list.length !== 0 ) { this.$parent_block.removeClass( 'js-display-none' ); } this.$body.trigger( $.Event( 'onLoadRecommendcontents' ), [ this.requestURL, { result:true, total_count:this.total_count } ] ); } }; TDR.RECOMMENDCONTENTS = RecommendContents; })( window, document, jQuery, _ );