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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
|
/*!
* VisualEditor MediaWiki Initialization ArticleTarget class.
*
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/* eslint-disable no-jquery/no-global-selector */
/**
* Initialization MediaWiki article target.
*
* @class
* @extends ve.init.mw.Target
*
* @constructor
* @param {Object} [config] Configuration options
*/
ve.init.mw.ArticleTarget = function VeInitMwArticleTarget( config ) {
var enableVisualSectionEditing;
config = config || {};
config.toolbarConfig = $.extend( {
shadow: true,
actions: true,
floatable: true
}, config.toolbarConfig );
// Parent constructor
ve.init.mw.ArticleTarget.super.call( this, config );
// Register
if ( config.register !== false ) {
// ArticleTargets are never destroyed, but we can't trust ve.init.target to
// not get overridden by other targets that may get created on the page.
ve.init.articleTarget = this;
}
// Properties
this.saveDialog = null;
this.saveDeferred = null;
this.saveFields = {};
this.docToSave = null;
this.originalDmDocPromise = null;
this.originalHtml = null;
this.toolbarSaveButton = null;
this.pageExists = mw.config.get( 'wgRelevantArticleId', 0 ) !== 0;
enableVisualSectionEditing = mw.config.get( 'wgVisualEditorConfig' ).enableVisualSectionEditing;
this.enableVisualSectionEditing = enableVisualSectionEditing === true || enableVisualSectionEditing === this.constructor.static.trackingName;
this.toolbarScrollOffset = mw.config.get( 'wgVisualEditorToolbarScrollOffset', 0 );
// A workaround, as default URI does not get updated after pushState (T74334)
this.currentUri = new mw.Uri( location.href );
this.section = null;
this.sectionTitle = null;
this.editSummaryValue = null;
this.initialEditSummary = null;
this.initialCheckboxes = {};
this.checkboxFields = null;
this.checkboxesByName = null;
this.$saveAccessKeyElements = null;
// Sometimes we actually don't want to send a useful oldid
// if we do, PostEdit will give us a 'page restored' message
this.requestedRevId = mw.config.get( 'wgRevisionId' );
this.currentRevisionId = mw.config.get( 'wgCurRevisionId' );
this.revid = this.requestedRevId || this.currentRevisionId;
this.edited = false;
this.restoring = !!this.requestedRevId && this.requestedRevId !== this.currentRevisionId;
this.pageDeletedWarning = false;
this.submitUrl = ( new mw.Uri( mw.util.getUrl( this.getPageName() ) ) )
.extend( {
action: 'submit',
veswitched: 1
} );
this.events = {
track: function () {},
trackActivationStart: function () {},
trackActivationComplete: function () {}
};
this.preparedCacheKeyPromise = null;
this.clearState();
// Initialization
this.$element.addClass( 've-init-mw-articleTarget' );
};
/* Inheritance */
OO.inheritClass( ve.init.mw.ArticleTarget, ve.init.mw.Target );
/* Events */
/**
* @event editConflict
*/
/**
* @event save
* @param {Object} data Save data from the API, see ve.init.mw.ArticleTarget#saveComplete
* Fired immediately after a save is successfully completed
*/
/**
* @event showChanges
*/
/**
* @event noChanges
*/
/**
* @event saveErrorEmpty
* Fired when save API returns no data object
*/
/**
* @event saveErrorSpamBlacklist
* Fired when save is considered spam or blacklisted
* TODO: Move this to the extension
*/
/**
* @event saveErrorAbuseFilter
* Fired when AbuseFilter throws warnings
* TODO: Move this to the extension
*/
/**
* @event saveErrorBadToken
* @param {boolean} willRetry Whether an automatic retry will occur
* Fired on save if we have to fetch a new edit token.
* This is mainly for analytical purposes.
*/
/**
* @event saveErrorNewUser
* Fired when user is logged in as a new user
*/
/**
* @event saveErrorCaptcha
* Fired when saveError indicates captcha field is required
* TODO: Move this to the extension
*/
/**
* @event saveErrorUnknown
* @param {string} errorMsg Error message shown to the user
* Fired for any other type of save error
*/
/**
* @event saveErrorPageDeleted
* Fired when user tries to save page that was deleted after opening VE
*/
/**
* @event saveErrorTitleBlacklist
* Fired when the user tries to save page in violation of the TitleBlacklist
*/
/**
* @event saveErrorHookAborted
* Fired when the user tries to save page in violation of an extension
*/
/**
* @event saveErrorReadOnly
* Fired when the user tries to save page but the database is locked
*/
/**
* @event loadError
*/
/**
* @event showChangesError
*/
/**
* @event serializeError
*/
/**
* @event serializeComplete
* Fired when serialization is complete
*/
/* Static Properties */
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.static.name = 'article';
/**
* Tracking name of target class. Used by ArticleTargetEvents to identify which target we are tracking.
*
* @static
* @property {string}
* @inheritable
*/
ve.init.mw.ArticleTarget.static.trackingName = 'mwTarget';
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.static.integrationType = 'page';
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.static.platformType = 'other';
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.static.documentCommands = ve.init.mw.ArticleTarget.super.static.documentCommands.concat( [
// Make save commands triggerable from anywhere
'showSave',
'showChanges',
'showPreview',
'showMinoredit',
'showWatchthis'
] );
/* Static methods */
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.static.parseDocument = function ( documentString, mode, section, onlySection ) {
// Add trailing linebreak to non-empty wikitext documents for consistency
// with old editor and usability. Will be stripped on save. T156609
if ( mode === 'source' && documentString ) {
documentString += '\n';
}
// Parent method
return ve.init.mw.ArticleTarget.super.static.parseDocument.call( this, documentString, mode, section, onlySection );
};
/**
* Build DOM for the redirect page subtitle (#redirectsub).
*
* @return {jQuery}
*/
ve.init.mw.ArticleTarget.static.buildRedirectSub = function () {
// Page subtitle
// Compare: Article::view()
return $( '<span>' )
.attr( 'id', 'redirectsub' )
.append( mw.message( 'redirectpagesub' ).parseDom() );
};
/**
* Build DOM for the redirect page content header (.redirectMsg).
*
* @param {string} title Redirect target
* @return {jQuery}
*/
ve.init.mw.ArticleTarget.static.buildRedirectMsg = function ( title ) {
var $link;
$link = $( '<a>' )
.attr( {
href: mw.Title.newFromText( title ).getUrl(),
title: mw.msg( 'visualeditor-redirect-description', title )
} )
.text( title );
ve.init.platform.linkCache.styleElement( title, $link );
// Page content header
// Compare: Article::getRedirectHeaderHtml()
return $( '<div>' )
.addClass( 'redirectMsg' )
// Hack: This is normally inside #mw-content-text, but we may insert it before, so we need this.
// The following classes are used here:
// * mw-content-ltr
// * mw-content-rtl
.addClass( 'mw-content-' + mw.config.get( 'wgVisualEditor' ).pageLanguageDir )
.append(
$( '<p>' ).text( mw.msg( 'redirectto' ) ),
$( '<ul>' )
.addClass( 'redirectText' )
.append( $( '<li>' ).append( $link ) )
);
};
/* Methods */
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.setDefaultMode = function () {
var oldDefaultMode = this.defaultMode;
// Parent method
ve.init.mw.ArticleTarget.super.prototype.setDefaultMode.apply( this, arguments );
if ( this.defaultMode !== oldDefaultMode ) {
this.updateTabs( true );
if ( mw.libs.ve.setEditorPreference ) {
// only set up by DAT.init
mw.libs.ve.setEditorPreference( this.defaultMode === 'visual' ? 'visualeditor' : 'wikitext' );
}
}
};
/**
* Update state of editing tabs
*
* @param {boolean} editing Whether the editor is loaded.
*/
ve.init.mw.ArticleTarget.prototype.updateTabs = function ( editing ) {
var $tab;
if ( editing ) {
if ( this.section === 'new' ) {
$tab = $( '#ca-addsection' );
} else if ( $( '#ca-ve-edit' ).length ) {
if ( this.getDefaultMode() === 'visual' ) {
$tab = $( '#ca-ve-edit' );
} else {
$tab = $( '#ca-edit' );
}
} else {
// Single edit tab
$tab = $( '#ca-edit' );
}
} else {
$tab = $( '#ca-view' );
}
// Deselect current mode (e.g. "view" or "history") in skins that have
// separate tab sections for content actions and namespaces, like Vector.
$( '#p-views' ).find( 'li.selected' ).removeClass( 'selected' );
// In skins like MonoBook that don't have the separate tab sections,
// deselect the known tabs for editing modes (when switching or exiting editor).
$( '#ca-edit, #ca-ve-edit, #ca-addsection' ).not( $tab ).removeClass( 'selected' );
$tab.addClass( 'selected' );
};
/**
* Handle response to a successful load request.
*
* This method is called within the context of a target instance. If successful the DOM from the
* server will be parsed, stored in {this.doc} and then {this.documentReady} will be called.
*
* @param {Object} response API response data
* @param {string} status Text status message
*/
ve.init.mw.ArticleTarget.prototype.loadSuccess = function ( response ) {
var mode, section,
data = response ? ( response.visualeditor || response.visualeditoredit ) : null;
if ( !data || typeof data.content !== 'string' ) {
this.loadFail( 've-api', { errors: [ {
code: 've-api',
html: mw.message( 'api-clientside-error-invalidresponse' ).parse()
} ] } );
} else if ( response.veMode && response.veMode !== this.getDefaultMode() ) {
this.loadFail( 've-mode', { errors: [ {
code: 've-mode',
html: mw.message( 'visualeditor-loaderror-wrongmode',
response.veMode, this.getDefaultMode() ).parse()
} ] } );
} else {
this.track( 'trace.parseResponse.enter' );
this.originalHtml = data.content;
this.etag = data.etag;
this.fromEditedState = !!data.fromEditedState;
this.switched = data.switched || 'wteswitched' in new mw.Uri( location.href ).query;
mode = this.getDefaultMode();
section = ( mode === 'source' || this.enableVisualSectionEditing ) ? this.section : null;
this.doc = this.constructor.static.parseDocument( this.originalHtml, mode, section );
// Properties that don't come from the API
this.initialSourceRange = data.initialSourceRange;
this.recovered = data.recovered;
// Parse data this not available in RESTBase
if ( !this.parseMetadata( response ) ) {
// Invalid metadata, loadFail() or load() has been called
return;
}
this.track( 'trace.parseResponse.exit' );
// Everything worked, the page was loaded, continue initializing the editor
this.documentReady( this.doc );
}
if ( [ 'edit', 'submit' ].indexOf( mw.util.getParamValue( 'action' ) ) !== -1 ) {
$( '#firstHeading' ).text(
mw.Title.newFromText( this.getPageName() ).getPrefixedText()
);
}
};
/**
* Parse document metadata from the API response
*
* @param {Object} response API response data
* @return {boolean} Whether metadata was loaded successfully. If true, you should call
* loadSuccess(). If false, either that loadFail() has been called or we're retrying via load().
*/
ve.init.mw.ArticleTarget.prototype.parseMetadata = function ( response ) {
var aboutDoc, docRevIdMatches, docRevId, checkboxes,
data = response ? ( response.visualeditor || response.visualeditoredit ) : null;
if ( !data ) {
this.loadFail( 've-api', { errors: [ {
code: 've-api',
html: mw.message( 'api-clientside-error-invalidresponse' ).parse()
} ] } );
return false;
}
this.remoteNotices = ve.getObjectValues( data.notices );
this.protectedClasses = data.protectedClasses;
this.baseTimeStamp = data.basetimestamp;
this.startTimeStamp = data.starttimestamp;
this.revid = data.oldid;
this.preloaded = !!data.preloaded;
this.checkboxesDef = data.checkboxesDef;
this.checkboxesMessages = data.checkboxesMessages;
mw.messages.set( data.checkboxesMessages );
this.canEdit = data.canEdit;
aboutDoc = this.doc.documentElement && this.doc.documentElement.getAttribute( 'about' );
if ( aboutDoc ) {
docRevIdMatches = aboutDoc.match( /revision\/([0-9]*)$/ );
if ( docRevIdMatches.length >= 2 ) {
docRevId = parseInt( docRevIdMatches[ 1 ] );
}
}
if ( docRevId && docRevId !== this.revid ) {
if ( this.retriedRevIdConflict ) {
// Retried already, just error the second time.
this.loadFail( 've-api', { errors: [ {
code: 've-api',
html: mw.message( 'visualeditor-loaderror-revidconflict',
String( docRevId ), String( this.revid ) ).parse()
} ] } );
} else {
this.retriedRevIdConflict = true;
// TODO this retries both requests, in RESTbase mode we should only retry
// the request that gave us the lower revid
this.loading = null;
// HACK: Load with explicit revid to hopefully prevent this from happening again
this.requestedRevId = Math.max( docRevId, this.revid );
this.load();
}
return false;
} else {
// Set this to false after a successful load, so we don't immediately give up
// if a subsequent load mismatches again
this.retriedRevIdConflict = false;
}
checkboxes = mw.libs.ve.targetLoader.createCheckboxFields( this.checkboxesDef );
this.checkboxFields = checkboxes.checkboxFields;
this.checkboxesByName = checkboxes.checkboxesByName;
this.checkboxFields.forEach( function ( field ) {
// TODO: This method should be upstreamed or moved so that targetLoader
// can use it safely.
ve.targetLinksToNewWindow( field.$label[ 0 ] );
} );
return true;
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.documentReady = function () {
// We need to wait until documentReady as local notices may require special messages
this.editNotices = this.remoteNotices.concat(
this.localNoticeMessages.map( function ( msgKey ) {
return '<p>' + ve.init.platform.getParsedMessage( msgKey ) + '</p>';
} )
);
this.loading = null;
this.edited = this.fromEditedState;
// Parent method
ve.init.mw.ArticleTarget.super.prototype.documentReady.apply( this, arguments );
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.surfaceReady = function () {
var name, i, triggers,
accessKeyPrefix = $.fn.updateTooltipAccessKeys.getAccessKeyPrefix().replace( /-/g, '+' ),
accessKeyModifiers = new ve.ui.Trigger( accessKeyPrefix + '-' ).modifiers,
surfaceModel = this.getSurface().getModel();
// loadSuccess() may have called setAssumeExistence( true );
ve.init.platform.linkCache.setAssumeExistence( false );
surfaceModel.connect( this, {
history: 'updateToolbarSaveButtonState'
} );
// Iterate over the trigger registry and resolve any access key conflicts
for ( name in ve.ui.triggerRegistry.registry ) {
triggers = ve.ui.triggerRegistry.registry[ name ];
for ( i = 0; i < triggers.length; i++ ) {
if ( ve.compare( triggers[ i ].modifiers, accessKeyModifiers ) ) {
this.disableAccessKey( triggers[ i ].primary );
}
}
}
if ( !this.canEdit ) {
this.getSurface().setReadOnly( true );
} else {
// Auto-save
this.initAutosave();
setTimeout( function () {
mw.libs.ve.targetSaver.preloadDeflate();
}, 500 );
}
// Parent method
ve.init.mw.ArticleTarget.super.prototype.surfaceReady.apply( this, arguments );
// Do this after window is made scrollable on mobile
// ('surfaceReady' handler in VisualEditorOverlay in MobileFrontend)
this.restoreEditSection();
mw.hook( 've.activationComplete' ).fire();
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.storeDocState = function ( html ) {
var mode = this.getSurface().getMode();
this.getSurface().getModel().storeDocState( {
request: {
pageName: this.getPageName(),
mode: mode,
// Check true section editing is in use
section: ( mode === 'source' || this.enableVisualSectionEditing ) ? this.section : null
},
response: {
etag: this.etag,
fromEditedState: this.fromEditedState,
switched: this.switched,
preloaded: this.preloaded,
notices: this.remoteNotices,
protectedClasses: this.protectedClasses,
basetimestamp: this.baseTimeStamp,
starttimestamp: this.startTimeStamp,
oldid: this.revid,
canEdit: this.canEdit,
checkboxesDef: this.checkboxesDef,
checkboxesMessages: this.checkboxesMessages
}
}, html );
};
/**
* Disable an access key by removing the attribute from any element containing it
*
* @param {string} key Access key
*/
ve.init.mw.ArticleTarget.prototype.disableAccessKey = function ( key ) {
$( '[accesskey=' + key + ']' ).each( function () {
var $this = $( this );
$this
.attr( 'data-old-accesskey', $this.attr( 'accesskey' ) )
.removeAttr( 'accesskey' );
} );
};
/**
* Re-enable all access keys
*/
ve.init.mw.ArticleTarget.prototype.restoreAccessKeys = function () {
$( '[data-old-accesskey]' ).each( function () {
var $this = $( this );
$this
.attr( 'accesskey', $this.attr( 'data-old-accesskey' ) )
.removeAttr( 'data-old-accesskey' );
} );
};
/**
* Handle an unsuccessful load request.
*
* This method is called within the context of a target instance.
*
* @param {string} code Error code from mw.Api
* @param {Object} errorDetails API response
* @fires loadError
*/
ve.init.mw.ArticleTarget.prototype.loadFail = function () {
this.loading = null;
this.emit( 'loadError' );
};
/**
* Handle successful DOM save event.
*
* @param {Object} data Save data from the API
* @param {string} data.content Rendered page HTML from server
* @param {string} data.categorieshtml Rendered categories HTML from server
* @param {number} data.newrevid New revision id, undefined if unchanged
* @param {boolean} data.isRedirect Whether this page is a redirect or not
* @param {string} data.displayTitleHtml What HTML to show as the page title
* @param {Object} data.lastModified Object containing user-formatted date
* and time strings, or undefined if we made no change.
* @param {string} data.contentSub HTML to show as the content subtitle
* @param {Array} data.modules The modules to be loaded on the page
* @param {Object} data.jsconfigvars The mw.config values needed on the page
* @fires save
*/
ve.init.mw.ArticleTarget.prototype.saveComplete = function ( data ) {
this.editSummaryValue = null;
this.initialEditSummary = null;
this.saveDeferred.resolve();
this.emit( 'save', data );
};
/**
* Handle an unsuccessful save request.
*
* TODO: This code should be mostly moved to ArticleTargetSaver,
* in particular the badtoken error handling.
*
* @param {HTMLDocument} doc HTML document we tried to save
* @param {Object} saveData Options that were used
* @param {boolean} wasRetry Whether this was a retry after a 'badtoken' error
* @param {string} code Error code
* @param {Object|null} data Full API response data, or XHR error details
*/
ve.init.mw.ArticleTarget.prototype.saveFail = function ( doc, saveData, wasRetry, code, data ) {
var name, handler, i, error,
saveErrorHandlerFactory = ve.init.mw.saveErrorHandlerFactory,
target = this;
this.pageDeletedWarning = false;
// Handle empty response
if ( !data ) {
this.saveErrorEmpty();
return;
}
if ( data.errors ) {
for ( i = 0; i < data.errors.length; i++ ) {
error = data.errors[ i ];
if ( error.code === 'badtoken' ) {
this.saveErrorBadToken();
} else if ( error.code === 'assertanonfailed' || error.code === 'assertuserfailed' || error.code === 'assertnameduserfailed' ) {
this.refreshUser().then( function ( username ) {
target.saveErrorNewUser( username );
}, function () {
target.saveErrorUnknown( data );
} );
return;
} else if ( error.code === 'editconflict' ) {
this.editConflict();
return;
} else if ( error.code === 'pagedeleted' ) {
this.saveErrorPageDeleted();
return;
} else if ( error.code === 'hookaborted' ) {
this.saveErrorHookAborted( data );
return;
} else if ( error.code === 'readonly' ) {
this.saveErrorReadOnly( data );
return;
}
}
}
for ( name in saveErrorHandlerFactory.registry ) {
handler = saveErrorHandlerFactory.lookup( name );
if ( handler.static.matchFunction( data ) ) {
handler.static.process( data, this );
// Error was handled
return;
}
}
// Handle (other) unknown and/or unrecoverable errors
this.saveErrorUnknown( data );
};
/**
* Show an save process error message
*
* @param {string|jQuery|Node[]} msg Message content (string of HTML, jQuery object or array of
* Node objects)
* @param {boolean} [allowReapply=true] Whether or not to allow the user to reapply.
* Reset when swapping panels. Assumed to be true unless explicitly set to false.
* @param {boolean} [warning=false] Whether or not this is a warning.
*/
ve.init.mw.ArticleTarget.prototype.showSaveError = function ( msg, allowReapply, warning ) {
this.saveDeferred.reject( [ new OO.ui.Error( msg, { recoverable: allowReapply, warning: warning } ) ] );
};
/**
* Extract the error messages from an erroneous API response
*
* @param {Object} data API response data
* @return {jQuery}
*/
ve.init.mw.ArticleTarget.prototype.extractErrorMessages = function ( data ) {
var $errorMsgs = ( new mw.Api() ).getErrorMessage( data );
$errorMsgs.each( function () {
ve.targetLinksToNewWindow( this );
} );
return $errorMsgs;
};
/**
* Handle general save error
*
* @fires saveErrorEmpty
*/
ve.init.mw.ArticleTarget.prototype.saveErrorEmpty = function () {
this.showSaveError(
ve.msg( 'visualeditor-saveerror', ve.msg( 'visualeditor-error-invalidresponse' ) ),
false /* prevents reapply */
);
this.emit( 'saveErrorEmpty' );
};
/**
* Handle hook abort save error
*
* @param {Object} data API response data
* @fires saveErrorHookAborted
*/
ve.init.mw.ArticleTarget.prototype.saveErrorHookAborted = function ( data ) {
this.showSaveError( this.extractErrorMessages( data ) );
this.emit( 'saveErrorHookAborted' );
};
/**
* Handle assert error indicating another user is logged in.
*
* @param {string|null} username Name of newly logged-in user, or null if anonymous
* @fires saveErrorNewUser
*/
ve.init.mw.ArticleTarget.prototype.saveErrorNewUser = function ( username ) {
var $msg;
this.emit( 'saveErrorNewUser' );
// TODO: Improve this message, concatenating it this way is a bad practice.
// This should read more like 'session_fail_preview' in MediaWiki core
// (but with the caveat that we know already whether you're logged in or not).
$msg = $( document.createTextNode( mw.msg( 'visualeditor-savedialog-error-badtoken' ) + ' ' ) ).add(
mw.message(
username === null ?
'visualeditor-savedialog-identify-anon' :
'visualeditor-savedialog-identify-user',
username
).parseDom()
);
this.showSaveError( $msg );
};
/**
* Handle token fetch errors.
*
* @fires saveErrorBadToken
*/
ve.init.mw.ArticleTarget.prototype.saveErrorBadToken = function () {
this.emit( 'saveErrorBadToken', false );
// TODO: Improve this message, concatenating it this way is a bad practice.
// Also, it's not always true that you're "no longer logged in".
// This should read more like 'session_fail_preview' in MediaWiki core.
this.showSaveError(
mw.msg( 'visualeditor-savedialog-error-badtoken' ) + ' ' +
mw.msg( 'visualeditor-savedialog-identify-trylogin' )
);
};
/**
* Handle unknown save error
*
* @param {Object|null} data API response data
* @fires saveErrorUnknown
*/
ve.init.mw.ArticleTarget.prototype.saveErrorUnknown = function ( data ) {
var errorCodes;
this.showSaveError( this.extractErrorMessages( data ), false );
if ( data.errors ) {
errorCodes = data.errors.map( function ( err ) {
return err.code;
} ).join( ',' );
} else {
errorCodes = 'http-' + ( ( data.xhr && data.xhr.status ) || 0 );
}
this.emit( 'saveErrorUnknown', errorCodes );
};
/**
* Handle page deleted error
*
* @fires saveErrorPageDeleted
*/
ve.init.mw.ArticleTarget.prototype.saveErrorPageDeleted = function () {
this.pageDeletedWarning = true;
// The API error message 'apierror-pagedeleted' is poor, make our own
this.showSaveError( mw.msg( 'visualeditor-recreate', mw.msg( 'ooui-dialog-process-continue' ) ), true, true );
this.emit( 'saveErrorPageDeleted' );
};
/**
* Handle read only error
*
* @param {Object} data API response data
* @fires saveErrorReadOnly
*/
ve.init.mw.ArticleTarget.prototype.saveErrorReadOnly = function ( data ) {
this.showSaveError( this.extractErrorMessages( data ), true, true );
this.emit( 'saveErrorReadOnly' );
};
/**
* Handle an edit conflict
*
* @fires editConflict
*/
ve.init.mw.ArticleTarget.prototype.editConflict = function () {
this.emit( 'editConflict' );
this.saveDialog.popPending();
this.saveDialog.swapPanel( 'conflict' );
};
/**
* Handle clicks on the review button in the save dialog.
*
* @fires saveReview
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogReview = function () {
var target = this;
if ( !this.saveDialog.hasDiff ) {
this.emit( 'saveReview' );
this.saveDialog.pushPending();
if ( this.pageExists ) {
// Has no callback, handled via target.showChangesDiff
this.showChanges( this.getDocToSave() );
} else {
this.serialize( this.getDocToSave() ).then( function ( data ) {
target.onSaveDialogReviewComplete( data.content );
} );
}
} else {
this.saveDialog.swapPanel( 'review' );
}
};
/**
* Handle clicks on the show preview button in the save dialog.
*
* @fires savePreview
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogPreview = function () {
var wikitext,
api = this.getContentApi(),
target = this;
if ( !this.saveDialog.$previewViewer.children().length ) {
this.emit( 'savePreview' );
this.saveDialog.pushPending();
wikitext = this.getDocToSave();
if ( this.sectionTitle && this.sectionTitle.getValue() ) {
wikitext = '== ' + this.sectionTitle.getValue() + ' ==\n\n' + wikitext;
}
api.post( {
action: 'visualeditor',
paction: 'parsedoc',
page: this.getPageName(),
wikitext: wikitext,
pst: true
} ).then( function ( response ) {
var doc, baseDoc;
baseDoc = target.getSurface().getModel().getDocument().getHtmlDocument();
doc = target.constructor.static.parseDocument( response.visualeditor.content, 'visual' );
target.saveDialog.showPreview( doc, baseDoc );
}, function ( errorCode, details ) {
target.saveDialog.showPreview( target.extractErrorMessages( details ) );
} ).always( function () {
target.bindSaveDialogClearDiff();
} );
} else {
this.saveDialog.swapPanel( 'preview' );
}
};
/**
* Clear the save dialog's diff cache when the document changes
*/
ve.init.mw.ArticleTarget.prototype.bindSaveDialogClearDiff = function () {
// Invalidate the viewer wikitext on next change
this.getSurface().getModel().getDocument().once( 'transact',
this.saveDialog.clearDiff.bind( this.saveDialog )
);
if ( this.sectionTitle ) {
this.sectionTitle.once( 'change', this.saveDialog.clearDiff.bind( this.saveDialog ) );
}
};
/**
* Handle completed serialize request for diff views for new page creations.
*
* @param {string} wikitext Wikitext
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogReviewComplete = function ( wikitext ) {
this.bindSaveDialogClearDiff();
this.saveDialog.setDiffAndReview(
ve.createDeferred().resolve( $( '<pre>' ).text( wikitext ) ).promise(),
this.getVisualDiffGeneratorPromise(),
this.getSurface().getModel().getDocument().getHtmlDocument()
);
};
/**
* Get a visual diff object for the current document state
*
* @return {jQuery.Promise} Promise resolving with a generator for a ve.dm.VisualDiff visual diff
*/
ve.init.mw.ArticleTarget.prototype.getVisualDiffGeneratorPromise = function () {
var target = this;
return mw.loader.using( 'ext.visualEditor.diffLoader' ).then( function () {
var newRevPromise, doc;
if ( !target.originalDmDocPromise ) {
if ( !target.fromEditedState && target.getSurface().getMode() === 'visual' ) {
// If this.doc was loaded from an un-edited state and in visual mode,
// then just parse it to get originalDmDoc, otherwise we need to
// re-fetch the HTML
if ( target.section !== null && target.enableVisualSectionEditing ) {
// Create a document with just the section
doc = target.doc.cloneNode( true );
// Empty
while ( doc.body.firstChild ) {
doc.body.removeChild( doc.body.firstChild );
}
// Append section and unwrap
doc.body.appendChild( target.doc.body.querySelectorAll( 'section[data-mw-section-id]' )[ 0 ] );
mw.libs.ve.unwrapParsoidSections( doc.body );
} else {
doc = target.doc;
}
target.originalDmDocPromise = ve.createDeferred().resolve( target.constructor.static.createModelFromDom( doc, 'visual' ) ).promise();
} else {
target.originalDmDocPromise = mw.libs.ve.diffLoader.fetchRevision( target.revid, target.getPageName(), target.section );
}
}
if ( target.getSurface().getMode() === 'source' ) {
newRevPromise = target.getContentApi().post( {
action: 'visualeditor',
paction: 'parsedoc',
page: target.getPageName(),
wikitext: target.getSurface().getDom(),
pst: true
} ).then( function ( response ) {
// Use anonymous function to avoid passing through API promise argument
return mw.libs.ve.diffLoader.getModelFromResponse( response, target.section === null ? null : undefined );
} );
return mw.libs.ve.diffLoader.getVisualDiffGeneratorPromise( target.originalDmDocPromise, newRevPromise );
} else {
return target.originalDmDocPromise.then( function ( originalDmDoc ) {
return function () {
return new ve.dm.VisualDiff( originalDmDoc, target.getSurface().getModel().getAttachedRoot() );
};
} );
}
} );
};
/**
* Handle clicks on the resolve conflict button in the conflict dialog.
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogResolveConflict = function () {
var fields = { wpSave: 1 },
target = this;
if ( this.getSurface().getMode() === 'source' && this.section !== null ) {
// TODO: This should happen in #getSaveFields, check if moving it there breaks anything
fields.section = this.section;
}
// Get Wikitext from the DOM, and set up a submit call when it's done
this.serialize( this.getDocToSave() ).then( function ( data ) {
target.submitWithSaveFields( fields, data.content );
} );
};
/**
* Handle dialog retry events
* So we can handle trying to save again after page deletion warnings
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogRetry = function () {
if ( this.pageDeletedWarning ) {
this.recreating = true;
this.pageExists = false;
}
};
/**
* Handle dialog close events.
*
* @fires saveWorkflowEnd
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogClose = function () {
this.emit( 'saveWorkflowEnd' );
};
/**
* Load the editor.
*
* This method initiates an API request for the page data unless dataPromise is passed in,
* in which case it waits for that promise instead.
*
* @param {jQuery.Promise} [dataPromise] Promise for pending request, if any
* @return {jQuery.Promise} Data promise
*/
ve.init.mw.ArticleTarget.prototype.load = function ( dataPromise ) {
// Prevent duplicate requests
if ( this.loading ) {
return this.loading;
}
this.events.trackActivationStart( mw.libs.ve.activationStart );
mw.libs.ve.activationStart = null;
dataPromise = dataPromise || mw.libs.ve.targetLoader.requestPageData( this.getDefaultMode(), this.getPageName(), {
sessionStore: true,
section: this.section,
oldId: this.requestedRevId,
targetName: this.constructor.static.trackingName
} );
this.loading = dataPromise;
dataPromise
.done( this.loadSuccess.bind( this ) )
.fail( this.loadFail.bind( this ) );
return dataPromise;
};
/**
* Clear the state of this target, preparing it to be reactivated later.
*/
ve.init.mw.ArticleTarget.prototype.clearState = function () {
this.restoreAccessKeys();
this.clearPreparedCacheKey();
this.loading = null;
this.saving = null;
this.clearDiff();
this.serializing = false;
this.submitting = false;
this.baseTimeStamp = null;
this.startTimeStamp = null;
this.checkboxes = null;
this.initialSourceRange = null;
this.doc = null;
this.originalDmDocPromise = null;
this.originalHtml = null;
this.toolbarSaveButton = null;
this.section = null;
this.editNotices = [];
this.remoteNotices = [];
this.localNoticeMessages = [];
this.recovered = false;
this.teardownPromise = null;
};
/**
* Switch to edit source mode
*
* Opens a confirmation dialog if the document is modified or VE wikitext mode
* is not available.
*/
ve.init.mw.ArticleTarget.prototype.editSource = function () {
var modified = this.fromEditedState || this.getSurface().getModel().hasBeenModified();
this.switchToWikitextEditor( modified );
};
/**
* Get a document to save, cached until the surface is modified
*
* The default implementation returns an HTMLDocument, but other targets
* may use a different document model (e.g. plain text for source mode).
*
* @return {Object} Document to save
*/
ve.init.mw.ArticleTarget.prototype.getDocToSave = function () {
var surface;
if ( !this.docToSave ) {
this.docToSave = this.createDocToSave();
// Cache clearing events
surface = this.getSurface();
surface.getModel().getDocument().once( 'transact', this.clearDocToSave.bind( this ) );
surface.once( 'destroy', this.clearDocToSave.bind( this ) );
}
return this.docToSave;
};
/**
* Create a document to save
*
* @return {Object} Document to save
*/
ve.init.mw.ArticleTarget.prototype.createDocToSave = function () {
return this.getSurface().getDom();
};
/**
* Clear the document to save from the cache
*/
ve.init.mw.ArticleTarget.prototype.clearDocToSave = function () {
this.docToSave = null;
this.clearPreparedCacheKey();
};
/**
* Serialize the current document and store the result in the serialization cache on the server.
*
* This function returns a promise that is resolved once serialization is complete, with the
* cache key passed as the first parameter.
*
* If there's already a request pending for the same (reference-identical) HTMLDocument, this
* function will not initiate a new request but will return the promise for the pending request.
* If a request for the same document has already been completed, this function will keep returning
* the same promise (which will already have been resolved) until clearPreparedCacheKey() is called.
*
* @param {HTMLDocument} doc Document to serialize
*/
ve.init.mw.ArticleTarget.prototype.prepareCacheKey = function ( doc ) {
var xhr,
aborted = false,
start = ve.now(),
target = this;
if ( this.getSurface().getMode() === 'source' ) {
return;
}
if ( this.preparedCacheKeyPromise && this.preparedCacheKeyPromise.doc === doc ) {
return;
}
this.clearPreparedCacheKey();
this.preparedCacheKeyPromise = mw.libs.ve.targetSaver.deflateDoc( doc, this.doc )
.then( function ( deflatedHtml ) {
if ( aborted ) {
return ve.createDeferred().reject();
}
xhr = target.getContentApi().postWithToken( 'csrf',
{
action: 'visualeditoredit',
paction: 'serializeforcache',
html: deflatedHtml,
page: target.getPageName(),
oldid: target.revid,
etag: target.etag
},
{ contentType: 'multipart/form-data' }
);
return xhr.then(
function ( response ) {
var trackData = { duration: ve.now() - start };
if ( response.visualeditoredit && typeof response.visualeditoredit.cachekey === 'string' ) {
target.events.track( 'performance.system.serializeforcache', trackData );
return {
cacheKey: response.visualeditoredit.cachekey,
// Pass the HTML for retries.
html: deflatedHtml
};
} else {
target.events.track( 'performance.system.serializeforcache.nocachekey', trackData );
return ve.createDeferred().reject();
}
},
function () {
target.events.track( 'performance.system.serializeforcache.fail', { duration: ve.now() - start } );
return ve.createDeferred().reject();
}
);
} )
.promise( {
abort: function () {
if ( xhr ) {
xhr.abort();
}
aborted = true;
},
doc: doc
} );
};
/**
* Get the prepared wikitext, if any. Same as prepareWikitext() but does not initiate a request
* if one isn't already pending or finished. Instead, it returns a rejected promise in that case.
*
* @param {HTMLDocument} doc Document to serialize
* @return {jQuery.Promise} Abortable promise, resolved with a plain object containing `cacheKey`,
* and `html` for retries.
*/
ve.init.mw.ArticleTarget.prototype.getPreparedCacheKey = function ( doc ) {
if ( this.preparedCacheKeyPromise && this.preparedCacheKeyPromise.doc === doc ) {
return this.preparedCacheKeyPromise;
}
return ve.createDeferred().reject().promise();
};
/**
* Clear the promise for the prepared wikitext cache key, and abort it if it's still in progress.
*/
ve.init.mw.ArticleTarget.prototype.clearPreparedCacheKey = function () {
if ( this.preparedCacheKeyPromise ) {
this.preparedCacheKeyPromise.abort();
this.preparedCacheKeyPromise = null;
}
};
/**
* Try submitting an API request with a cache key for prepared wikitext, falling back to submitting
* HTML directly if there is no cache key present or pending, or if the request for the cache key
* fails, or if using the cache key fails with a badcachekey error.
*
* This function will use mw.Api#postWithToken to retry automatically when encountering a 'badtoken'
* error.
*
* @param {HTMLDocument|string} doc Document to submit or string in source mode
* @param {Object} extraData POST parameters to send. Do not include 'html', 'cachekey' or 'format'.
* @param {string} [eventName] If set, log an event when the request completes successfully. The
* full event name used will be 'performance.system.{eventName}.withCacheKey' or .withoutCacheKey
* depending on whether or not a cache key was used.
* @return {jQuery.Promise} Promise which resolves/rejects when saving is complete/fails
*/
ve.init.mw.ArticleTarget.prototype.tryWithPreparedCacheKey = function ( doc, extraData, eventName ) {
var data, htmlOrCacheKeyPromise,
target = this;
if ( this.getSurface().getMode() === 'source' ) {
data = ve.copy( extraData );
// TODO: This should happen in #getSaveOptions, check if moving it there breaks anything
if ( this.section !== null ) {
data.section = this.section;
}
if ( this.sectionTitle ) {
data.sectiontitle = this.sectionTitle.getValue();
data.summary = undefined;
}
return mw.libs.ve.targetSaver.postWikitext(
doc,
data,
{ api: target.getContentApi() }
);
}
// getPreparedCacheKey resolves with { cacheKey: ..., html: ... } or rejects.
// After modification it never rejects, just resolves with { html: ... } instead
htmlOrCacheKeyPromise = this.getPreparedCacheKey( doc ).then(
// Success, use promise as-is.
null,
// Fail, get deflatedHtml promise
function () {
return mw.libs.ve.targetSaver.deflateDoc( doc, target.doc ).then( function ( html ) {
return { html: html };
} );
} );
return htmlOrCacheKeyPromise.then( function ( htmlOrCacheKey ) {
return mw.libs.ve.targetSaver.postHtml(
htmlOrCacheKey.html,
htmlOrCacheKey.cacheKey,
extraData,
{
onCacheKeyFail: target.clearPreparedCacheKey.bind( target ),
api: target.getContentApi(),
track: target.events.track.bind( target.events ),
eventName: eventName,
now: ve.now
}
);
} );
};
/**
* Handle the save dialog's save event
*
* Validates the inputs then starts the save process
*
* @param {jQuery.Deferred} saveDeferred Deferred object to resolve/reject when the save
* succeeds/fails.
* @fires saveInitiated
*/
ve.init.mw.ArticleTarget.prototype.onSaveDialogSave = function ( saveDeferred ) {
var saveOptions;
if ( this.deactivating ) {
return;
}
saveOptions = this.getSaveOptions();
if (
+mw.user.options.get( 'forceeditsummary' ) &&
( saveOptions.summary === '' || saveOptions.summary === this.initialEditSummary ) &&
!this.saveDialog.messages.missingsummary
) {
this.saveDialog.showMessage(
'missingsummary',
// Wrap manually since this core message already includes a bold "Warning:" label
$( '<p>' ).append( ve.init.platform.getParsedMessage( 'missingsummary' ) ),
{ wrap: false }
);
this.saveDialog.popPending();
} else {
this.emit( 'saveInitiated' );
this.startSave( saveOptions );
this.saveDeferred = saveDeferred;
}
};
/**
* Start the save process
*
* @param {Object} saveOptions Save options
*/
ve.init.mw.ArticleTarget.prototype.startSave = function ( saveOptions ) {
this.save( this.getDocToSave(), saveOptions );
};
/**
* Get save form fields from the save dialog form.
*
* @return {Object} Form data for submission to the MediaWiki action=edit UI
*/
ve.init.mw.ArticleTarget.prototype.getSaveFields = function () {
var name,
fields = {};
if ( this.section === 'new' ) {
// MediaWiki action=edit UI doesn't have separate parameters for edit summary and new section
// title. The edit summary parameter is supposed to contain the section title, and the real
// summary is autogenerated.
fields.wpSummary = this.sectionTitle ? this.sectionTitle.getValue() : '';
} else {
fields.wpSummary = this.saveDialog ?
this.saveDialog.editSummaryInput.getValue() :
( this.editSummaryValue || this.initialEditSummary );
}
// Extra save fields added by extensions
for ( name in this.saveFields ) {
fields[ name ] = this.saveFields[ name ]();
}
if ( this.recreating ) {
fields.wpRecreate = true;
}
for ( name in this.checkboxesByName ) {
if ( this.checkboxesByName[ name ].isSelected() ) {
fields[ name ] = this.checkboxesByName[ name ].getValue();
}
}
return fields;
};
/**
* Invoke #submit with the data from #getSaveFields
*
* @param {Object} fields Fields to add in addition to those from #getSaveFields
* @param {string} wikitext Wikitext to submit
* @return {boolean} Whether submission was started
*/
ve.init.mw.ArticleTarget.prototype.submitWithSaveFields = function ( fields, wikitext ) {
return this.submit( wikitext, $.extend( this.getSaveFields(), fields ) );
};
/**
* Get edit API options from the save dialog form.
*
* @return {Object} Save options for submission to the MediaWiki API
*/
ve.init.mw.ArticleTarget.prototype.getSaveOptions = function () {
var key,
options = this.getSaveFields(),
fieldMap = {
wpSummary: 'summary',
wpMinoredit: 'minor',
wpWatchthis: 'watchlist',
wpCaptchaId: 'captchaid',
wpCaptchaWord: 'captchaword'
};
for ( key in fieldMap ) {
if ( options[ key ] !== undefined ) {
options[ fieldMap[ key ] ] = options[ key ];
delete options[ key ];
}
}
options.watchlist = 'watchlist' in options ? 'watch' : 'unwatch';
return options;
};
/**
* Post DOM data to the Parsoid API.
*
* This method performs an asynchronous action and uses a callback function to handle the result.
*
* target.save( dom, { summary: 'test', minor: true, watch: false } );
*
* @param {HTMLDocument} doc Document to save
* @param {Object} options Saving options. All keys are passed through, including unrecognized ones.
* - {string} summary Edit summary
* - {boolean} minor Edit is a minor edit
* - {boolean} watch Watch the page
* @param {boolean} [isRetry=false] Whether this is a retry after a 'badtoken' error
* @return {jQuery.Promise} Save promise, see mw.libs.ve.targetSaver.postHtml
*/
ve.init.mw.ArticleTarget.prototype.save = function ( doc, options, isRetry ) {
var data, promise,
target = this;
// Prevent duplicate requests
if ( this.saving ) {
return this.saving;
}
data = ve.extendObject( {}, options, {
page: this.getPageName(),
oldid: this.revid,
basetimestamp: this.baseTimeStamp,
starttimestamp: this.startTimeStamp,
etag: this.etag,
assert: mw.user.isAnon() ? 'anon' : 'user',
assertuser: mw.user.getName() || undefined
} );
if ( mw.config.get( 'wgVisualEditorConfig' ).useChangeTagging && !data.vetags ) {
if ( this.getSurface().getMode() === 'source' ) {
data.vetags = 'visualeditor-wikitext';
} else {
data.vetags = 'visualeditor';
}
}
promise = this.saving = this.tryWithPreparedCacheKey( doc, data, 'save' )
.done( this.saveComplete.bind( this ) )
.fail( this.saveFail.bind( this, doc, data, !!isRetry ) )
.always( function () {
target.saving = null;
} );
return promise;
};
/**
* Show changes in the save dialog
*
* @param {Object} doc Document
*/
ve.init.mw.ArticleTarget.prototype.showChanges = function ( doc ) {
var target = this;
// Invalidate the viewer diff on next change
this.getSurface().getModel().getDocument().once( 'transact', function () {
target.clearDiff();
} );
this.saveDialog.setDiffAndReview(
this.getWikitextDiffPromise( doc ),
this.getVisualDiffGeneratorPromise(),
this.getSurface().getModel().getDocument().getHtmlDocument()
);
};
/**
* Clear all state associated with the diff
*/
ve.init.mw.ArticleTarget.prototype.clearDiff = function () {
if ( this.saveDialog ) {
this.saveDialog.clearDiff();
}
this.wikitextDiffPromise = null;
};
/**
* Post DOM data to the Parsoid API to retrieve wikitext diff.
*
* @param {HTMLDocument} doc Document to compare against (via wikitext)
* @return {jQuery.Promise} Promise which resolves with the wikitext diff, or rejects with an error
* @fires showChanges
* @fires showChangesError
*/
ve.init.mw.ArticleTarget.prototype.getWikitextDiffPromise = function ( doc ) {
var target = this;
if ( !this.wikitextDiffPromise ) {
this.wikitextDiffPromise = this.tryWithPreparedCacheKey( doc, {
paction: 'diff',
page: this.getPageName(),
oldid: this.revid,
etag: this.etag
}, 'diff' ).then( function ( data ) {
if ( !data.diff ) {
target.emit( 'noChanges' );
}
return data.diff;
} );
this.wikitextDiffPromise
.done( this.emit.bind( this, 'showChanges' ) )
.fail( this.emit.bind( this, 'showChangesError' ) );
}
return this.wikitextDiffPromise;
};
/**
* Post wikitext to MediaWiki.
*
* This method performs a synchronous action and will take the user to a new page when complete.
*
* target.submit( wikitext, { wpSummary: 'test', wpMinorEdit: 1, wpSave: 1 } );
*
* @param {string} wikitext Wikitext to submit
* @param {Object} fields Other form fields to add (e.g. wpSummary, wpWatchthis, etc.). To actually
* save the wikitext, add { wpSave: 1 }. To go to the diff view, add { wpDiff: 1 }.
* @return {boolean} Submitting has been started
*/
ve.init.mw.ArticleTarget.prototype.submit = function ( wikitext, fields ) {
var key, $form, params;
// Prevent duplicate requests
if ( this.submitting ) {
return false;
}
// Clear autosave now that we don't expect to need it again.
// FIXME: This isn't transactional, so if the save fails we're left with no recourse.
this.clearDocState();
// Save DOM
this.submitting = true;
$form = $( '<form>' ).attr( { method: 'post', enctype: 'multipart/form-data' } ).addClass( 'oo-ui-element-hidden' );
params = ve.extendObject( {
format: 'text/x-wiki',
model: 'wikitext',
oldid: this.requestedRevId,
wpStarttime: this.startTimeStamp,
wpEdittime: this.baseTimeStamp,
wpTextbox1: wikitext,
wpEditToken: mw.user.tokens.get( 'csrfToken' ),
// MediaWiki function-verification parameters, mostly relevant to the
// classic editpage, but still required here:
wpUnicodeCheck: 'ℳ𝒲♥𝓊𝓃𝒾𝒸ℴ𝒹ℯ',
wpUltimateParam: true
}, fields );
// Add params as hidden fields
for ( key in params ) {
$form.append( $( '<input>' ).attr( { type: 'hidden', name: key, value: params[ key ] } ) );
}
// Submit the form, mimicking a traditional edit
// Firefox requires the form to be attached
$form.attr( 'action', this.submitUrl ).appendTo( 'body' ).trigger( 'submit' );
return true;
};
/**
* Get Wikitext data from the Parsoid API.
*
* This method performs an asynchronous action and uses a callback function to handle the result.
*
* target.serialize( doc ).then( function ( data ) {
* // Do something with data.content (wikitext)
* } );
*
* @param {HTMLDocument} doc Document to serialize
* @param {Function} [callback] Optional callback to run after.
* Deprecated in favor of using the returned promise.
* @return {jQuery.Promise} Serialize promise, see mw.libs.ve.targetSaver.postHtml
*/
ve.init.mw.ArticleTarget.prototype.serialize = function ( doc, callback ) {
var promise,
target = this;
// Prevent duplicate requests
if ( this.serializing ) {
return this.serializing;
}
promise = this.serializing = this.tryWithPreparedCacheKey( doc, {
paction: 'serialize',
page: this.getPageName(),
oldid: this.revid,
etag: this.etag
}, 'serialize' )
.done( this.emit.bind( this, 'serializeComplete' ) )
.fail( this.emit.bind( this, 'serializeError' ) )
.always( function () {
target.serializing = null;
} );
if ( callback ) {
OO.ui.warnDeprecation( 'Passing a callback to ve.init.mw.ArticleTarget#serialize is deprecated. Use the returned promise instead.' );
promise.then( function ( data ) {
callback.call( target, data.content );
} );
}
return promise;
};
/**
* Get list of edit notices.
*
* @return {Array} List of edit notices
*/
ve.init.mw.ArticleTarget.prototype.getEditNotices = function () {
return this.editNotices;
};
// FIXME: split out view specific functionality, emit to subclass
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.track = function ( name ) {
var mode = this.surface ? this.surface.getMode() : this.getDefaultMode();
ve.track( name, { mode: mode } );
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.createSurface = function ( dmDoc, config ) {
var surface, sections, attachedRoot;
sections = dmDoc.getNodesByType( 'section' );
if ( sections.length && sections.length === 1 ) {
attachedRoot = sections[ 0 ];
if ( !attachedRoot.isSurfaceable() ) {
throw new Error( 'Not a surfaceable node' );
}
}
// Parent method
surface = ve.init.mw.ArticleTarget.super.prototype.createSurface.call(
this,
dmDoc,
ve.extendObject( { attachedRoot: attachedRoot }, config )
);
// The following classes are used here:
// * mw-textarea-proteced
// * mw-textarea-cproteced
// * mw-textarea-sproteced
surface.$element.addClass( this.protectedClasses );
return surface;
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.getSurfaceConfig = function ( config ) {
return ve.init.mw.ArticleTarget.super.prototype.getSurfaceConfig.call( this, ve.extendObject( {
// Don't null selection on blur when editing a document.
// Do use it in new section mode as there are multiple inputs
// on the surface (header+content).
nullSelectionOnBlur: this.section === 'new'
}, config ) );
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.teardown = function () {
var surface,
target = this;
if ( !this.teardownPromise ) {
surface = this.getSurface();
// Restore access keys
if ( this.$saveAccessKeyElements ) {
this.$saveAccessKeyElements.attr( 'accesskey', ve.msg( 'accesskey-save' ) );
this.$saveAccessKeyElements = null;
}
if ( surface ) {
// Disconnect history listener
surface.getModel().disconnect( this );
}
// Parent method
this.teardownPromise = ve.init.mw.ArticleTarget.super.prototype.teardown.call( this ).then( function () {
mw.hook( 've.deactivationComplete' ).fire( target.edited );
} );
}
return this.teardownPromise;
};
/**
* Try to tear down the target, but leave ready for re-activation later
*
* Will first prompt the user if required, then call #teardown.
*
* @param {boolean} [noPrompt] Do not display a prompt to the user
* @param {string} [trackMechanism] Abort mechanism; used for event tracking if present
* @return {jQuery.Promise} Promise which resolves when the target has been torn down
*/
ve.init.mw.ArticleTarget.prototype.tryTeardown = function ( noPrompt, trackMechanism ) {
var target = this;
if ( noPrompt || !this.edited ) {
return this.teardown( trackMechanism );
} else {
return this.getSurface().dialogs.openWindow( 'abandonedit' )
.closed.then( function ( data ) {
if ( data && data.action === 'discard' ) {
return target.teardown( trackMechanism );
}
return ve.createDeferred().reject().promise();
} );
}
};
/**
* @inheritdoc
*/
ve.init.mw.ArticleTarget.prototype.setupToolbar = function () {
// Parent method
ve.init.mw.ArticleTarget.super.prototype.setupToolbar.apply( this, arguments );
this.setupToolbarSaveButton();
this.updateToolbarSaveButtonState();
if ( this.saveDialog ) {
this.editSummaryValue = this.saveDialog.editSummaryInput.getValue();
this.saveDialog.disconnect( this );
this.saveDialog = null;
}
};
/**
* Getting the message for the toolbar / save dialog save / publish button
*
* @param {boolean} [startProcess] Use version of the label for starting that process, i.e. with an ellipsis after it
* @return {Function|string} An i18n message or resolveable function
*/
ve.init.mw.ArticleTarget.prototype.getSaveButtonLabel = function ( startProcess ) {
var suffix = startProcess ? '-start' : '';
// The following messages can be used here
// * publishpage
// * publishpage-start
// * publishchanges
// * publishchanges-start
// * savearticle
// * savearticle-start
// * savechanges
// * savechanges-start
if ( mw.config.get( 'wgEditSubmitButtonLabelPublish' ) ) {
return OO.ui.deferMsg( ( !this.pageExists ? 'publishpage' : 'publishchanges' ) + suffix );
}
return OO.ui.deferMsg( ( !this.pageExists ? 'savearticle' : 'savechanges' ) + suffix );
};
/**
* Setup the toolbarSaveButton property to point to the save tool
*
* @method
* @abstract
*/
ve.init.mw.ArticleTarget.prototype.setupToolbarSaveButton = null;
/**
* Re-evaluate whether the article can be saved
*
* @return {boolean} The article can be saved
*/
ve.init.mw.ArticleTarget.prototype.isSaveable = function () {
var surface = this.getSurface();
if ( !surface ) {
// Called before we're attached, so meaningless; abandon for now
return false;
}
this.edited =
// Document was edited before loading
this.fromEditedState || this.preloaded ||
// Document was edited
surface.getModel().hasBeenModified() ||
// Section title (if it exists) was edited
( !!this.sectionTitle && this.sectionTitle.getValue() !== '' );
return this.edited || this.restoring;
};
/**
* Update the toolbar save button to reflect if the article can be saved
*/
ve.init.mw.ArticleTarget.prototype.updateToolbarSaveButtonState = function () {
// This should really be an emit('updateState') but that would cause
// every tool to be updated on every transaction.
this.toolbarSaveButton.onUpdateState();
};
/**
* Show a save dialog
*
* @param {string} [action] Window action to trigger after opening
* @param {string} [checkboxName] Checkbox to toggle after opening
*
* @fires saveWorkflowBegin
*/
ve.init.mw.ArticleTarget.prototype.showSaveDialog = function ( action, checkboxName ) {
var checkbox, name, currentWindow, openPromise,
firstLoad = false,
target = this;
if ( !this.isSaveable() || this.saveDialogIsOpening ) {
return;
}
currentWindow = this.getSurface().getDialogs().getCurrentWindow();
if ( currentWindow && currentWindow.constructor.static.name === 'mwSave' && ( action === 'save' || action === null ) ) {
// The current window is the save dialog, and we've gotten here via
// the save action. Trigger a save. We're doing this here instead of
// relying on an accesskey on the save button, because that has some
// cross-browser issues that makes it not work in Firefox.
currentWindow.executeAction( 'save' );
return;
}
this.saveDialogIsOpening = true;
this.emit( 'saveWorkflowBegin' );
// Preload the serialization
this.prepareCacheKey( this.getDocToSave() );
// Get the save dialog
this.getSurface().getDialogs().getWindow( 'mwSave' ).done( function ( win ) {
var data, checked,
windowAction = ve.ui.actionFactory.create( 'window', target.getSurface() );
if ( !target.saveDialog ) {
target.saveDialog = win;
firstLoad = true;
// Connect to save dialog
target.saveDialog.connect( target, {
save: 'onSaveDialogSave',
review: 'onSaveDialogReview',
preview: 'onSaveDialogPreview',
resolve: 'onSaveDialogResolveConflict',
retry: 'onSaveDialogRetry',
close: 'onSaveDialogClose'
} );
}
data = target.getSaveDialogOpeningData();
if (
( action === 'review' && !data.canReview ) ||
( action === 'preview' && !data.canPreview )
) {
target.saveDialogIsOpening = false;
return;
}
if ( firstLoad ) {
for ( name in target.checkboxesByName ) {
if ( target.initialCheckboxes[ name ] !== undefined ) {
target.checkboxesByName[ name ].setSelected( target.initialCheckboxes[ name ] );
}
}
}
if ( checkboxName && ( checkbox = target.checkboxesByName[ checkboxName ] ) ) {
checked = !checkbox.isSelected();
// Wait for native access key change to happen
setTimeout( function () {
checkbox.setSelected( checked );
} );
}
// When calling review/preview action, switch to those panels immediately
if ( action === 'review' || action === 'preview' ) {
data.initialPanel = action;
}
// Open the dialog
openPromise = windowAction.open( 'mwSave', data, action );
if ( openPromise ) {
openPromise.always( function () {
target.saveDialogIsOpening = false;
} );
}
} );
};
/**
* Get opening data to pass to the save dialog
*
* @return {Object} Opening data
*/
ve.init.mw.ArticleTarget.prototype.getSaveDialogOpeningData = function () {
var mode = this.getSurface().getMode();
return {
canPreview: mode === 'source',
canReview: !( mode === 'source' && this.section === 'new' ),
sectionTitle: this.sectionTitle && this.sectionTitle.getValue(),
saveButtonLabel: this.getSaveButtonLabel(),
checkboxFields: this.checkboxFields,
checkboxesByName: this.checkboxesByName
};
};
/**
* Move the cursor in the editor to section specified by this.section.
* Do nothing if this.section is undefined.
*/
ve.init.mw.ArticleTarget.prototype.restoreEditSection = function () {
var dmDoc, headingModel, headingView, headingText,
section = this.section,
surface = this.getSurface(),
mode = surface.getMode();
if ( section !== null && section !== 'new' && section !== '0' && section !== 'T-0' ) {
if ( mode === 'visual' ) {
dmDoc = surface.getModel().getDocument();
// In mw.libs.ve.unwrapParsoidSections we copy the data-mw-section-id from the section element
// to the heading. Iterate over headings to find the one with the correct attribute
// in originalDomElements.
dmDoc.getNodesByType( 'mwHeading' ).some( function ( heading ) {
var domElements = heading.getOriginalDomElements( dmDoc.getStore() );
if (
domElements && domElements[ 0 ].nodeType === Node.ELEMENT_NODE &&
domElements[ 0 ].getAttribute( 'data-mw-section-id' ) === section
) {
headingModel = heading;
return true;
}
return false;
} );
if ( headingModel ) {
headingView = surface.getView().getDocument().getDocumentNode().getNodeFromOffset( headingModel.getRange().start );
if ( new mw.Uri().query.summary === undefined ) {
headingText = headingView.$element.text();
}
if ( !this.enableVisualSectionEditing ) {
this.goToHeading( headingView );
}
}
} else if ( mode === 'source' ) {
// With elements of extractSectionTitle + stripSectionName TODO:
// Arguably, we should just throw this through the API and then do
// the same extract-text pass we do in visual mode. Would save us
// having to think about wikitext here.
headingText = surface.getModel().getDocument().data.getText(
false,
surface.getModel().getDocument().getDocumentNode().children[ 0 ].getRange()
)
// Extract the title
.replace( /^\s*=+\s*(.*?)\s*=+\s*$/, '$1' )
// Remove links
.replace( /\[\[:?([^[|]+)\|([^[]+)\]\]/, '$2' )
.replace( /\[\[:?([^[]+)\|?\]\]/, '$1' )
.replace( new RegExp( '\\[(?:' + ve.init.platform.getUnanchoredExternalLinkUrlProtocolsRegExp().source + ')([^ ]+?) ([^\\[]+)\\]', 'i' ), '$3' )
// Cheap HTML removal
.replace( /<[^>]+?>/g, '' );
}
if ( headingText ) {
this.initialEditSummary =
'/* ' +
ve.graphemeSafeSubstring( headingText, 0, 244 ) +
' */ ';
}
}
};
/**
* Move the cursor to a given heading and scroll to it.
*
* @param {ve.ce.HeadingNode} headingNode Heading node to scroll to
*/
ve.init.mw.ArticleTarget.prototype.goToHeading = function ( headingNode ) {
var nextNode, offset,
target = this,
offsetNode = headingNode,
surface = this.getSurface(),
surfaceModel = surface.getModel(),
surfaceView = surface.getView(),
lastHeadingLevel = -1;
// Find next sibling which isn't a heading
while ( offsetNode instanceof ve.ce.HeadingNode && offsetNode.getModel().getAttribute( 'level' ) > lastHeadingLevel ) {
lastHeadingLevel = offsetNode.getModel().getAttribute( 'level' );
// Next sibling
nextNode = offsetNode.parent.children[ offsetNode.parent.children.indexOf( offsetNode ) + 1 ];
if ( !nextNode ) {
break;
}
offsetNode = nextNode;
}
offset = surfaceModel.getDocument().data.getNearestContentOffset(
offsetNode.getModel().getOffset(), 1
);
function scrollAndSetSelection() {
surfaceModel.setLinearSelection( new ve.Range( offset ) );
// Focussing the document triggers showSelection which calls scrollIntoView
// which uses a jQuery animation, so make sure this is aborted.
$( OO.ui.Element.static.getClosestScrollableContainer( surfaceView.$element[ 0 ] ) ).stop( true );
target.scrollToHeading( headingNode );
}
if ( surfaceView.isFocused() ) {
scrollAndSetSelection();
} else {
// onDocumentFocus is debounced, so wait for that to happen before setting
// the model selection, otherwise it will get reset
surfaceView.once( 'focus', scrollAndSetSelection );
}
};
/**
* Scroll to a given heading in the document.
*
* @param {ve.ce.HeadingNode} headingNode Heading node to scroll to
*/
ve.init.mw.ArticleTarget.prototype.scrollToHeading = function ( headingNode ) {
var $window = $( OO.ui.Element.static.getWindow( this.$element ) );
$window.scrollTop( headingNode.$element.offset().top - this.getToolbar().$element.height() );
};
/**
* Get the hash fragment for the current section's ID using the page's PHP HTML.
*
* TODO: Do this in a less skin-dependent way
*
* @param {jQuery} [context] Page context to search in, if narrowing down the content is required
* @return {string} Hash fragment, or empty string if not found
*/
ve.init.mw.ArticleTarget.prototype.getSectionFragmentFromPage = function ( context ) {
var section, $sections, $section;
// Assume there are section edit links, as the user just did a section edit. This also means
// that the section numbers line up correctly, as not every H_ tag is a numbered section.
$sections = $( '.mw-editsection', context );
if ( this.section === 'new' ) {
// A new section is appended to the end, so take the last one.
section = $sections.length;
} else {
section = this.section;
}
if ( section > 0 ) {
$section = $sections.eq( section - 1 ).parent().find( '.mw-headline' );
if ( $section.length && $section.attr( 'id' ) ) {
return $section.attr( 'id' ) || '';
}
}
return '';
};
/**
* Switches to the wikitext editor, either keeping (default) or discarding changes.
*
* @param {boolean} [modified] Whether there were any changes at all.
*/
ve.init.mw.ArticleTarget.prototype.switchToWikitextEditor = function ( modified ) {
var dataPromise,
target = this;
// When switching with changes we always pass the full page as changes in visual section mode
// can still affect the whole document (e.g. removing a reference)
if ( modified ) {
this.section = null;
}
if ( this.isModeAvailable( 'source' ) ) {
if ( !modified ) {
dataPromise = mw.libs.ve.targetLoader.requestPageData( 'source', this.getPageName(), {
sessionStore: true,
section: this.section,
oldId: this.requestedRevId,
targetName: this.constructor.static.trackingName
} ).then(
function ( response ) { return response; },
function () {
// TODO: Some sort of progress bar?
target.switchToFallbackWikitextEditor( modified );
// Keep everything else waiting so our error handler can do its business
return ve.createDeferred().promise();
}
);
} else {
dataPromise = this.getWikitextDataPromiseForDoc( modified );
}
this.reloadSurface( 'source', dataPromise );
} else {
this.switchToFallbackWikitextEditor( modified );
}
};
/**
* Get a data promise for wikitext editing based on the current doc state
*
* @param {boolean} modified Whether there were any changes
* @return {jQuery.Promise} Data promise
*/
ve.init.mw.ArticleTarget.prototype.getWikitextDataPromiseForDoc = function ( modified ) {
var target = this;
return this.serialize( this.getDocToSave() ).then( function ( data ) {
// HACK - add parameters the API doesn't provide for a VE->WT switch
data.etag = target.etag;
data.fromEditedState = modified;
data.notices = target.remoteNotices;
data.protectedClasses = target.protectedClasses;
data.basetimestamp = target.baseTimeStamp;
data.starttimestamp = target.startTimeStamp;
data.oldid = target.revid;
data.canEdit = target.canEdit;
data.checkboxesDef = target.checkboxesDef;
// Wrap up like a response object as that is what dataPromise is expected to be
return { visualeditoredit: data };
} );
};
/**
* Switches to the fallback wikitext editor, either keeping (default) or discarding changes.
*
* @param {boolean} [modified] Whether there were any changes at all.
*/
ve.init.mw.ArticleTarget.prototype.switchToFallbackWikitextEditor = function () {};
/**
* Switch to the visual editor.
*/
ve.init.mw.ArticleTarget.prototype.switchToVisualEditor = function () {
var dataPromise, windowManager, switchWindow,
config = mw.config.get( 'wgVisualEditorConfig' ),
canSwitch = config.fullRestbaseUrl || config.allowLossySwitching,
target = this;
if ( !this.edited ) {
this.reloadSurface( 'visual' );
return;
}
// Show a discard-only confirm dialog, and then reload the whole page, if
// the server can't switch for us because that's not supported.
if ( !canSwitch ) {
windowManager = new OO.ui.WindowManager();
switchWindow = new mw.libs.ve.SwitchConfirmDialog();
$( document.body ).append( windowManager.$element );
windowManager.addWindows( [ switchWindow ] );
windowManager.openWindow( switchWindow, { mode: 'simple' } )
.closed.then( function ( data ) {
if ( data && data.action === 'discard' ) {
target.section = null;
target.reloadSurface( 'visual' );
}
windowManager.destroy();
} );
} else {
dataPromise = mw.libs.ve.targetLoader.requestParsoidData( this.getPageName(), {
oldId: this.revid,
targetName: this.constructor.static.trackingName,
modified: this.edited,
wikitext: this.getDocToSave(),
section: this.section
} );
this.reloadSurface( 'visual', dataPromise );
}
};
/**
* Switch to a different wikitext section
*
* @param {string|null} section Section to switch to: a number, 'T-'-prefixed number, 'new'
* or null (whole document)
* @param {boolean} noConfirm Switch without prompting (changes will be lost either way)
*/
ve.init.mw.ArticleTarget.prototype.switchToWikitextSection = function ( section, noConfirm ) {
var promise,
target = this;
if ( section === this.section ) {
return;
}
if ( !noConfirm && this.edited && mw.user.options.get( 'useeditwarning' ) ) {
promise = this.getSurface().dialogs.openWindow( 'abandonedit' )
.closed.then( function ( data ) {
return data && data.action === 'discard';
} );
} else {
promise = ve.createDeferred().resolve( true ).promise();
}
promise.then( function ( confirmed ) {
if ( confirmed ) {
// Section has changed and edits have been discarded, so edit summary is no longer valid
// TODO: Preserve summary if document changes can be preserved
if ( target.saveDialog ) {
target.saveDialog.reset();
}
// TODO: If switching to a non-null section, get the new section title
target.initialEditSummary = null;
target.section = section;
target.reloadSurface( 'source' );
target.updateTabs( true );
}
} );
};
/**
* Reload the target surface in the new editor mode
*
* @param {string} newMode New mode
* @param {jQuery.Promise} [dataPromise] Data promise, if any
*/
ve.init.mw.ArticleTarget.prototype.reloadSurface = function ( newMode, dataPromise ) {
this.setDefaultMode( newMode );
this.clearDiff();
// Create progress - will be discarded when surface is destroyed.
this.getSurface().createProgress(
ve.createDeferred().promise(),
ve.msg( newMode === 'source' ? 'visualeditor-mweditmodesource-progress' : 'visualeditor-mweditmodeve-progress' ),
true /* non-cancellable */
);
this.load( dataPromise );
};
/**
* Display the given redirect subtitle and redirect page content header on the page.
*
* @param {jQuery} $sub Redirect subtitle, see #buildRedirectSub
* @param {jQuery} $msg Redirect page content header, see #buildRedirectMsg
*/
ve.init.mw.ArticleTarget.prototype.updateRedirectInterface = function ( $sub, $msg ) {
var $currentSub, $currentMsg, $subtitle,
target = this;
// For the subtitle, replace the real one with ours.
// This is more complicated than it should be because we have to fiddle with the <br>.
$currentSub = $( '#redirectsub' );
if ( $currentSub.length ) {
if ( $sub.length ) {
$currentSub.replaceWith( $sub );
} else {
$currentSub.prev().filter( 'br' ).remove();
$currentSub.remove();
}
} else {
$subtitle = $( '#contentSub' );
if ( $sub.length ) {
if ( $subtitle.children().length ) {
$subtitle.append( $( '<br>' ) );
}
$subtitle.append( $sub );
}
}
if ( $msg.length ) {
$msg
// We need to be able to tell apart the real one and our fake one
.addClass( 've-redirect-header' )
.on( 'click', function ( e ) {
var windowAction = ve.ui.actionFactory.create( 'window', target.getSurface() );
windowAction.open( 'meta', { page: 'settings' } );
e.preventDefault();
} );
}
// For the content header, the real one is hidden, insert ours before it.
$currentMsg = $( '.ve-redirect-header' );
if ( $currentMsg.length ) {
$currentMsg.replaceWith( $msg );
} else {
// Hack: This is normally inside #mw-content-text, but that's hidden while editing.
$( '#mw-content-text' ).before( $msg );
}
};
/**
* Render a list of categories
*
* Duplicate items are not shown.
*
* @param {ve.dm.MetaItem[]} categoryItems Array of category metaitems to display
* @return {jQuery.Promise} A promise which will be resolved with the rendered categories
*/
ve.init.mw.ArticleTarget.prototype.renderCategories = function ( categoryItems ) {
var $normal, $hidden, categoriesNormal, categoriesHidden,
promises = [],
categories = { hidden: {}, normal: {} };
categoryItems.forEach( function ( categoryItem, index ) {
var attributes = ve.cloneObject( ve.getProp( categoryItem, 'element', 'attributes' ) );
attributes.index = index;
promises.push( ve.init.platform.linkCache.get( attributes.category ).done( function ( result ) {
var group = result.hidden ? categories.hidden : categories.normal;
// In case of duplicates, first entry wins (like in MediaWiki)
if ( !group[ attributes.category ] || group[ attributes.category ].index > attributes.index ) {
group[ attributes.category ] = attributes;
}
} ) );
} );
return ve.promiseAll( promises ).then( function () {
var $output = $( '<div>' ).addClass( 'catlinks' );
function renderPageLink( page ) {
var title = mw.Title.newFromText( page );
return $( '<a>' ).attr( 'rel', 'mw:WikiLink' ).attr( 'href', title.getUrl() ).text( title.getMainText() );
}
function renderPageLinks( pages ) {
var i, $list = $( '<ul>' );
for ( i = 0; i < pages.length; i++ ) {
$list.append( $( '<li>' ).append( renderPageLink( pages[ i ] ) ) );
}
return $list;
}
function categorySort( group, a, b ) {
return group[ a ].index - group[ b ].index;
}
categoriesNormal = Object.keys( categories.normal );
if ( categoriesNormal.length ) {
categoriesNormal.sort( categorySort.bind( null, categories.normal ) );
$normal = $( '<div>' ).addClass( 'mw-normal-catlinks' );
$normal.append(
renderPageLink( ve.msg( 'pagecategorieslink' ) ).text( ve.msg( 'pagecategories', categoriesNormal.length ) ),
ve.msg( 'colon-separator' ),
renderPageLinks( categoriesNormal )
);
$output.append( $normal );
}
categoriesHidden = Object.keys( categories.hidden );
if ( categoriesHidden.length ) {
categoriesHidden.sort( categorySort.bind( null, categories.hidden ) );
$hidden = $( '<div>' ).addClass( 'mw-hidden-catlinks' );
if ( mw.user.options.get( 'showhiddencats' ) ) {
$hidden.addClass( 'mw-hidden-cats-user-shown' );
} else if ( mw.config.get( 'wgNamespaceIds' ).category === mw.config.get( 'wgNamespaceNumber' ) ) {
$hidden.addClass( 'mw-hidden-cats-ns-shown' );
} else {
$hidden.addClass( 'mw-hidden-cats-hidden' );
}
$hidden.append(
ve.msg( 'hidden-categories', categoriesHidden.length ),
ve.msg( 'colon-separator' ),
renderPageLinks( categoriesHidden )
);
$output.append( $hidden );
}
return $output;
} );
};
// Used in tryTeardown
ve.ui.windowFactory.register( mw.widgets.AbandonEditDialog );
|