Skip to content

Commit cec9303

Browse files
committed
Issues-202: refreshed menu items(mybookmarks,mydrafts)
1 parent 64ad454 commit cec9303

File tree

6 files changed

+275
-26
lines changed

6 files changed

+275
-26
lines changed

config/vanilla/bootstrap.before.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,57 @@ function filtersDropDown($baseUrl, array $filters = [], $extraClasses = '', $def
746746
return $output;
747747
}
748748
}
749+
750+
if (!function_exists('filterCountString')) {
751+
/**
752+
* This function was moved from 'vanilla/applications/vanilla/views/modules/discussionfilter.php'
753+
* @param $count
754+
* @param string $url
755+
* @param array $options
756+
* @return string
757+
*/
758+
function filterCountString($count, $url = '', $options = []) {
759+
$count = countString($count, $url, $options);
760+
return $count != '' ? '<span class="Aside">'.$count.'</span>' : '';
761+
}
762+
}
763+
764+
if (!function_exists('myBookmarksMenuItem')) {
765+
/**
766+
*
767+
*
768+
* @param $CountBookmarks
769+
* @return string
770+
*/
771+
function myBookmarksMenuItem($CountBookmarks) {
772+
if (!Gdn::session()->isValid()) {
773+
return '';
774+
}
775+
$Bookmarked = t('My Bookmarks');
776+
$Bookmarked .= FilterCountString($CountBookmarks, '/discussions/UserBookmarkCount');
777+
$cssClass = 'MyBookmarks';
778+
$cssClass .= Gdn::controller()->RequestMethod == 'bookmarked' ? ' Active' : '';
779+
$cssClass .= $CountBookmarks == 0 && Gdn::controller()->RequestMethod != 'bookmarked' ? ' hidden': '';
780+
return sprintf('<li id="MyBookmarks" class="%s">%s</li>', $cssClass, anchor(sprite('SpBookmarks').$Bookmarked, '/discussions/bookmarked'));
781+
}
782+
}
783+
784+
if (!function_exists('myDraftsMenuItem')) {
785+
/**
786+
*
787+
*
788+
* @param $CountBookmarks
789+
* @return string
790+
*/
791+
function myDraftsMenuItem($CountDrafts) {
792+
if (!Gdn::session()->isValid()) {
793+
return '';
794+
}
795+
$Drafts = t('My Drafts');
796+
$Drafts .= FilterCountString($CountDrafts, '/drafts');
797+
$cssClass = 'MyDrafts';
798+
$cssClass .= Gdn::controller()->ControllerName == 'draftscontroller' ? ' Active' : '';
799+
$cssClass .= $CountDrafts == 0 ? ' hidden': '';
800+
return sprintf('<li id="MyDrafts" class="%s">%s</li>', $cssClass, anchor(sprite('SpMyDrafts').$Drafts, '/drafts'));
801+
}
802+
}

vanilla/applications/vanilla/controllers/class.discussioncontroller.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ public function bookmark($DiscussionID = null) {
508508

509509
// Update the user's bookmark count
510510
$CountBookmarks = $this->DiscussionModel->setUserBookmarkCount($UserID);
511-
$this->jsonTarget('.User-CountBookmarks', (string)$CountBookmarks);
511+
$CountBookmarksHtml = myBookmarksMenuItem($CountBookmarks);
512+
$this->jsonTarget('#MyBookmarks', $CountBookmarksHtml, 'ReplaceWith');
512513

513514
// Short circuit if this is an api call.
514515
if ($this->deliveryType() === DELIVERY_TYPE_DATA) {

vanilla/applications/vanilla/controllers/class.postcontroller.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ public function discussion($categoryUrlCode = '') {
369369
$this->setJson('DiscussionID', $discussionID);
370370
$this->setJson('DraftID', $draftID);
371371

372+
// And update the draft count
373+
$DraftModel = new DraftModel();
374+
$CountDrafts = $DraftModel->getCountByUser($session->UserID);
375+
$this->setJson('CountDrafts', $CountDrafts);
376+
$this->setJson('MyDrafts', t('My Drafts'));
377+
372378
if (!$preview) {
373379
// If the discussion was not a draft
374380
if (!$draft) {
@@ -897,8 +903,8 @@ public function comment($DiscussionID = '') {
897903
$this->informMessage(sprintf(t('Draft saved at %s'), Gdn_Format::date()));
898904
}
899905
// And update the draft count
900-
$UserModel = Gdn::userModel();
901-
$CountDrafts = $UserModel->getAttribute($Session->UserID, 'CountDrafts', 0);
906+
$DraftModel = new DraftModel();
907+
$CountDrafts = $DraftModel->getCountByUser($Session->UserID);
902908
$this->setJson('MyDrafts', t('My Drafts'));
903909
$this->setJson('CountDrafts', $CountDrafts);
904910
}

vanilla/applications/vanilla/js/discussion.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,15 @@ jQuery(document).ready(function($) {
144144
if (json.DraftID != null && json.DraftID != '')
145145
$(inpDraftID).val(json.DraftID);
146146

147-
if (json.MyDrafts != null) {
148-
if (json.CountDrafts != null && json.CountDrafts > 0)
149-
json.MyDrafts += '<span>' + json.CountDrafts + '</span>';
150-
151-
$('ul#Menu li.MyDrafts a').html(json.MyDrafts);
147+
if (json.MyDrafts != null && json.CountDrafts != null) {
148+
var countMyDraftsHtml = '<span aria-hidden="true" class="Sprite SpMyDrafts"></span> My Drafts';
149+
if(json.CountDrafts > 0) {
150+
countMyDraftsHtml += '<span class="Aside"><span class="Count">' + json.CountDrafts + '</span></span>';
151+
$('li#MyDrafts').removeClass('hidden');
152+
} else {
153+
$('li#MyDrafts').addClass('hidden');
154+
}
155+
$('li#MyDrafts a').html(countMyDraftsHtml);
152156
}
153157

154158
// Remove any old errors from the form
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
jQuery(document).ready(function($) {
2+
3+
// Reveal the textarea and hide previews.
4+
$(document).on('click', 'a.WriteButton', function() {
5+
if ($(this).hasClass('WriteButton')) {
6+
var frm = $(this).parents('.MessageForm').find('form');
7+
frm.trigger('WriteButtonClick', [frm]);
8+
9+
// Reveal the "Preview" button and hide this one
10+
$(this).parents('.DiscussionForm').find('.PreviewButton').show();
11+
$(this).addClass('Hidden');
12+
}
13+
resetDiscussionForm(this);
14+
return false;
15+
});
16+
17+
function resetDiscussionForm(sender) {
18+
var parent = $(sender).parents('.DiscussionForm, .EditDiscussionForm');
19+
$(parent).find('.Preview').remove();
20+
$(parent).find('.bodybox-wrap .TextBoxWrapper').show();
21+
}
22+
23+
// Hijack comment form button clicks
24+
$('#CommentForm :submit').click(function() {
25+
var btn = this;
26+
var frm = $(btn).parents('form').get(0);
27+
28+
// Handler before submitting
29+
$(frm).triggerHandler('BeforeCommentSubmit', [frm, btn]);
30+
31+
var textbox = $(frm).find('textarea');
32+
var inpCommentID = $(frm).find('input:hidden[name$=CommentID]');
33+
var inpDraftID = $(frm).find('input:hidden[name$=DraftID]');
34+
var preview = $(btn).attr('name') == $('#Form_Preview').attr('name') ? true : false;
35+
var draft = $(btn).attr('name') == $('#Form_SaveDraft').attr('name') ? true : false;
36+
var postValues = $(frm).serialize();
37+
postValues += '&DeliveryType=VIEW&DeliveryMethod=JSON'; // DELIVERY_TYPE_VIEW
38+
postValues += '&' + btn.name + '=' + btn.value;
39+
var discussionID = $(frm).find('[name$=DiscussionID]').val();
40+
var action = $(frm).attr('action') + '/' + discussionID;
41+
gdn.disable(btn);
42+
43+
$.ajax({
44+
type: "POST",
45+
url: action,
46+
data: postValues,
47+
dataType: 'json',
48+
error: function(XMLHttpRequest, textStatus, errorThrown) {
49+
// Remove any old popups
50+
$('div.Popup').remove();
51+
// Add new popup with error
52+
$.popup({}, XMLHttpRequest.responseText);
53+
},
54+
success: function(json) {
55+
// Remove any old popups if not saving as a draft
56+
if (!draft)
57+
$('div.Popup').remove();
58+
59+
// Assign the comment id to the form if it was defined
60+
if (json.CommentID != null && json.CommentID != '') {
61+
$(inpCommentID).val(json.CommentID);
62+
gdn.definition('LastCommentID', json.CommentID, true);
63+
}
64+
65+
if (json.DraftID != null && json.DraftID != '')
66+
$(inpDraftID).val(json.DraftID);
67+
68+
// Remove any old errors from the form
69+
$(frm).find('div.Errors').remove();
70+
71+
if (json.FormSaved == false) {
72+
$(frm).prepend(json.ErrorMessages);
73+
json.ErrorMessages = null;
74+
} else if (preview) {
75+
// Pop up the new preview.
76+
$.popup({}, json.Data);
77+
} else if (!draft && json.DiscussionUrl != null) {
78+
$(frm).triggerHandler('complete');
79+
// Redirect to the discussion
80+
document.location = json.DiscussionUrl;
81+
}
82+
gdn.inform(json);
83+
},
84+
complete: function(XMLHttpRequest, textStatus) {
85+
gdn.enable(btn);
86+
}
87+
});
88+
$(frm).triggerHandler('submit');
89+
return false;
90+
});
91+
92+
// Hijack discussion form button clicks
93+
//$('#DiscussionForm :submit').live('click', function() {
94+
95+
// Jan28, 2014 jQuery upgrade to 1.10.2, as live() removed in 1.7.
96+
$(document).on('click', '#DiscussionForm :submit', function() {
97+
var btn = this;
98+
var frm = $(btn).parents('form').get(0);
99+
100+
// Handler before submitting
101+
$(frm).triggerHandler('BeforeDiscussionSubmit', [frm, btn]);
102+
103+
var inpDiscussionID = $(frm).find(':hidden[name$=DiscussionID]');
104+
var inpDraftID = $(frm).find(':hidden[name$=DraftID]');
105+
var preview = $(btn).attr('name') == $('#Form_Preview').attr('name') ? true : false;
106+
var draft = $(btn).attr('name') == $('#Form_SaveDraft').attr('name') ? true : false;
107+
var postValues = $(frm).serialize();
108+
postValues += '&DeliveryType=VIEW&DeliveryMethod=JSON'; // DELIVERY_TYPE_VIEW
109+
postValues += '&' + btn.name + '=' + btn.value;
110+
gdn.disable(btn);
111+
112+
$.ajax({
113+
type: "POST",
114+
url: $(frm).attr('action'),
115+
data: postValues,
116+
dataType: 'json',
117+
error: function(XMLHttpRequest, textStatus, errorThrown) {
118+
$('div.Popup').remove();
119+
$.popup({}, XMLHttpRequest.responseText);
120+
},
121+
success: function(json) {
122+
// Remove any old popups if not saving as a draft
123+
if (!draft)
124+
$('div.Popup').remove();
125+
126+
// Assign the discussion id to the form if it was defined
127+
if (json.DiscussionID != null)
128+
$(inpDiscussionID).val(json.DiscussionID);
129+
130+
if (json.DraftID != null)
131+
$(inpDraftID).val(json.DraftID);
132+
133+
// Remove any old errors from the form
134+
$(frm).find('div.Errors').remove();
135+
136+
if (json.MyDrafts != null && json.CountDrafts != null) {
137+
var countMyDraftsHtml = '<span aria-hidden="true" class="Sprite SpMyDrafts"></span> My Drafts';
138+
if(json.CountDrafts > 0) {
139+
countMyDraftsHtml += '<span class="Aside"><span class="Count">' + json.CountDrafts + '</span></span>';
140+
$('li#MyDrafts').removeClass('hidden');
141+
} else {
142+
$('li#MyDrafts').addClass('hidden');
143+
}
144+
$('li#MyDrafts a').html(countMyDraftsHtml);
145+
}
146+
147+
if (json.FormSaved == false) {
148+
$(frm).prepend(json.ErrorMessages);
149+
json.ErrorMessages = null;
150+
} else if (preview) {
151+
// Reveal the "Edit" button and hide this one
152+
$(btn).hide();
153+
$(frm).find('.WriteButton').removeClass('Hidden');
154+
155+
$(frm).find('.bodybox-wrap .TextBoxWrapper').hide().after(json.Data);
156+
$(frm).trigger('PreviewLoaded', [frm]);
157+
} else if (!draft) {
158+
if (json.RedirectTo) {
159+
$(frm).triggerHandler('complete');
160+
// Redirect to the new discussion
161+
document.location = json.RedirectTo;
162+
} else {
163+
var contentContainer = $("#Content");
164+
165+
if (contentContainer.length === 1) {
166+
contentContainer.html(json.Data);
167+
} else {
168+
// Hack to emulate a content container.
169+
contentContainer = $(document.createElement("div"));
170+
contentContainer.html(json.Data);
171+
$(frm).replaceWith(contentContainer);
172+
}
173+
}
174+
}
175+
gdn.inform(json);
176+
},
177+
complete: function(XMLHttpRequest, textStatus) {
178+
gdn.enable(btn);
179+
}
180+
});
181+
$(frm).triggerHandler('submit');
182+
return false;
183+
});
184+
185+
// Autosave
186+
if ($.fn.autosave) {
187+
var btn = $('#Form_SaveDraft');
188+
189+
$('#CommentForm textarea').autosave({
190+
button: btn
191+
});
192+
$('#DiscussionForm textarea').autosave({
193+
button: btn
194+
});
195+
}
196+
});

vanilla/applications/vanilla/views/modules/discussionfilter.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
$CountDiscussions = $Session->User->CountDiscussions;
1919
$CountDrafts = $Session->User->CountDrafts;
2020
}
21+
// function_exists('filterCountString') was moved to 'bootstrap.before.php' to re-use it
2122

22-
if (!function_exists('FilterCountString')) {
23-
function filterCountString($count, $url = '') {
24-
$count = countString($count, $url);
25-
return $count != '' ? '<span class="Aside">'.$count.'</span>' : '';
26-
}
27-
}
2823
if (c('Vanilla.Discussions.ShowCounts', true)) {
29-
$Bookmarked .= filterCountString($CountBookmarks, '/discussions/UserBookmarkCount');
24+
//$Bookmarked .= filterCountString($CountBookmarks, '/discussions/UserBookmarkCount', ['cssclass' => 'User-CountBookmarks']);
3025
$MyDiscussions .= filterCountString($CountDiscussions);
3126
$MyDrafts .= filterCountString($CountDrafts);
3227
}
@@ -50,21 +45,14 @@ function filterCountString($count, $url = '') {
5045
echo '<li class="'.$CssClass.'">'.anchor(sprite('SpAllCategories').' '.t('All Categories', 'Categories'), '/categories').'</li> ';
5146
}
5247
?>
53-
<li class="Discussions<?php echo strtolower($Controller->ControllerName) == 'discussionscontroller' && strtolower($Controller->RequestMethod) == 'index' ? ' Active' : ''; ?>"><?php echo Gdn_Theme::link('forumroot', sprite('SpDiscussions').' '.t('Recent Discussions')); ?></li>
54-
<?php if ($CountBookmarks > 0 || $Controller->RequestMethod == 'bookmarked') { ?>
55-
<li class="MyBookmarks<?php echo $Controller->RequestMethod == 'bookmarked' ? ' Active' : ''; ?>"><?php echo anchor(sprite('SpBookmarks').' '.$Bookmarked, '/discussions/bookmarked'); ?></li>
56-
<?php
57-
}
58-
if (($CountDiscussions > 0 || $Controller->RequestMethod == 'mine') && c('Vanilla.Discussions.ShowMineTab', true)) {
48+
<li id="RecentDiscussions" class="Discussions<?php echo strtolower($Controller->ControllerName) == 'discussionscontroller' && strtolower($Controller->RequestMethod) == 'index' ? ' Active' : ''; ?>"><?php echo Gdn_Theme::link('forumroot', sprite('SpDiscussions').' '.t('Recent Discussions')); ?></li>
49+
<?php echo myBookmarksMenuItem($CountBookmarks); ?>
50+
<?php if (($CountDiscussions > 0 || $Controller->RequestMethod == 'mine') && c('Vanilla.Discussions.ShowMineTab', true)) {
5951
?>
6052
<li class="MyDiscussions<?php echo $Controller->ControllerName == 'discussionscontroller' && $Controller->RequestMethod == 'mine' ? ' Active' : ''; ?>"><?php echo anchor(sprite('SpMyDiscussions').' '.$MyDiscussions, '/discussions/mine'); ?></li>
6153
<?php
6254
}
63-
if ($CountDrafts > 0 || $Controller->ControllerName == 'draftscontroller') {
64-
?>
65-
<li class="MyDrafts<?php echo $Controller->ControllerName == 'draftscontroller' ? ' Active' : ''; ?>"><?php echo anchor(sprite('SpMyDrafts').' '.$MyDrafts, '/drafts'); ?></li>
66-
<?php
67-
}
55+
echo myDraftsMenuItem($CountDrafts);
6856
$Controller->fireEvent('AfterDiscussionFilters');
6957
?>
7058
</ul>

0 commit comments

Comments
 (0)