MediaWiki:Gadget-CommentsToVk.js: различия между версиями

Материал из ВикиФизтех
Перейти к навигации Перейти к поиску
imported>Admin2
imported>Admin2
 
(не показано 40 промежуточных версий этого же участника)
Строка 61: Строка 61:
 
buttons : {
 
buttons : {
 
Submit : function() {
 
Submit : function() {
 
+
instance.publish();
 
},
 
},
 
Cancel : function() {
 
Cancel : function() {
Строка 112: Строка 112:
 
this.publishFormLink.val(link);
 
this.publishFormLink.val(link);
 
this.publishDialog.dialog('open');
 
this.publishDialog.dialog('open');
 +
 +
var lastDate;
 +
if (typeof localStorage['CommentsToVk_lastDate'] === 'undefined') {
 +
lastDate = new Date();
 +
} else {
 +
var lastDate = new Date(1 * localStorage['CommentsToVk_lastDate']);
 +
lastDate = new Date(Math.max(new Date().getTime(), lastDate
 +
.getTime()));
 +
}
 +
nextDate = new Date(
 +
(Math.floor(lastDate.getTime() / 1000.0 / 60 / 60) + 1) * 1000 * 60 * 60);
  
 
this.publishFormTime.datetimepicker({
 
this.publishFormTime.datetimepicker({
format : $.datetimepicker.ISO_8601,
+
format : 'H:i:s d.m.Y',
minDate : new Date().toISOString().split('T')[0],
+
value : nextDate.toLocaleTimeString("ru-RU") + ' '
value : new Date().toISOString()
+
+ nextDate.toLocaleDateString("ru-RU")
 +
});
 +
}
 +
 
 +
CommentToVk.prototype.publish = function() {
 +
var instance = this;
 +
var link = this.publishFormLink.val();
 +
var text = this.publishFormText.val();
 +
var date = this.publishFormTime.datetimepicker('getValue');
 +
var timestamp = Math.round(date.getTime() / 1000);
 +
 
 +
VK.Api.call('wall.post', {
 +
owner_id : -47630162,
 +
from_group : 1,
 +
message : text,
 +
attachments : link,
 +
signed : 0,
 +
publish_date : Math.floor(date.getTime() / 1000),
 +
}, function(r) {
 +
console.log(r);
 +
if (r.response) {
 +
localStorage['CommentsToVk_lastDate'] = date.getTime();
 +
instance.publishDialog.dialog("close");
 +
return;
 +
}
 +
if (r.error) {
 +
alert(r.error.error_msg);
 +
}
 
});
 
});
 
}
 
}

Текущая версия от 02:19, 3 сентября 2016

(function() {

	CommentToVk = function() {
		var instance = this;

		var loginDiv = this.loginDiv = $('<div id="vkLoginStatus" style="float: right;"></div>');
		var loginLabel = this.loginLabel = $('<h4>Пользователь не авторизован</h4>');
		loginLabel.appendTo(loginDiv);

		var btnLogin = $("<button>Login</button>");
		btnLogin.button();
		btnLogin.click(function() {
			instance.login();
		});
		loginDiv.append(btnLogin);

		loginDiv.prependTo($("#bodyContent"));

		if ($("#vk_api_transport").length == 0) {
			$(document.body).append($('<div id="vk_api_transport"></div>'));

			window.vkAsyncInit = function() {
				VK.init({
					apiId : 2654830
				});
			};

			setTimeout(function() {
				var el = document.createElement("script");
				el.type = "text/javascript";
				el.src = "//vk.com/js/api/openapi.js";
				el.async = true;
				document.getElementById("vk_api_transport").appendChild(el);
			}, 0);
		}

		var publishDialog = this.publishDialog = $('<div id="commentsToVkDialog" title="Добавить комментарий в очередь"></div>')
		var publishForm = $('<form></form>')
		publishForm.appendTo(publishDialog);

		var publishFormFieldset = $('<fieldset></fieldset>')
		publishFormFieldset.appendTo(publishForm);

		var publishFormText = this.publishFormText = $('<textarea rows="10" cols="80"></textarea>')
		publishFormFieldset.append(publishFormText);
		publishFormFieldset.append('<br>');

		var publishFormLink = this.publishFormLink = $('<input type="url"></input>')
		publishFormFieldset.append(publishFormLink);
		publishFormFieldset.append('<br>');

		var publishFormTime = this.publishFormTime = $('<input type="datetime"></input>')
		publishFormFieldset.append(publishFormTime);
		publishFormFieldset.append('<br>');

		publishDialog.dialog({
			autoOpen : false,
			height : 'auto',
			width : 'auto',
			modal : true,
			buttons : {
				Submit : function() {
					instance.publish();
				},
				Cancel : function() {
					publishDialog.dialog("close");
				}
			},
		});

		$.datetimepicker.setLocale('ru');
	}

	CommentToVk.prototype.login = function() {
		var instance = this;

		VK.Auth.login(function(result) {
			if (result.status === 'connected') {
				var user = result.session.user;
				instance.loginLabel.text('Авторизован: ' + user.first_name
						+ ' ' + user.last_name);

				instance.appendPublishButtons();
			}
		});
	}

	CommentToVk.prototype.appendPublishButtons = function() {
		var instance = this;

		$(".c-item").each(function(index, item) {
			var container = $(item).find(".c-container");

			var btnPublish = $("<button>Publish</button>");
			btnPublish.button();
			btnPublish.click(function() {
				instance.dialogPublish($(item));
			});
			container.append($("<br>"));
			container.append(btnPublish);
		});
	}

	CommentToVk.prototype.dialogPublish = function(cItem) {
		var container = $(cItem).find(".c-container");
		var comment = $(cItem).find(".c-comment");
		var link = $(container.find("a")[0]).attr('href');
		link = link.split('#')[0];
		var text = comment.text();

		this.publishFormText.val(text);
		this.publishFormLink.val(link);
		this.publishDialog.dialog('open');

		var lastDate;
		if (typeof localStorage['CommentsToVk_lastDate'] === 'undefined') {
			lastDate = new Date();
		} else {
			var lastDate = new Date(1 * localStorage['CommentsToVk_lastDate']);
			lastDate = new Date(Math.max(new Date().getTime(), lastDate
					.getTime()));
		}
		nextDate = new Date(
				(Math.floor(lastDate.getTime() / 1000.0 / 60 / 60) + 1) * 1000 * 60 * 60);

		this.publishFormTime.datetimepicker({
			format : 'H:i:s d.m.Y',
			value : nextDate.toLocaleTimeString("ru-RU") + ' '
					+ nextDate.toLocaleDateString("ru-RU")
		});
	}

	CommentToVk.prototype.publish = function() {
		var instance = this;
		var link = this.publishFormLink.val();
		var text = this.publishFormText.val();
		var date = this.publishFormTime.datetimepicker('getValue');
		var timestamp = Math.round(date.getTime() / 1000);

		VK.Api.call('wall.post', {
			owner_id : -47630162,
			from_group : 1,
			message : text,
			attachments : link,
			signed : 0,
			publish_date : Math.floor(date.getTime() / 1000),
		}, function(r) {
			console.log(r);
			if (r.response) {
				localStorage['CommentsToVk_lastDate'] = date.getTime();
				instance.publishDialog.dialog("close");
				return;
			}
			if (r.error) {
				alert(r.error.error_msg);
			}
		});
	}

	if (wgArticleId == 5168) {
		new CommentToVk();
	}
})();