MediaWiki:Gadget-dropDisplay-core.js

From Old School Near-Reality Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
  1 // <nowiki>
  2 'use strict'; 
  3 
  4 var userSettings,
  5 	settingsName = 'rsw-drop-display-settings',
  6 	defaultSettings = {_ratedisp: 2, _valcoldisp: 1};
  7 
  8 // grabs settings from localstorage (or defaults if not supported)
  9 function getSettings(){
 10 	var settings = {};
 11 	if (rswiki.hasLocalStorage()) {
 12 		try {
 13 			settings = JSON.parse(localStorage.getItem(settingsName));
 14 		} catch (err) {
 15 			settings = {};
 16 		}
 17 		if (settings === null) {
 18 			settings = {};
 19 		}
 20 
 21 	}
 22 	userSettings = $.extend({}, defaultSettings, settings);
 23 }
 24 
 25 // put settings back into localstorage
 26 function updateSettings(){
 27 	if (!rswiki.hasLocalStorage()) return;
 28 	localStorage.setItem(settingsName, JSON.stringify(userSettings));
 29 }
 30 
 31 // change rate to a different display
 32 function changeRateDisp(data, selected, init){
 33 	var  rdisp = 0, attr = '', append = '', upsettings = false;
 34 	if (init == true) {
 35 		rdisp = data;
 36 	} else {
 37 		rdisp = data.getData();
 38 	}
 39 	switch(rdisp) {
 40 		case 1:
 41 			attr = 'data-drop-fraction';
 42 			upsettings = true;
 43 			break;
 44 		case 2:
 45 			attr = 'data-drop-oneover';
 46 			upsettings = true;
 47 			break;
 48 		case 3:
 49 			attr = 'data-drop-percent';
 50 			upsettings = true;
 51 			append = '%';
 52 			break;
 53 		default:
 54 			mw.log('Invalid rate display type '+rdisp);
 55 	}
 56 	$('table.item-drops td span[data-drop-fraction]').each(function(){
 57 		var $cell = $(this), newText = $cell.attr(attr);
 58 		$cell.text(newText + append);
 59 	});
 60 	if (upsettings == true) {
 61 		userSettings._ratedisp = rdisp;
 62 		updateSettings();
 63 	}
 64 }
 65 
 66 // change value display column
 67 function changeValDisp(data, selected, init){
 68 	var tbl = 'table.item-drops.filterable';
 69 	var vdisp = 0, upsettings = false;
 70 	if (init == true) {
 71 		vdisp = data;
 72 	} else {
 73 		vdisp = data.getData();
 74 	}
 75 	switch (vdisp) {
 76 		case 1:
 77 			$(tbl).each(function(){
 78 				$(this).removeClass('rsw-dropsline-hidege');
 79 				$(this).addClass('rsw-dropsline-hidealch');
 80 			});
 81 			upsettings = true;
 82 			break;
 83 		case 2:
 84 			$(tbl).each(function(){
 85 				$(this).addClass('rsw-dropsline-hidege');
 86 				$(this).removeClass('rsw-dropsline-hidealch');
 87 			});
 88 			upsettings = true;
 89 			break;
 90 		case 3:
 91 			$(tbl).each(function(){
 92 				$(this).removeClass('rsw-dropsline-hidege');
 93 				$(this).removeClass('rsw-dropsline-hidealch');
 94 			});
 95 			upsettings = true;
 96 			break;
 97 		default:
 98 			mw.log('Invalid value column display type '+vdisp);
 99 	}
100 	if (upsettings == true) {
101 		userSettings._valcoldisp = vdisp;
102 		updateSettings();
103 	}
104 }
105 
106 // initialise
107 function init() {
108 	var $tables = $('table.item-drops.filterable'),
109 	$overlay = $('<div id="rsw-drops-overlay2">').appendTo('body'),
110 	popup,
111 	fieldset, applyButton, 
112 	fractionButton, overoneButton, percentButton, rateGroup,
113 	gecolButton, alcolButton, bothcolButton, valGroup,
114 	typeGroup;
115 
116 	// get settings and update display
117 	getSettings();
118 	changeRateDisp(userSettings._ratedisp, true, true);
119 	changeValDisp(userSettings._valcoldisp, true, true);
120 	
121 	// build popup
122 	// Droprate column
123 	fractionButton = new OO.ui.ButtonOptionWidget({
124 		data: 1,
125 		label: 'Default fraction (a/b)',
126 		title: 'Displays a fraction without simplifying, in a/b style. Example: 4/128. This is the default display.',
127 	});
128 	overoneButton = new OO.ui.ButtonOptionWidget({
129 		data: 2,
130 		label: 'One-over fraction (1/x, default)',
131 		title: 'Displays a fraction simplified to 1/x. Fraction denominators are rounded to 3 significant figures.',
132 	});
133 	percentButton = new OO.ui.ButtonOptionWidget({
134 		data: 3,
135 		label: 'Percentage (y%)',
136 		title: 'Displays a percentage (y%), rounded to 3 significant figures.',
137 	});
138 	rateGroup = new OO.ui.ButtonSelectWidget({
139 		items: [overoneButton, fractionButton, percentButton]
140 	});
141 	rateGroup.selectItemByData(userSettings._ratedisp);
142 	rateGroup.on('choose',changeRateDisp);
143 
144 	//Price/Value columns
145 	gecolButton = new OO.ui.ButtonOptionWidget({
146 		data: 1,
147 		label: 'Show GE Price',
148 		title: 'Display only the GE Price column.',
149 	});
150 	alcolButton = new OO.ui.ButtonOptionWidget({
151 		data: 2,
152 		label: 'Show High alch value',
153 		title: 'Display only the high alch value column.',
154 	});
155 	bothcolButton = new OO.ui.ButtonOptionWidget({
156 		data: 3,
157 		label: 'Show both',
158 		title: 'Display both the GE Price and high alch value columns.',
159 	});
160 	valGroup = new OO.ui.ButtonSelectWidget({
161 		items: [gecolButton, alcolButton, bothcolButton]
162 	});
163 	valGroup.selectItemByData(userSettings._valcoldisp);
164 	valGroup.on('choose',changeValDisp);
165 
166 
167 	fieldset = new OO.ui.FieldsetLayout({});
168 	fieldset.addItems([
169 		new OO.ui.FieldLayout(rateGroup, {label: 'Display drops as: ', align: 'top'}),
170 		new OO.ui.FieldLayout(valGroup, {label: 'Display price/value as: ', align: 'top'}),
171 	]);
172 	
173 	popup = new OO.ui.PopupWidget({
174 		padded: true,
175 		autoClose: true,
176 		$content: fieldset.$element,
177 		width: 'auto',
178 		position: 'above',
179 		align: 'force-right',
180 		head: true,
181 		label: 'Display settings',
182 		classes: ['rsw-drop-display-popup2']
183 	});
184 	$overlay.append(popup.$element);
185 	
186 	// add button to each table
187 	$tables.each(function(i,e){
188 		var button = new OO.ui.ButtonWidget({
189 			icon: 'advanced',
190 			title: 'Open display settings',
191 			framed: false,
192 			invisibleLabel: true,
193 			classes: ['rsw-drop-display-button2']
194 		});
195 		button.on('click',function(){
196 			// move popup to the clicked button
197 			popup.setFloatableContainer(button.$element);
198 			// reset buttons (i.e. open popup, click a button but don't apply, close - next time it is opened it should show the current setting not the unapplied one)
199 			rateGroup.selectItemByData(userSettings._ratedisp);
200 			valGroup.selectItemByData(userSettings._valcoldisp);
201 			// show popup
202 			popup.toggle(true);
203 		});
204 		
205 		var $cell = $(e).find('th.drop-disp-btn');
206 		$cell.append(button.$element);
207 	});
208 }
209 
210 $(init);
211 // </nowiki>