# HG changeset patch # User hertzhaft # Date 1351240334 -7200 # Node ID 8878c6e36fd5fd02d8feef71c384afea269fefad # Parent 34f893492adbc728cf21a0d7e93ed0b0d6438bb0 refactoring; define regions in JS diff -r 34f893492adb -r 8878c6e36fd5 webapp/src/main/webapp/jquery/jquery.digilib.regions.js --- a/webapp/src/main/webapp/jquery/jquery.digilib.regions.js Thu Oct 25 19:35:33 2012 +0200 +++ b/webapp/src/main/webapp/jquery/jquery.digilib.regions.js Fri Oct 26 10:32:14 2012 +0200 @@ -2,8 +2,8 @@ Mark up a digilib image with rectangular regions. -If hasRegionInfo=true reads regions from page HTML. -Element with regions has to be in digilib element, e.g. +If the parameter "processHtmlRegions" is set, the plugin reads region data from HTML. +Region data should be declared inside in the digilib element, like so: MPI fuer Wissenschaftsgeschichte @@ -13,7 +13,7 @@ According to the HTML specs, "area" and "a" elements are allowed inside of a "map". Both can have a "coords" attribute, but "area" elements can't contain child nodes. -To have regions with content use "a" tags, e.g. +To have regions with content use "a" tags, like so:. @@ -25,6 +25,19 @@ +Regions can also be defined in Javascript: +Set the parameter "regions" to an array with items. +Each item should be an object containing the following fields: + +rect + a retangle with relative coordinates (0..1); +index (optional) + a number to display in the region +attributes (optional) + HTML attributes for the region (id, class, title) +inner (optional) + inner HTML (has to be a jQuery object) + */ (function($) { @@ -41,6 +54,8 @@ }; // affine geometry plugin var geom = null; + // convenience variable, set in init() + var CSS = ''; var buttons = { defineregion : { @@ -105,14 +120,17 @@ 'regionSet' : ['regions', 'defineregion', 'removeregion', 'removeallregions', 'regioninfo', 'findcoords', 'finddata', 'lessoptions'], // url param for regions 'rg' : null, + // array with region data + 'regions' : null, // region attributes to copy from HTML 'regionAttributes' : { 'id' :1, 'href' :1, - 'title' :1, + 'name' :1, + 'alt' :1, 'target':1, - 'style' :1, - 'class' :1 + 'title' :1, + 'style' :1 } }; @@ -124,7 +142,6 @@ alert("Please turn on regions visibility!"); return; } - var cssPrefix = data.settings.cssPrefix; var $elem = data.$elem; var $body = $('body'); var bodyRect = geom.rectangle($body); @@ -132,11 +149,11 @@ var scalerRect = geom.rectangle($scaler); var pt1, pt2; // overlay prevents other elements from reacting to mouse events - var $overlay = $('
'); + var $overlay = $('
'); $body.append($overlay); bodyRect.adjustDiv($overlay); - var $regionDiv = newRegionDiv(data); - addRegionAttributes(data, $regionDiv, {'class' : cssPrefix+"regionURL"}); + var attr = {'class' : CSS+"regionURL"}; + var $regionDiv = newRegionDiv(data, attr); // mousedown handler: start sizing var regionStart = function (evt) { @@ -192,7 +209,7 @@ alert("Please turn on regions visibility!"); return; } - var selector = 'div.'+data.settings.cssPrefix+'regionURL'; + var selector = 'div.'+CSS+'regionURL'; var $regionDiv = data.$elem.find(selector).last(); $regionDiv.remove(); redisplay(data); @@ -204,7 +221,7 @@ alert("Please turn on regions visibility!"); return; } - var selector = 'div.'+data.settings.cssPrefix+'regionURL'; + var selector = 'div.'+CSS+'regionURL'; var $regionDivs = data.$elem.find(selector); $regionDivs.remove(); redisplay(data); @@ -221,18 +238,17 @@ // show region info in a window showRegionInfo : function (data) { var $elem = data.$elem; - var cssPrefix = data.settings.cssPrefix; - var infoSelector = '#'+cssPrefix+'regionInfo'; + var infoSelector = '#'+CSS+'regionInfo'; if (fn.isOnScreen(data, infoSelector)) return; // already onscreen var html = '\ -
\ - \ +
\ +
\ \ - \ - \ - \ - \ - \ + \ + \ + \ + \ + \ \
HTMLSVGCSVDigilibXHTMLSVGCSVDigilibX
\
'; @@ -245,8 +261,8 @@ $info.append(regionInfoDigilib(data, $regions)); var bind = function(name) { $info.find('.'+name).on('click.regioninfo', function () { - $info.find('div.'+cssPrefix+'info').hide(); - $info.find('div.'+cssPrefix+name).show(); + $info.find('div.'+CSS+'info').hide(); + $info.find('div.'+CSS+name).show(); fn.centerOnScreen(data, $info); }); }; @@ -264,12 +280,11 @@ // display region coordinates in an edit line showRegionCoords : function (data, $regionDiv) { var $elem = data.$elem; - var cssPrefix = data.settings.cssPrefix; var rect = $regionDiv.data('rect'); var text = $regionDiv.data('text'); var coordString = packCoords(rect, ','); var html = '\ -
\ +
\
'+text+'
\ \
'; @@ -302,9 +317,9 @@ alert('invalid coordinates: ' + coords); return; } - var cssPrefix = data.settings.cssPrefix; - var attr = { 'class' : cssPrefix+'regionURL '+cssPrefix+'findregion' }; - addRegionDiv(data, rect, attr); + var attr = { 'class' : CSS+'regionURL '+CSS+'findregion' }; + var item = { 'rect' : rect, 'attributes' : attr }; + var $regionDiv = addRegionDiv(data, item); var za = data.zoomArea; if (!fn.isFullArea(za)) { za.setCenter(rect.getCenter()).stayInside(FULL_AREA); @@ -320,35 +335,34 @@ // find coordinates and display as new region findCoords : function (data) { var $elem = data.$elem; - var cssPrefix = data.settings.cssPrefix; - var findSelector = '#'+cssPrefix+'regionFindCoords'; + var findSelector = '#'+CSS+'regionFindCoords'; if (fn.isOnScreen(data, findSelector)) return; // already onscreen var html = '\ -
\ +
\
coordinates to find:
\ -
\ + \
\ - \ + \
\ - \ - \ + \ + \
\
'; var $info = $(html); $info.appendTo($elem); var $form = $info.find('form'); - var $input = $info.find('input.'+cssPrefix+'input'); + var $input = $info.find('input.'+CSS+'input'); // handle submit $form.on('submit', function () { var coords = $input.val(); actions.regionFromCoords(data, coords); fn.withdraw($info); return false; - }); + }); // handle cancel - $form.find('.'+cssPrefix+'cancel').on('click', function () { + $form.find('.'+CSS+'cancel').on('click', function () { fn.withdraw($info); - }); + }); $info.fadeIn(); fn.centerOnScreen(data, $info); $input.focus(); @@ -357,28 +371,27 @@ // find text data and display as new region findData : function (data) { var $elem = data.$elem; - var cssPrefix = data.settings.cssPrefix; - var findSelector = '#'+cssPrefix+'regionFindData'; + var findSelector = '#'+CSS+'regionFindData'; if (fn.isOnScreen(data, findSelector)) return; // already onscreen var textOptions = getTextOptions(data, 'regionHTML'); var html = '\ -
\ +
\
text to find:
\ -
\ + \
\ - \ '+textOptions+'\ \
\ - \ - \ - \ + \ + \ + \
\
'; var $info = $(html); $info.appendTo($elem); var $form = $info.find('form'); - var $input = $info.find('input.'+cssPrefix+'input'); + var $input = $info.find('input.'+CSS+'input'); var $select = $info.find('select'); var findRegion = function () { var coords = $select.val(); @@ -390,9 +403,9 @@ $form.on('submit', findRegion); $select.on('change', findRegion); // handle cancel - $form.find('.'+cssPrefix+'cancel').on('click', function () { + $form.find('.'+CSS+'cancel').on('click', function () { fn.withdraw($info); - }); + }); $info.fadeIn(); fn.centerOnScreen(data, $info); $input.focus(); @@ -430,11 +443,10 @@ }; // create a new regionDiv and add it to data.$elem - var newRegionDiv = function (data) { - var settings = data.settings; - var cssPrefix = settings.cssPrefix; - var cls = cssPrefix+'region '+cssPrefix+'overlay'; + var newRegionDiv = function (data, attr) { + var cls = CSS+'region'; var $regionDiv = $('