Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to change item tag #43

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions assets/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ var Notifications = (function(options) {
markAllSeenSelector: null,
deleteAllSelector: null,
listSelector: null,
tag: "<div>",
listItemTemplate:
'<div class="row">' +
'<div class="col-xs-10">' +
Expand Down Expand Up @@ -204,14 +205,17 @@ var Notifications = (function(options) {
* @returns {jQuery|HTMLElement|*}
*/
this.renderRow = function (object) {
var keywords = ['id', 'title', 'description', 'url', 'type'];
var ret, html = self.opts.listItemTemplate;

html = '<div class="notification notification-{type} ' +
(object.seen ? 'notification-seen' : 'notification-unseen') +
'" data-route="{url}" data-id="{id}">' +
html +
'</div>';
var keywords = ['title', 'description', 'url'];
var ret, html;

html = $(self.opts.tag, {
class: "notification notification-" + object.type + " "+(object.seen ? 'notification-seen' : 'notification-unseen'),
"data-route": object.url,
"data-id": object.id,
html: self.opts.listItemTemplate
})[0].outerHTML;



for (var i = 0; i < keywords.length; i++) {
html = html.replace(new RegExp('{' + keywords[i] + '}', 'g'), object[keywords[i]]);
Expand All @@ -227,8 +231,10 @@ var Notifications = (function(options) {

// Update all counters
for (var i = 0; i < self.opts.counters.length; i++) {
if ($(self.opts.counters[i]).text() != parseInt($(self.opts.counters[i]).html())-1) {
$(self.opts.counters[i]).text(parseInt($(self.opts.counters[i]).html())-1);
var currentCounter = $(self.opts.counters[i]),
targetCount = parseInt(currentCounter.html()) - 1;
if (currentCounter.text() != targetCount) {
currentCounter.text(targetCount || "");
}
}

Expand Down Expand Up @@ -348,8 +354,9 @@ var Notifications = (function(options) {

// Update all counters
for (var i = 0; i < self.opts.counters.length; i++) {
if ($(self.opts.counters[i]).text() != data.length) {
$(self.opts.counters[i]).text(data.length);
var counter=$(self.opts.counters[i]);
if (counter.text() != data.length) {
counter.text(data.length || "");
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ NotificationsWidget::widget([
| listSelector | A jQuery selector for your UI element that will holds the notification list | null |
| listItemTemplate | An optional template for the list item. | built-in |
| listItemBeforeRender | An optional callback to tweak the list item layout before rendering | empty cb |
| tag | The name of the tag used for each notification | 'div' |


Supported libraries
Expand Down
6 changes: 6 additions & 0 deletions widgets/NotificationsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class NotificationsWidget extends Widget
*/
public $listSelector = null;

/**
* @var string The wrapper tag for each item
*/
public $tag = "div";

/**
* @var string The list item HTML template
*/
Expand Down Expand Up @@ -205,6 +210,7 @@ public function registerAssets()
'pollSeen' => !!$this->pollSeen,
'pollInterval' => Html::encode($this->pollInterval),
'counters' => $this->counters,
'tag' => Html::beginTag($this->tag)
];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the user providing only the tag name, shouldn't we enable him to pass also attributes?
Maybe move the processing in the Javascript, I don't like that we're calling Yii just to get '<' '>'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little modification. I have seen that javascript template can be impoved in many ways. I have a few ideas, but now i´m busy with other projects. When i have time i´ll try to do it.


if ($this->theme) {
Expand Down