Mercurial > hg > digilib-old
changeset 794:ad5cc0212b66 jquery
merge
with d742bd92b05a03544c3298fccd07962c6d75647e
author | robcast |
---|---|
date | Fri, 18 Feb 2011 15:18:55 +0100 |
parents | 63c1b33e38b1 (current diff) d742bd92b05a (diff) |
children | 00d37112d062 |
files | client/digitallibrary/jquery/jquery.digilib.js |
diffstat | 7 files changed, 207 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/client/digitallibrary/jquery/jquery-test-full.html Fri Feb 18 15:16:30 2011 +0100 +++ b/client/digitallibrary/jquery/jquery-test-full.html Fri Feb 18 15:18:55 2011 +0100 @@ -59,6 +59,7 @@ <script type="text/javascript" src="jquery.cookie.js"></script> <script type="text/javascript" src="jquery.digilib.js"></script> <script type="text/javascript" src="jquery.digilib.geometry.js"></script> + <script type="text/javascript" src="jquery.digilib.regions.js"></script> <link rel="stylesheet" type="text/css" href="jquery.digilib.css" />
--- a/client/digitallibrary/jquery/jquery.digilib.css Fri Feb 18 15:16:30 2011 +0100 +++ b/client/digitallibrary/jquery/jquery.digilib.css Fri Feb 18 15:18:55 2011 +0100 @@ -56,6 +56,11 @@ z-index: 1000; } +div.digilib div.region { + background-color: red; + opacity: 0.5; +} + /* special definitions for fullscreen */ div.digilib.dl_fullscreen div.buttons { position: fixed;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/digitallibrary/jquery/jquery.digilib.regions.js Fri Feb 18 15:18:55 2011 +0100 @@ -0,0 +1,201 @@ +/** optional digilib regions plugin + +markup a digilib image with rectangular regions + +TODO: +- store region in params/cookie, regarding zoom, mirror, rotation (like marks) +- set regions programmatically +- read regions from params/cookie and display +- backlink mechanism +- don't write to data.settings? +*/ + +(function($) { + // the data object passed by digilib + var data; + var buttons; + var fn; + var geom; + + var buttons = { + addregion : { + onclick : "setRegion", + tooltip : "set a region", + icon : "addregion.png" + }, + delregion : { + onclick : "removeRegion", + tooltip : "delete the last region", + icon : "delregion.png" + }, + regions : { + onclick : "toggleRegions", + tooltip : "show or hide regions", + icon : "regions.png" + }, + regioninfo : { + onclick : "infoRegions", + tooltip : "information about regions", + icon : "regioninfo.png" + } + }; + var regionSet = ['addregion', 'delregion', 'regions', 'regioninfo', 'lessoptions']; + + var actions = { + // define a region interactively with two clicked points + "setRegion" : function(data) { + $elem = data.$elem; + $scaler = data.$scaler; + var pt1, pt2; + var $regionDiv = $('<div class="region" style="display:none"/>'); + $regionDiv.attr("id", "region" + data.regions.length); + $elem.append($regionDiv); + var picRect = geom.rectangle($scaler); + + var regionStart = function (evt) { + pt1 = geom.position(evt); + // setup and show zoom div + pt1.adjustDiv($regionDiv); + $regionDiv.width(0).height(0); + $regionDiv.show(); + // register events + $elem.bind("mousemove.dlRegion", regionMove); + $elem.bind("mouseup.dlRegion", regionEnd); + return false; + }; + + // mouse move handler + var regionMove = function (evt) { + pt2 = geom.position(evt); + var rect = geom.rectangle(pt1, pt2); + rect.clipTo(picRect); + // update zoom div + rect.adjustDiv($regionDiv); + return false; + }; + + // mouseup handler: end moving + var regionEnd = function (evt) { + pt2 = geom.position(evt); + // assume a click and continue if the area is too small + var clickRect = geom.rectangle(pt1, pt2); + if (clickRect.getArea() <= 5) return false; + // unregister events + $elem.unbind("mousemove.dlRegion", regionMove); + $elem.unbind("mouseup.dlRegion", regionEnd); + // clip and transform + clickRect.clipTo(picRect); + clickRect.adjustDiv($regionDiv); + data.regions.push($regionDiv); + // fn.redisplay(data); + return false; + }; + + // clear old handler (also ZoomDrag) + $scaler.unbind('.dlRegion'); + $elem.unbind('.dlRegion'); + // bind start zoom handler + $scaler.one('mousedown.dlRegion', regionStart); + }, + + // remove the last added region + "removeRegion" : function (data) { + var $regionDiv = data.regions.pop(); + $regionDiv.remove(); + // fn.redisplay(data); + }, + + // add a region programmatically + "addRegion" : function(data, pos, url) { + // TODO: backlink mechanism + if (pos.length === 4) { + // TODO: trafo + var $regionDiv = $('<div class="region" style="display:none"/>'); + $regionDiv.attr("id", "region" + i); + var regionRect = geom.rectangle(pos[0], pos[1], pos[2], pos[3]); + regionRect.adjustDiv($regionDiv); + if (!data.regions) { + data.regions = []; + } + data.regions.push($regionDiv); + } + } + }; + + var addRegion = actions.addRegion; + + var realizeRegions = function (data) { + // create regions from parameters + var settings = data.settings; + var rg = settings.rg; + var regions = rg.split(","); + for (var i = 0; i < regions.length ; i++) { + var pos = regions.split("/", 4); + // TODO: backlink mechanism + var url = paramString.match(/http.*$/); + addRegion(data, pos, url); + } + }; + + // display current regions + var renderRegions = function (data) { + var regions = data.regions; + for (var i = 0; i < regions.length; i++) { + var region = regions[i]; + if (data.zoomArea.containsPosition(region)) { + var rpos = data.imgTrafo.transform(region); + console.debug("renderRegions: rpos=", rpos); + // create region + var $regionDiv = $('<div class="region" style="display:none"/>'); + $regionDiv.attr("id", "region" + data.regions.length); + $elem.append($regionDiv); + rpos.adjustDiv($regionDiv); + } + } + }; + + var serializeRegions = function (data) { + if (data.regions) { + settings.rg = ''; + for (var i = 0; i < data.regions.length; i++) { + if (i) { + settings.rg += ','; + } + settings.rg += + cropFloat(data.regions[i].x).toString() + '/' + + cropFloat(data.regions[i].y).toString() + '/' + + cropFloat(data.regions[i].width).toString() + '/' + + cropFloat(data.regions[i].height).toString(); + } + } + }; + + // export constructor functions to digilib plugin + var init = function (digilibdata) { + console.log('initialising regions plugin. data:', digilibdata); + data = digilibdata; + data.regions = []; + // setup geometry object + geom = data.plugins.geometry.init(); + // add buttons and actions from this plugin + $.extend(this.buttons, buttons); + $.extend(this.actions, actions); + var buttonSettings = data.settings.buttonSettings.fullscreen; + // configure buttons through digilib "regionSet" option + var buttonSet = data.settings.regionSet || regionSet; + // set regionSet to [] or '' for no buttons (when showing regions only) + if (buttonSet.length && buttonSet.length > 0) { + buttonSettings['regionSet'] = buttonSet; + buttonSettings.buttonSets.push('regionSet'); + } + fn = this.fn; + // console.log(data.settings.buttonSettings.fullscreen.buttonSets); + return { + }; + }; + if ($.fn.digilib == null) { + $.error("jquery.digilib.regions must be loaded after jquery.digilib!"); + } else { + $.fn.digilib('plugin', {name : 'regions', init : init}); + } +})(jQuery);