前回作ったプラグインのおさらい。
QueryStatusHelperPlugin/setup.py:
from setuptools import setup
setup(
name='QueryStatusHelperPlugin',
entry_points = {
'trac.plugins': [
'QueryStatusHelperPlugin = query.doubleclick',
],
},
)
QueryStatusHelperPlugin/query/__init__.py:
from query import *QueryStatusHelperPlugin/query/doubleclick.py:
from trac.core import Component, implements
from trac.web.api import ITemplateStreamFilter
from trac.web.chrome import add_script, ITemplateProvider
import pkg_resources
class Checkbox(Component):
implements(ITemplateProvider, ITemplateStreamFilter)
#ITemplateStreamFilter methods
def filter_stream(self, req, method, filename, stream, data):
add_script(req, 'querystatushelper/js/enabler.js')
return stream
# ITemplateProvider methods
def get_htdocs_dirs(self):
return [('querystatushelper', pkg_resources.resource_filename('query', 'htdocs'))]
def get_templates_dirs(self):
return []
QueryStatusHelperPlugin/query/htdocs/js/enabler.js:
jQuery(document).ready(function() {
jQuery('#label_0_status').dblclick(function() {
jQuery('input[name="0_status"]').each(function() {
checked = $(this).attr("checked")
$(this).attr("checked",!checked)
})
})
});おさらいは以上。
さて、どれから手をつけようか。
add_script の前に if文を入れて、必要なとき、つまりクエリページにだけ add_script するようにする。
条件は……。「クエリページだけ」とは? さっそくブレークポイントを張って、使えそうな変数を探す:
filter_stream(req, method, filename, stream, data)やりたいことは「適切なチェックボックスにdblclickハンドラを追加したい」ので、その「適切なチェックボックス」を生成しているやつを特定したい。テンプレートである query.html をみると:
Return a filtered Genshi event stream, or the original unfilteredstream if no match.
`req` is the current request object, `method` is the Genshi rendermethod (xml, xhtml or text), `filename` is the filename of the templateto be rendered, `stream` is the event stream and `data` is the data forthe current template.
${prjectloc}/python/System Libs/site-packages/trac/ticket/templates/query.html(104):
<py:when test="field.type == 'radio'">
<py:for each="option in field.options">
<input type="checkbox" id="_${n_field_name}_$option" name="${n_field_name}"
value="$option"
checked="${((constraint['mode'] == '') == (option in constraint['values'])) or None}" />
<label for="_${n_field_name}_$option" class="control">${option or 'none'}</label>
</py:for>
</py:when>
……ビンゴ。if(filename=='query.html'): でラップしてやればいい。
次回は、残りの課題をいくつかやっつける。