https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114330
Revision: 114330
Author: foxtrott
Date: 2012-03-20 23:30:51 +0000 (Tue, 20 Mar 2012)
Log Message:
-----------
first version of distance filter for filtered format; some small fixes
Modified Paths:
--------------
trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered.php
trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered_Item.php
trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Value.php
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.js
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.value-filter.js
trunk/extensions/SemanticResultFormats/Filtered/views/SRF_FV_List.php
Added Paths:
-----------
trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Distance.php
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.distance-filter.js
trunk/extensions/SemanticResultFormats/Filtered/skins/ext.srf.filtered.distance-filter.css
Modified: trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered.php
===================================================================
--- trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered.php
2012-03-20 23:10:41 UTC (rev 114329)
+++ trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered.php
2012-03-20 23:30:51 UTC (rev 114330)
@@ -18,6 +18,7 @@
$wgAutoloadClasses['SRF_Filtered_Filter'] = $formatDir .
'filters/SRF_Filtered_Filter.php';
$wgAutoloadClasses['SRF_FF_Value'] = $formatDir . 'filters/SRF_FF_Value.php';
+$wgAutoloadClasses['SRF_FF_Distance'] = $formatDir .
'filters/SRF_FF_Distance.php';
/**
@@ -68,6 +69,7 @@
*/
private $mFilterTypes = array(
'value' => 'SRF_FF_Value',
+ 'distance' => 'SRF_FF_Distance',
);
private $mViews;
Modified: trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered_Item.php
===================================================================
--- trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered_Item.php
2012-03-20 23:10:41 UTC (rev 114329)
+++ trunk/extensions/SemanticResultFormats/Filtered/SRF_Filtered_Item.php
2012-03-20 23:30:51 UTC (rev 114330)
@@ -24,16 +24,16 @@
$this->mQueryPrinter = $queryPrinter;
}
- public function setDataForView ( $viewId, &$data ) {
- $this->mItemData[$viewId] = $data;
+ public function setData ( $viewOrFilterId, $data ) {
+ $this->mItemData[$viewOrFilterId] = $data;
}
- public function unsetDataForView ( $viewId ) {
- unset( $this->mItemData[$viewId] );
+ public function unsetData ( $viewOrFilterId ) {
+ unset( $this->mItemData[$viewOrFilterId] );
}
- public function getDataForView ( $viewId ) {
- return $this->mItemData[$viewId];
+ public function getData ( $viewOrFilterId ) {
+ return $this->mItemData[$viewOrFilterId];
}
public function getValue() {
Added:
trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Distance.php
===================================================================
--- trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Distance.php
(rev 0)
+++ trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Distance.php
2012-03-20 23:30:51 UTC (rev 114330)
@@ -0,0 +1,130 @@
+<?php
+
+/**
+ * File holding the SRF_FF_Distance class
+ *
+ * @author Stephan Gambke
+ * @file
+ * @ingroup SemanticResultFormats
+ */
+
+/**
+ * The SRF_FF_Distance class.
+ *
+ * Available parameters for this filter:
+ * distance filter origin: the point from which the distance is measured
(address or geo coordinate)
+ * distance filter property: the property containing the point to which
distance is measured - not implemented yet
+ * distance filter unit: the unit in which the distance is measured
+ *
+ * @ingroup SemanticResultFormats
+ */
+class SRF_FF_Distance extends SRF_Filtered_Filter {
+
+ private $mUnit = null;
+ private $mMaxDistance = 1;
+
+ public function __construct( &$results, SMWPrintRequest $printRequest,
SRFFiltered &$queryPrinter ) {
+ parent::__construct($results, $printRequest, $queryPrinter);
+
+ if ( !defined('Maps_VERSION') || version_compare( Maps_VERSION,
'1.0', '<' ) ) {
+ throw new FatalError('You need to have the <a
href="http://www.mediawiki.org/wiki/Extension:Maps">Maps</a> extension version
1.0 or higher installed in order to use the distance filter.<br />');
+ }
+
+ MapsGeocoders::init();
+
+ $params = $this->getActualParameters();
+
+ if ( array_key_exists( 'distance filter origin', $params ) ) {
+ $origin = MapsGeocoders::attemptToGeocode(
$params['distance filter origin'] );
+ } else {
+ $origin = array( 'lat'=>'0', 'lon' => '0' );
+ }
+
+ if ( array_key_exists( 'distance filter unit', $params ) ) {
+ $this->mUnit = MapsDistanceParser::getValidUnit(
$params['distance filter unit'] );
+ } else {
+ $this->mUnit = MapsDistanceParser::getValidUnit();
+ }
+
+ $targetLabel = $printRequest->getLabel();
+
+ foreach ( $this->getQueryResults() as $id => $filteredItem ) {
+
+ $row = $filteredItem->getValue();
+
+ // $filteredItem is of class SRF_Filtered_Item
+ // $row is an array of SMWResultArray
+
+ foreach ( $row as $field ) {
+
+ // $field is an SMWResultArray
+
+ $label = $field->getPrintRequest()->getLabel();
+
+ if ($label === $targetLabel) {
+ $field->reset();
+ $dataValue =
$field->getNextDataValue(); // only use first value
+
+ if ( $dataValue !== false ) {
+
+ $posText =
$dataValue->getShortText( SMW_OUTPUT_WIKI, false );
+ $pos =
MapsGeocoders::attemptToGeocode( $posText );
+
+ if ( is_array( $pos ) ){
+ $distance = round(
MapsGeoFunctions::calculateDistance( $origin, $pos ) /
MapsDistanceParser::getUnitRatio( $this->mUnit ) );
+
+ if ( $distance >
$this->mMaxDistance ) {
+
$this->mMaxDistance = $distance;
+ }
+
+ } else {
+ $distance = -1;
+ }
+
+ } else {
+ $distance = -1; // no location
given
+ }
+ $filteredItem->setData(
'distance-filter', $distance );
+ break;
+ }
+
+ }
+ }
+
+ if ( $this->mMaxDistance > 1 ) {
+ $base = pow( 10, floor( log10( $this->mMaxDistance ) )
);
+ $this->mMaxDistance = ceil ( $this->mMaxDistance /
$base ) * $base;
+ }
+
+ }
+
+ /**
+ * Returns the name (string) or names (array of strings) of the resource
+ * modules to load.
+ *
+ * @return string|array
+ */
+ public function getResourceModules() {
+ return 'ext.srf.filtered.distance-filter';
+ }
+
+ /**
+ * Returns an array of config data for this filter to be stored in the
JS
+ * @return null
+ */
+ public function getJsData() {
+ $params = $this->getActualParameters();
+
+ $ret = array();
+
+ $ret['unit'] = $this->mUnit;
+ $ret['max'] = $this->mMaxDistance;
+
+ if ( array_key_exists( 'distance filter collapsible', $params )
) {
+ $ret['collapsible'] = trim($params['distance filter
collapsible']);
+ }
+
+ return $ret;
+ }
+
+}
Property changes on:
trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Distance.php
___________________________________________________________________
Added: svn:eol-style
+ native
Modified:
trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Value.php
===================================================================
--- trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Value.php
2012-03-20 23:10:41 UTC (rev 114329)
+++ trunk/extensions/SemanticResultFormats/Filtered/filters/SRF_FF_Value.php
2012-03-20 23:30:51 UTC (rev 114330)
@@ -19,18 +19,6 @@
class SRF_FF_Value extends SRF_Filtered_Filter {
/**
- * Returns the HTML text that is to be included for this view.
- *
- * This text will appear on the page in a div that has the view's id
set as
- * class.
- *
- * @return string
- */
- public function getResultText() {
- return '';
- }
-
- /**
* Returns the name (string) or names (array of strings) of the resource
* modules to load.
*
Added:
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.distance-filter.js
===================================================================
---
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.distance-filter.js
(rev 0)
+++
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.distance-filter.js
2012-03-20 23:30:51 UTC (rev 114330)
@@ -0,0 +1,225 @@
+/**
+ * File holding the distance-filter plugin
+ *
+ * For this plugin to work, the filtered plugin needs to be available first.
+ *
+ * @author Stephan Gambke
+ * @file
+ * @ingroup SemanticResultFormats
+ */
+
+(function ($) {
+
+ var methods = {
+
+ init: function( args ){
+
+ function update( filtered, filterDistance, target ) {
+
+ var values =
filtered.data('ext.srf.filtered')['values'];
+
+ for ( i in values ) {
+
+ var distance =
values[i]['data']['distance-filter'];
+
+ filtered.filtered(
'voteItemVisibilityAndUpdate', {
+ 'filter': 'value',
+ 'printout' : target,
+ 'visible': distance <=
filterDistance,
+ 'item': i
+ });
+
+ }
+
+ } // function update( filtered, filterDistance, target
)
+
+
+ var filtered = this;
+
+
+
+ var target = args.printout;
+ var values = this.data('ext.srf.filtered')['values'];
+ var data =
this.data('ext.srf.filtered')['data']['filterdata']['distance'][target];
+
+ // build filter controls
+ var filtercontrols =
this.children('.filtered-filters').children('.filtered-distance');
+
+ // insert the label of the printout this filter filters
on
+
+ for ( var i in values ) break;
+
+ var readoutAndSlider = $('<tr>');
+
+ var readout = $('<div
class="filtered-distance-readout">' + data['max'] + data['unit'] + '</div>' );
+ var slider = $('<div
class="filtered-distance-slider">');
+
+ var readoutTD = $('<td
class="filtered-distance-readout-cell">');
+ var sliderTD = $('<td
class="filtered-distance-slider-cell">');
+
+ readoutTD.append( readout );
+ sliderTD.append( slider );
+
+ readoutAndSlider
+ .append( readoutTD )
+ .append( sliderTD );
+
+ filtercontrols
+ .append( '<div class="filtered-distance-label"><span>'
+ values[i]['printouts'][target]['label'] + '</span></div>' )
+ .append( readoutAndSlider );
+
+ readoutAndSlider.wrap('<table><tbody>');
+
+ readout.width( readout.width() ); // fix width of
readout
+
+ slider.slider({
+ animate: true,
+ max: data['max'],
+ value: data['max'],
+ slide: function(event, ui) {
+ readout.empty().append( ui.value +
data['unit'] );
+ },
+ change: function(event, ui) {
+ update( filtered, ui.value, target );
+ }
+ });
+
+// // insert the label of the printout this filter filters
on
+// filtercontrols.append('<div
class="filtered-value-label"><span>' + values[i]['printouts'][target]['label']
+ '</span></div>');
+//
+// if ( collapsible != null && ( collapsible ==
'collapsed' || collapsible == 'uncollapsed') ) {
+// var showControl = $('<span
class="filtered-value-show">[+]</span>');
+// var hideControl = $('<span
class="filtered-value-hide">[-]</span>');
+//
+//
+// filtercontrols
+// .prepend(showControl)
+// .prepend(hideControl);
+//
+// filtercontrols = $('<div
class="filtered-value-collapsible">')
+// .appendTo(filtercontrols);
+//
+// showControl.click(function(){
+// filtercontrols.slideDown();
+// showControl.hide();
+// hideControl.show();
+// });
+//
+// hideControl.click(function(){
+// filtercontrols.slideUp();
+// showControl.show();
+// hideControl.hide();
+// });
+//
+// if ( collapsible == 'collapsed' ) {
+// hideControl.hide();
+// filtercontrols.slideUp(0);
+// } else {
+// showControl.hide();
+// }
+//
+// }
+//
+// // set default config values
+// filtered.filtered( 'setFilterData', {filter: 'value',
printout: target, configvar: 'use or', configvalue: true} );
+//
+//
+// // insert switches
+// if ( switches != null && switches.length > 0 ) {
+//
+// var switchControls = $('<div
class="filtered-value-switches">');
+//
+// if ( $.inArray('and or', switches) >= 0 ) {
+//
+// var andorControl = $('<div
class="filtered-value-andor">');
+// var andControl = $('<input type="radio"
name="filtered-value-andor ' +
+// target + '"
class="filtered-value-andor ' + target + '" value="and">');
+//
+// var orControl = $('<input type="radio"
name="filtered-value-andor ' +
+// target + '"
class="filtered-value-andor ' + target + '" value="or" checked>');
+//
+// andControl
+// .add( orControl )
+// .change(function() {
+// filtered.filtered(
'setFilterData', {filter: 'value', printout: target, configvar: 'use or',
configvalue: orControl.is(':checked')} );
+// update( filtered,
filtercontrols, target );
+// });
+//
+// andorControl
+// .append( orControl )
+// .append(' OR ')
+// .append( andControl )
+// .append(' AND ')
+// .appendTo( switchControls );
+//
+// }
+//
+// filtercontrols.append( switchControls );
+// }
+//
+// if ( height != null ) {
+// filtercontrols = $( '<div
class="filtered-value-scrollable">' )
+// .appendTo( filtercontrols );
+//
+// filtercontrols.height( height );
+// }
+//
+//
+// var sortedDistinctValues = [];
+//
+// for ( var i in distinctValues ) {
+// sortedDistinctValues.push(i);
+// }
+//
+// sortedDistinctValues.sort();
+//
+// // insert options (checkboxes and labels) and attach
event handlers
+// // TODO: Do we need to wrap these in a form?
+// for ( var j in sortedDistinctValues ) {
+// var option = $('<div
class="filtered-value-option">');
+// var checkbox = $('<input type="checkbox"
class="filtered-value-value" value="' + sortedDistinctValues[j] + '" >');
+//
+// // attach event handler
+// checkbox.change(function( evt ){
+// update(filtered, filtercontrols,
target);
+// });
+//
+// option
+// .append(checkbox)
+// .append(sortedDistinctValues[j]);
+//
+// filtercontrols
+// .append(option);
+//
+// }
+
+ return this;
+ },
+
+ alert: function(){
+ alert('DistanceFilter!');
+ return this;
+ }
+
+ };
+
+ distanceFilter = function( method ) {
+
+ // Method calling logic
+ if ( methods[method] ) {
+ return methods[ method ].apply( this,
Array.prototype.slice.call( arguments, 1 ));
+ } else if ( typeof method === 'object' || ! method ) {
+ return methods.init.apply( this, arguments );
+ } else {
+ $.error( 'Method ' + method + ' does not exist on
jQuery.filtered.distanceFilter' );
+ }
+
+
+ };
+
+ // attach ListView to all Filtered query printers
+ // let them sort out, if ListView is actually applicable to them
+ jQuery('.filtered').filtered('attachFilter', 'distance', distanceFilter
);
+
+})(jQuery);
+
Property changes on:
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.distance-filter.js
___________________________________________________________________
Added: svn:eol-style
+ native
Modified:
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.js
===================================================================
--- trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.js
2012-03-20 23:10:41 UTC (rev 114329)
+++ trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.js
2012-03-20 23:30:51 UTC (rev 114330)
@@ -30,13 +30,11 @@
// init housekeeping on values
for (i in data['values']) {
- data['values'][i]['data'] = {
-
- 'visibility': {
- 'overall' : true,
- 'votes' : {}
- }
+ data['values'][i]['data']['visibility']
= {
+ 'overall' : true,
+ 'votes' : {}
}
+
}
return this;
Modified:
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.value-filter.js
===================================================================
---
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.value-filter.js
2012-03-20 23:10:41 UTC (rev 114329)
+++
trunk/extensions/SemanticResultFormats/Filtered/libs/ext.srf.filtered.value-filter.js
2012-03-20 23:30:51 UTC (rev 114330)
@@ -16,67 +16,68 @@
function update( filtered, filtercontrols, target ) {
- var values =
filtered.data('ext.srf.filtered')['values'];
- var selectedInputs =
filtercontrols.children('div.filtered-value-option').children('input:checked');
-
- // show all if no value is checked
- if ( selectedInputs.length == 0 ) {
- for ( i in values ) {
- filtered.filtered(
'voteItemVisibilityAndUpdate', {
- 'filter':
'value',
- 'printout' :
target,
- 'visible': true,
- 'item': i
- });
- }
-
- } else {
-
- for ( i in values ) {
+ var values =
filtered.data('ext.srf.filtered')['values'];
+ var selectedInputs =
filtercontrols.children('div.filtered-value-option').children('input:checked');
- var printoutValues =
values[i]['printouts'][target]['values'];
- var useOr =
filtered.filtered( 'getFilterData', {filter: 'value', printout: target,
configvar: 'use or'} );
+ // show all if no value is checked
+ if ( selectedInputs.length == 0 ) {
+ for ( i in values ) {
+ filtered.filtered(
'voteItemVisibilityAndUpdate', {
+ 'filter': 'value',
+ 'printout' : target,
+ 'visible': true,
+ 'item': i
+ });
+ }
- if ( useOr ) {
- var selected =
false;
+ } else {
- for ( var j in
printoutValues ) {
+ for ( i in values ) {
-
selectedInputs.each(function(){
-
selected = selected || ( printoutValues[j] == $(this).attr('value') );
- });
- }
- } else {
- var selected =
( printoutValues.length > 0 );
+ var printoutValues =
values[i]['printouts'][target]['values'];
+ var useOr = filtered.filtered(
'getFilterData', {filter: 'value', printout: target, configvar: 'use or'} );
- if ( selected )
{
- // try
to find each required value
-
selectedInputs.each(function(){
+ if ( useOr ) {
+ var selected = false;
-
var selectedFoundInPrintout = false;
-
for ( var j in printoutValues ) {
-
selectedFoundInPrintout = selectedFoundInPrintout || (
printoutValues[j] == $(this).attr('value') );
+ for ( var j in
printoutValues ) {
-
if ( selectedFoundInPrintout ) {
-
break;
-
}
+
selectedInputs.each(function(){
+
selected = selected || ( printoutValues[j] == $(this).attr('value') );
+ });
+ }
+ } else {
+ var selected = (
printoutValues.length > 0 );
+
+ if ( selected ) {
+ // try to find
each required value
+
selectedInputs.each(function(){
+
+ var
selectedFoundInPrintout = false;
+ for (
var j in printoutValues ) {
+
selectedFoundInPrintout = selectedFoundInPrintout || ( printoutValues[j] ==
$(this).attr('value') );
+
+
if ( selectedFoundInPrintout ) {
+
break;
}
-
selected = selected && selectedFoundInPrintout;
- });
- }
+ }
+
selected = selected && selectedFoundInPrintout;
+ });
}
+ }
- filtered.filtered(
'voteItemVisibilityAndUpdate', {
- 'filter':
'value',
- 'printout' :
target,
- 'visible':
selected,
- 'item': i
- });
+ filtered.filtered(
'voteItemVisibilityAndUpdate', {
+ 'filter': 'value',
+ 'printout' : target,
+ 'visible': selected,
+ 'item': i
+ });
- }
}
- }
+ }
+ } // function update( filtered, filtercontrols, target
)
+
var filtered = this;
var values = this.data('ext.srf.filtered')['values'];
@@ -112,10 +113,10 @@
filtercontrols.append('<div
class="filtered-value-label"><span>' + values[i]['printouts'][target]['label']
+ '</span></div>');
if ( collapsible != null && ( collapsible ==
'collapsed' || collapsible == 'uncollapsed') ) {
+
var showControl = $('<span
class="filtered-value-show">[+]</span>');
var hideControl = $('<span
class="filtered-value-hide">[-]</span>');
-
filtercontrols
.prepend(showControl)
.prepend(hideControl);
@@ -123,21 +124,23 @@
filtercontrols = $('<div
class="filtered-value-collapsible">')
.appendTo(filtercontrols);
+ var outercontrols = filtercontrols
+
showControl.click(function(){
- filtercontrols.slideDown();
+ outercontrols.slideDown();
showControl.hide();
hideControl.show();
});
hideControl.click(function(){
- filtercontrols.slideUp();
+ outercontrols.slideUp();
showControl.show();
hideControl.hide();
});
if ( collapsible == 'collapsed' ) {
hideControl.hide();
- filtercontrols.slideUp(0);
+ outercontrols.slideUp(0);
} else {
showControl.hide();
}
Added:
trunk/extensions/SemanticResultFormats/Filtered/skins/ext.srf.filtered.distance-filter.css
===================================================================
---
trunk/extensions/SemanticResultFormats/Filtered/skins/ext.srf.filtered.distance-filter.css
(rev 0)
+++
trunk/extensions/SemanticResultFormats/Filtered/skins/ext.srf.filtered.distance-filter.css
2012-03-20 23:30:51 UTC (rev 114330)
@@ -0,0 +1,41 @@
+/*
+ Stylesheet for the value filter
+*/
+
+.filtered-filters .filtered-distance {
+ overflow: visible;
+ border: 1px solid #aaa;
+ padding: 1em;
+ margin: 1em;
+}
+
+.filtered-filters .filtered-distance .filtered-distance-label {
+ height: 0;
+}
+
+.filtered-filters .filtered-distance .filtered-distance-label span {
+ background-color: white;
+ font-weight: bold;
+
+ position: relative;
+ top: -1.8em;
+ left: 0;
+
+ padding: 0 1em;
+}
+
+.filtered-filters .filtered-distance .filtered-distance-slider-cell {
+ width: 100%;
+}
+
+.filtered-filters .filtered-distance .filtered-distance-readout {
+ margin: 0 1em;
+ text-align: right;
+}
+
+.filtered-filters .filtered-distance .filtered-distance-slider {
+ border: 1px solid #aaa;
+ border-radius: 3px;
+ background: #f1f2ff;
+ margin: 0 1em;
+}
Property changes on:
trunk/extensions/SemanticResultFormats/Filtered/skins/ext.srf.filtered.distance-filter.css
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/extensions/SemanticResultFormats/Filtered/views/SRF_FV_List.php
===================================================================
--- trunk/extensions/SemanticResultFormats/Filtered/views/SRF_FV_List.php
2012-03-20 23:10:41 UTC (rev 114329)
+++ trunk/extensions/SemanticResultFormats/Filtered/views/SRF_FV_List.php
2012-03-20 23:30:51 UTC (rev 114330)
@@ -138,6 +138,7 @@
foreach ( $row as $field ) {
$first_value = true;
+ $field->reset();
while ( ( $text = $field->getNextText(
SMW_OUTPUT_WIKI, $this->getQueryPrinter()->getLinker( $first_col ) ) ) !==
false ) {
if ( !$first_col && !$found_values ) {
// first values after first column
$result .= ' (';
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs