-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWEnotesPostWP.js
More file actions
143 lines (137 loc) · 4.64 KB
/
Copy pathWEnotesPostWP.js
File metadata and controls
143 lines (137 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* WEnotes widget
* Copyright 2012 Open Education Resource Foundation
* Available under CC-BY license.
*/
/* global wgPageName, oeru_user_object */
/* exported WEnotesPostWP */
function WEnotesPostWP(id, tag, button, leftmargin, language) {
console.log('in WEnotesPostWP - language = '+language);
var weAPI = '/api.php',
postLength = 300,
rawPostLength = postLength + 20;
leftmargin = (leftmargin === '') ? 53 : leftmargin;
default_button_text = 'Post a WEnote';
language = (language === '') ? 'en_EN' : language;
if (language == 'fr_FR' && (button === '' || button === default_button_text)) {
console.log('setting button language!');
button = 'Soumettre un WEnote';
}
button = (button === '') ? default_button_text : unescape(button);
if (id.charAt(0) !== '#') {
id = '#' + id;
}
$(id).css('margin', '0px 0px 10px ' + leftmargin +'px').append('<form><textarea rows="4" cols="40" style="width:auto; height: 1.5em; float: left; margin-right: 10px; margin-bottom: 5px;"></textarea><div style="float: left;"><input type="submit" style="margin-top: 0;" disabled="disabled" value="' + button + '" /><p class="WEnotesPostCounter" style="color:#999; margin-left: 7px; display: none;">' + postLength + '</p></div></form><br clear="all" />');
var $counter = $(id + ' p.WEnotesPostCounter');
var $button = $(id + ' input[type="submit"]');
var $text = $(id + ' textarea');
function update(t) {
var mt = t.replace(/http:\/\/([^ ]+)/g, function (target) {
return (target.length > 19) ? 'http://xxx.xx/xxxxx' : target;
});
var l = mt.length;
var r = Math.min(postLength - l, rawPostLength - t.length);
$counter.text(r);
if (r >= 0) {
$counter.css('color', '#999');
if (l === 0) {
$button.attr('disabled', 'disabled');
} else {
$button.removeAttr('disabled');
}
} else {
$counter.css('color', 'red');
$button.attr('disabled', 'disabled');
}
}
function livenForm() {
$button.click(function() {
$button.attr('disabled', 'disabled');
$wenote_ids = null;
current_origin = window.location.origin.split('//')[1];
current_schema = window.location.origin.split('//')[0].split(':')[0];
current_path = window.location.pathname;
if (typeof WEnotesIDs != 'undefined') {
$wenotes_ids = WEnotesIDs;
console.log('===== setting $wenotes_ids = ', $wenotes_ids);
console.log('===== setting origin = ', current_origin);
}
$.ajax({
url: oeru_user_object.ajaxurl,
data: {
action: 'wenotes',
format: 'json',
pathinfo: $wenotes_ids,
origin: current_origin,
origin_path: current_path,
origin_schema: current_schema,
notag: tag,
notext: $text.val()
},
async: true,
type: 'POST',
dataType: 'json',
success: function(d) {
var msg = 'Unable to save submission';
if (d === 0 || 'error' in d) {
if (d.hasOwnProperty('error')) {
msg = msg + ":\n" + d.error.info;
}
alert(msg);
} else {
$('div.WEnotes').trigger('WEnotes', [tag]);
}
$button.removeAttr('disabled');
$text.val('');
$counter.text(postLength);
}
});
return false;
});
$text.bind('keyup', function() {
update($(this).val());
});
$text.bind('change', function() {
update($(this).val());
});
$text.focus(function() {
$counter.fadeIn();
$text.css('height', 'auto');
});
}
function disableForm() {
$button.attr('disabled', 'disabled');
$text.attr('disabled', 'disabled');
$text.attr('rows', '2');
$counter.html('<a class="plainlinks" href="/Special:UserLogin?returnto=' + wgPageName + '">Login to post</a>');
}
// check if logged in to the wiki
// either directly where wgUserName is already set
// or in a snapshot, where we do an API call to find out
if ((window.wgUserName === null) && !window.hasOwnProperty('oeru_user_object')) {
$.ajax({
url: weAPI,
data: {
action: 'query',
meta: 'userinfo',
format: 'json'
},
type: 'POST',
dataType: 'json',
success: function(d) {
var userinfo;
if (d && d.query && d.query.userinfo) {
userinfo = d.query.userinfo;
if (! userinfo.hasOwnProperty('anon')) {
window.wgUserName = userinfo.name;
livenForm();
return;
}
}
disableForm();
}
});
} else {
// in-wiki case, already know logged in state
livenForm();
}
}