summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/res/smw/data/ext.smw.dataItem.wikiPage.js
blob: 3a2e654661cba9781abd2dc59dffc1ed044ce315 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*!
 * This file is part of the Semantic MediaWiki Extension
 * @see https://semantic-mediawiki.org/
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * @since 1.9
 *
 * @file
 *
 * @ingroup SMW
 *
 * @license GNU GPL v2+
 * @author mwjames
 */
( function( $, mw, smw ) {
	'use strict';

	/**
	 * Helper method
	 * @ignore
	 */
	var html = mw.html;

	/**
	 * Inheritance class for the smw.dataItem constructor
	 *
	 * @since 1.9
	 *
	 * @class smw.dataItem
	 * @abstract
	 */
	smw.dataItem = smw.dataItem || {};

	/**
	 * Initializes the constructor
	 *
	 * @param {string} fulltext
	 * @param {string} fullurl
	 * @param {number} ns
	 * @param {boolean} exists
	 *
	 * @return {smw.dataItem.wikiPage} this
	 */
	var wikiPage = function ( fulltext, fullurl, ns, exists, displaytitle ) {
		this.fulltext     = fulltext !== ''&& fulltext !== undefined ? fulltext : null;
		this.fullurl      = fullurl !== '' && fullurl !== undefined ? fullurl : null;
		this.ns           = ns !== undefined ? ns : 0;
		this.exists       = exists !== undefined ? exists : true;
		this.displaytitle = displaytitle !== '' && displaytitle !== undefined ? displaytitle : null;

		// Get mw.Title inheritance
		if ( this.fulltext !== null ){
			this.title = new mw.Title( this.fulltext );
		}

		return this;
	};

	/**
	 * A class that includes methods to create a wikiPage dataItem representation
	 * in JavaScript that resembles the SMW\DIWikiPage object in PHP
	 *
	 * @since 1.9
	 *
	 * @class
	 * @constructor
	 */
	smw.dataItem.wikiPage = function( fulltext, fullurl, ns, exists, displaytitle ) {
		if ( $.type( fulltext ) === 'string' && $.type( fullurl ) === 'string' ) {
			this.constructor( fulltext, fullurl, ns, exists, displaytitle );
		} else {
			throw new Error( 'smw.dataItem.wikiPage: fulltext, fullurl must be a string' );
		}
	};

	/* Public methods */

	var fn = {

		constructor: wikiPage,

		/**
		 * Returns type
		 *
		 * @since  1.9
		 *
		 * @return {string}
		 */
		getDIType: function() {
			return '_wpg';
		},

		/**
		 * Returns wikiPage text title as full name like "File:Foo bar.jpg"
		 * due to fact that the name is serialized in fulltext
		 *
		 * @since  1.9
		 *
		 * @return {string}
		 */
		getFullText: function() {
			return this.fulltext;
		},

		/**
		 * Returns main part of the title without any fragment
		 *
		 * @since  1.9
		 *
		 * @return {string}
		 */
		getText: function() {
			return this.fulltext && this.fulltext.split( '#' )[0];
		},

		/**
		 * Returns wikiPage uri
		 *
		 * @since  1.9
		 *
		 * @return {string}
		 */
		getUri: function() {
			return this.fullurl;
		},

		/**
		 * Returns mw.Title object
		 *
		 * @since  1.9
		 *
		 * @return {mw.Title}
		 */
		getTitle: function() {
			return this.title;
		},

		/**
		 * Returns if the wikiPage is a known entity or not
		 *
		 * @since  1.9
		 *
		 * @return {boolean}
		 */
		isKnown: function(){
			return this.exists;
		},

		/**
		 * Returns namespace id
		 *
		 * @since  1.9
		 *
		 * @return {number}
		 */
		getNamespaceId: function() {
			return this.ns;
		},

		/**
		 * Returns html representation
		 *
		 * @since  1.9
		 *
		 * @param {boolean}
		 *
		 * @return {string}
		 */
		getHtml: function( linker ) {
			var displaytitle = this.displaytitle;

			if ( displaytitle === null ) {
				displaytitle = this.getText();
			}

			if ( linker && this.fullurl !== null ){
				var attributes = this.exists ? { 'href': this.fullurl } : { 'href': this.fullurl, 'class': 'new' };
				return html.element( 'a', attributes , displaytitle );
			}

			return displaytitle;
		}
	};

	// Alias
	fn.exists = fn.isKnown;
	fn.getPrefixedText = fn.getFullText;
	fn.getName = fn.getFullText;
	fn.getValue = fn.getFullText;

	// Assign methods
	smw.dataItem.wikiPage.prototype = fn;

	// For additional methods use
	// $.extend( smw.dataItem.wikiPage.prototype, { method: function (){ ... } } );

} )( jQuery, mediaWiki, semanticMediaWiki );