;(function($){ var getMetricPrefix = function(view) { var metric = view.getMetric(); if (view.params.view === 'team') { metric += '/News'; } return metric; }; $.articles = function(opts) { var view = Application.currentView; var defaults = { 'params': {}, 'id': null }; opts = $.extend(defaults, opts); var show_article = function(params) { if (params.id.substring(0) !== "#") { params.id = "#"+params.id; } view.params.activeArticle = params.id; $(document.body).addClass('displaying_article'); $('.article_wrapper').hide(); $(params.id).show(); window.scrollTo(0,1); if (Application.params.clientSupportsClose) { Client.notify({ 'buttons': ($('.summary_link').size() < 2) ? 'Close:' : 'Close:NudgeNav', 'metric': getMetricPrefix(view) + '/Detail', 'refreshAd': true }); } else { // Makes the back button to work Application.stashView(view.params); Client.notify({ "buttons": view.params.should_have_buttons, "metric": getMetricPrefix(view) + "/Detail", "title": view.getTitle(), 'refreshAd': true }); } }; var handlers = function() { $('.resizeable').resizeable(); $('.summaries').list_hl('.summary_link', function(e, el) { view.params.pageYOffset = window.pageYOffset; show_article({ 'id': el.attr('for') }); }); $('.share_btn') .off('click') .on('click', function(e) { var $this = $(this); var c = { 'title': $this.data('title'), 'summary': $this.data('summary'), 'url': $this.data('url'), 'platform': Application.client.ios ? 'iOS' : 'Android', 'img': $this.data('fbimg'), 'shortName': Application.params.shortName, 'longName': Application.params.longName, 'sharingUrl': Application.params.sharingUrl }; var email_message_tmpl = [ ['p', 'I found "{title}" and thought you might be interested: '.format(c), ['a', {'href': '@SHORTENED_URL@'}, '@SHORTENED_URL@'] ], ['p', '--'], ['p', 'Get more from {longName} while on the go. '.format(c), ['a', {'href': '{sharingUrl}'.format(c)}, 'Download'], ' it now.' ] ]; var twitter_message = '"{title}" @SHORTENED_URL@ via {shortName}'.format(c); var email_subject = '"{title}" via {longName} for {platform}'.format(c); var share_params = { 'action': 'share', 'twitter_message': twitter_message, 'facebook_name': c.title, 'facebook_message': c.summary, 'facebook_image': c.img, 'email_subject': email_subject, 'email_message': macrojungle(email_message_tmpl), 'url': c.url }; // Don't send these to iOS. See bug 24061. if (Application.client.android) { share_params.title = c.title; share_params.excerpt = c.summary; if (c.img) { share_params.img_url = c.img; } } Client.notify(share_params); }); }; var handle_notifications = function(params) { params.notifs = params.notifs || []; // Check if this notif has been viewed before. if (params.notifs[params.digest]) return; if ($('#'+params.digest).size() > 0) { Client.notify({ 'metric': getMetricPrefix(view) + '/Notification' }); show_article({ 'id': params.digest }); } else { if (Application.params.clientSupportsAlert) { Client.notify({ 'action': 'alert', 'heading': 'An error occurred', 'text': 'The requested article could not be found.' }); } else { alert('The requested article could not be found.'); } } // Mark that this notif has been viewed, or even though // the digest was bogus, mark that it was read so // the user isn't told the article doesn't exist over and over. params.notifs[params.digest] = true; }; // This is used with the Nudge Nav. if (opts.id) { show_article({ 'id': opts.id }); // This is the standard initialization. } else { $('.article_wrapper').hide(); $(document.body).removeClass('displaying_article'); handlers(); if (opts.params.digest) { handle_notifications(opts.params); } if (!Application.params.clientSupportsClose) { Application.currentView.params.should_have_buttons = ($('.summary_link').size() < 2) ? "Back:" : "Back:NudgeNav"; } } }; // Supports nudge navigation via client application. // Navigation loops. $.articlesClientCallback = function(name, value) { var view = Application.currentView; var params = view.params; var $article = $(params.activeArticle); if (name === 'close') { $('.article_wrapper').hide(); $(document.body).removeClass('displaying_article'); window.scrollTo(0, view.params.pageYOffset); Client.notify({ 'buttons': "Back:", 'metric': getMetricPrefix(view), 'refreshAd': true }); } if (name === 'nudge') { if (value === 'next' && $article.data('next')) { $.articles({ 'params': params, 'id': $article.data('next') }); } else if (value === 'prev' && $article.data('prev')) { $.articles({ 'params': params, 'id': $article.data('prev') }); } else { $.articles({ 'params': params, 'id': $('.article_wrapper').first().attr('id') }); } } }; })(Zepto);