Changeset 171:32a6b9eea546

Show
Ignore:
Timestamp:
08/21/08 20:25:41 (2 years ago)
Author:
rgrp
Branch:
default
convert_revision:
svn:10edda23-d834-0410-9182-b00384516d49/trunk@181
Message:

[js][m]: more work on threadfactlets_controller (test esp.) plus created factletsearch_controller and added support for handling append click event.

Location:
microfacts/public
Files:
1 added
5 modified

Legend:

Unmodified
Added
Removed
  • microfacts/public/behaviour/app/controllers/factletsearch_controller.js

    r161 r171  
    77        this.searchResults = $$('.factlet-search-results')[0]; 
    88        // list of factlet ids currently being shown 
    9         this.currentList = []; 
     9        this.currentFactletIds = []; 
     10        this.currentFactlets = new Hash({}); 
    1011    }, 
    1112     
     
    1920    onDataChange: function (data) { 
    2021        // TODO: check this is factlet data and relevant to us .... 
    21         if (this.currentList.contains(data.id)) { 
     22        if (this.currentFactletIds.contains(data.id)) { 
    2223            this.createSearchResultLi(data); 
    2324        } 
     
    2829        var html = ''; 
    2930        html += factletData.title; 
    30         html += ' &mdash; <a class="factlet-append" href="#TODO-appendFactletAction">&laquo; add</a>'; 
     31        html += ' &mdash; <a class="factlet-append" href="#TODO-appendFactletAction">&laquo; append</a>'; 
    3132        newLi.innerHTML = html; 
    3233        this.searchResults.appendChild(newLi); 
     
    3637        console.log('FactletSearchController: updateSearchResults'); 
    3738        console.log(data); 
    38         this.currentList = data; 
     39        this.currentFactletIds = data; 
     40        this.currentFactlets.empty(); 
    3941        // empty current list of results 
    4042        // assume only one for present 
    4143        this.searchResults.innerHTML = ''; 
    4244        // data will be list of factlet ids 
    43         data.each(function(factletId) { 
     45        this.currentFactletIds.each(function(factletId) { 
    4446            var factlet = DomainObject.Resource('Factlet', factletId); 
     47            this.currentFactlets[factletId] = factlet; 
    4548            // if object is already  
    4649            if (factlet.data) { 
     
    5558    controls: { 
    5659        '#factlet-search-submit click': function (e) { 
     60            console.log('Factlet Search Button clicked.'); 
    5761            var evt = new Event(e); 
    5862            evt.stop(); 
    59             console.log('Factlet Search Button clicked.'); 
    6063            var requestOptions = { 
    6164                onSuccess: this.updateSearchResults.bind(this) 
     
    6669            var query = { "q": data }; 
    6770            request.post(url, encodeURIComponent(JSON.encode(query))); 
     71        }, 
     72        '.factlet-append click': function(e) { 
     73            var evt = new Event(e); 
     74            evt.stop(); 
     75            var elemId = evt.target.parentNode.id; 
     76            var factletId = elemId.split('-').getLast().toInt(); 
     77            this.fireEvent('factletAppend', this.currentFactlets[factletId]); 
    6878        } 
     79 
    6980    } 
    7081}); 
  • microfacts/public/behaviour/app/controllers/threadfactlets_controller.js

    r163 r171  
    22    Extends: Controller, 
    33 
    4 //    factletSummaryTemplate = [ 
    5 //'<div id="factlet-<%= c.id %>" class="factlet">', 
    6 //'  <%= this.partial({url: baseurl+'/title.ejs'}) %>', 
    7 //'</div>', 
    8 //    ].join('\n'); 
     4    factletSummaryTemplate: [ 
     5'<div id="factlet-<%= c.id %>" class="factlet">', 
     6'<h3 class="factlet-title"><%= c.title %></h3>', 
     7'</div>' 
     8    ].join('\n'), 
    99     
    1010    initialize: function (element) { 
    1111        this.parent.apply(this, arguments); 
    12 //        this.model = DomainObject.Resource('Factlet', this.id()); 
    13 //        this.views.push(new FillView()); 
    14 //        this.views.push(new PageTitleView('Factlets')); 
    15 //        this.addDefaultListeners(); 
    1612        this.factlets = []; 
    1713        this.selectedFactletId = null; 
    1814    }, 
    1915 
    20     // Name is relative to app/views directory, without an .ejs extension. 
    21     renderView: function (name, data) { 
    22         // return new EJS({url: '/app/views/'+name+'.ejs'}).render({'c': data}); 
    23     }, 
    24  
    25     generateFactletElement: function (factlet) { 
    26         // var html = this.renderView('factlet/summary', factlet.data); 
    27         html = null; 
     16    generateFactletHtml: function (factletData) { 
     17        var template = new EJS({text: this.factletSummaryTemplate}); 
     18        console.log(factletData); 
     19        var html = template.render({'c': factletData}); 
    2820        return html; 
    2921    }, 
     
    4133    }, 
    4234 
    43     onThreadStart: function () { 
     35    onThreadStart: function (thread, threadFactlets) { 
    4436        console.log("ThreadFactletsController saw threadStart event"); 
    45         this.thread = arguments[0]; 
    46         this.factlets = arguments[1]; 
    47         this.factletsKeyedById = new Hash({}); 
    48         this.factletDivsKeyedById = new Hash({}); 
    49         this.factlets.each(function(fct) { 
    50             this.factletsKeyedById[fct.id] = fct; 
    51             // now associate each factlet with its element 
    52             var divId = 'factlet-' + fct.id; 
    53             var fctdiv = $(divId); 
    54             this.factletDivsKeyedById[fct.id] = fctdiv; 
    55         }, this); 
     37        // no specific initializing here so just do update 
     38        this.onThreadUpdate(thread, threadFactlets); 
    5639        console.log('ThreadFactletsController: end'); 
    5740    }, 
    5841 
    59     onRemoveSelectedFactlet: function() { 
    60         this.thread.factlets.erase(this.selectedFactletId); 
    61         this.thread.save(); 
     42    onThreadUpdate: function(thread, threadFactlets) { 
     43        console.log(arguments); 
     44        this.thread = thread; 
     45        this.factlets = threadFactlets; 
     46        this.factletsKeyedById = new Hash({}); 
     47        this.factletDivsKeyedById = new Hash({}); 
     48        this.element.innerHTML = ''; 
     49        this.factlets.each(function(fct) { 
     50            this.factletsKeyedById[fct.id] = fct; 
     51            // Create ourselves so work on update too 
     52            var dummy = document.createElement('div'); 
     53            console.log(fct.data); 
     54            fctdivHTML = this.generateFactletHtml(fct.data); 
     55            dummy.innerHTML = fctdivHTML; 
     56            var fctdiv = dummy.childNodes[0]; 
     57            this.element.appendChild(fctdiv); 
     58            // now associate each factlet with its element 
     59            // var divId = 'factlet-' + fct.id; 
     60            // var fctdiv = $(divId); 
     61            this.factletDivsKeyedById[fct.id] = fctdiv; 
     62        }, this); 
    6263    }, 
    6364 
  • microfacts/public/behaviour/load.js

    r169 r171  
    197197    type: "js", 
    198198    path: "app/microfacts.js?" + Math.random(), 
    199     requires: ['domainobject', 'view', 'controller', 'microfactscontroller', 'factletcontroller', 'threadfactletscontroller', 'mapview', 'timelineview', 'geochronopagecontroller'], 
     199    requires: ['domainobject', 'view', 'controller', 'microfactscontroller', 'factletcontroller', 'threadfactletscontroller', 'mapview', 'timelineview', 'geochronopagecontroller', 'threadpagecontroller'], 
    200200    varName: "Microfacts" 
    201201}); 
  • microfacts/public/test/index.html

    r169 r171  
    5353  <script type="text/javascript" src="spec/model_spec.js?123"></script> 
    5454  <script type="text/javascript" src="spec/controller_spec.js?123"></script> 
     55 
     56  <!-- views --> 
    5557  <script type="text/javascript" src="spec/view_spec.js?123"></script> 
     58  <script type="text/javascript" src="spec/threadfactletscontroller_spec.js?123"></script> 
     59  <script type="text/javascript" src="spec/factletsearch_controller_spec.js?123"></script> 
     60 
    5661  <script type="text/javascript" src="spec/mapview_spec.js?123"></script> 
    5762  <script type="text/javascript" src="spec/timelineview_spec.js?123"></script> 
     63 
     64  <!-- (page) controllers --> 
    5865  <script type="text/javascript" src="spec/geochronopage_controller_spec.js?123"></script> 
    59   <!-- 
    60   <script type="text/javascript" src="spec/threadfactletscontroller_spec.js?123"></script> 
    61   --> 
    6266 
    6367<!-- 
     
    101105    </div> 
    102106  </div> 
     107 
     108  <div id="factlet-search"> 
     109    <ul class="factlet-search-results"></ul> 
     110  </div> 
    103111  
    104112      <script type="text/javascript" charset="utf-8"> 
  • microfacts/public/test/spec/threadfactletscontroller_spec.js

    r163 r171  
    1 var thread = { 
     1var threadData = { 
    22    'id' : 2, 
    33    'title': 'Blah', 
     
    55}; 
    66 
     7var thread = { 
     8    'id': threadData['id'], 
     9    'data': threadData 
     10}; 
     11 
    712var threadFactlets = [ 
    8     { 
     13    {  
    914        'id': 101, 
    10         'title': 'My Factlet', 
    11         'location' : { 
    12             "type": "Point", 
    13             "coordinates": [51.50842, -0.12553] 
     15        'data': { 
     16            'id': 101, 
     17            'title': 'My Factlet', 
     18            'location' : { 
     19                "type": "Point", 
     20                "coordinates": [51.50842, -0.12553] 
     21            } 
    1422        } 
    1523    }, 
    16     { 
     24    {  
    1725        'id': 102, 
    18         'title': 'Your Factlet', 
    19         'location' : { 
    20             "type": "Point",  
    21             "coordinates": [0, 0] 
     26        'data': { 
     27            'id': 102, 
     28            'title': 'Your Factlet', 
     29            'location' : { 
     30                "type": "Point",  
     31                "coordinates": [0, 0] 
     32            } 
    2233        } 
    2334    } 
    2435]; 
     36 
    2537 
    2638describe('ThreadFactletsController', { 
     
    5163    }, 
    5264 
    53     'should manage generateFactletElement': function() { 
    54         var html = ctrl.generateFactletElement(threadFactlets[0]); 
    55         console.log(html); 
     65    'test generateFactletHtml': function() { 
     66        var html = ctrl.generateFactletHtml(threadFactlets[0]); 
     67        expect(html.substring(0, 21)).should_be('<div id="factlet-101"'); 
    5668    } 
    5769 
    58  
    5970});