Changeset 343


Ignore:
Timestamp:
06/18/12 12:51:58 (13 years ago)
Author:
hendrikvanantwerpen
Message:

[Client] Changed store and pages to use CouchDB.
[Client] Disabled login for now, have to figure out some more details about CouchDB.

Location:
Dev/branches/rest-dojo-ui/client
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui/client/db/db.js

    r328 r343  
    9393            req('PUT','/rft')
    9494           
    95             log("Setting _security on database 'rft'");
     95            /*log("Setting _security on database 'rft'");
    9696            req('PUT','/rft/_security', securityDoc);
    9797
     
    103103                log("Creating user 'rft_admin' with password 'Welkom01'");
    104104                req('PUT','/_users/org.couchdb.user:rft_admin', rft_adminDoc);
    105             });
     105            });*/
    106106        };
    107107
  • Dev/branches/rest-dojo-ui/client/db/docs/rft/_design/default.json

    r328 r343  
    44    "validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) { if (oldDoc && oldDoc.published) { throw({forbidden: 'Published documents cannot be modified.'}); } }",
    55    "views": {
     6        "by_type": {
     7            "map": "function(doc) { emit(doc.type, doc); }"
     8        },
    69        "unpublished": {
    710            "map": "function(doc) { if ( doc.type == 'Survey' && !doc.published ) { emit(doc._id, doc); } }"
  • Dev/branches/rest-dojo-ui/client/rft/api.js

    r311 r343  
    55            this.post = function(path,args) {
    66                var dfd = xhr.post({
    7                     url: "../server/api.php/"+path,
     7                    url: "../server/api/"+path,
    88                    handleAs: "json",
    99                    headers: {
  • Dev/branches/rest-dojo-ui/client/rft/auth.js

    r274 r343  
    77            function post(path,args) {
    88                return xhr.post({
    9                     url: "../server/api.php"+path,
     9                    url: "../server/api"+path,
    1010                    handleAs: "json",
    1111                    headers: {
     
    1818            self.login = function(username,password) {
    1919                var d = new Deferred();
    20                 post("/login",
     20                /*post("/login",
    2121                {
    2222                    email:username,
     
    2828                },function(){
    2929                    d.reject();
    30                 });
     30                });*/
     31                d.resolve();
    3132                return d.promise;
    3233            };
     
    3435            self.restore = function() {
    3536                var d = new Deferred();
    36                 post("/login",{})
     37                /*post("/login",{})
    3738                .then(function(data) {
    3839                    currentUser = data;
     
    4041                },function(){
    4142                    d.reject();
    42                 });
     43                });*/
     44                d.resolve();
    4345                return d.promise;
    4446           
     
    4749            self.register = function(username,password) {
    4850                var d = new Deferred();
    49                 post("/register",
     51                /*post("/register",
    5052                {
    5153                    email:username,
     
    5759                },function(){
    5860                    d.reject();
    59                 });
     61                });*/
     62                d.resolve();
    6063                return d.promise;
    6164            };
  • Dev/branches/rest-dojo-ui/client/rft/pages/questions.js

    r316 r343  
    88            },
    99            onVisit: function() {
    10                 this._store = store.getStore('Question');
    1110                this._list = new AccordionList({
    1211                    actions: {
    1312                        'Edit': lang.hitch(this,'_editQuestion')
    1413                    },
    15                     idProperty: this._store.idProperty,
     14                    idProperty: store.idProperty,
    1615                    categoryProperty: 'category',
    1716                    titleProperty: 'title'
     
    2120            },
    2221            _refresh: function() {
    23                 Deferred.when(this._store.query())
     22                Deferred.when(store.query('_design/default/_view/by_type',{key: 'Question'}))
    2423                .then(lang.hitch(this,function(items){
    2524                    this._list.setItems(items);
     
    2726            },
    2827            onNewQuestion: function() {
    29                 Deferred.when( this._store.add({}) )
     28                Deferred.when(store.add({type:'Question'}))
    3029                .then(lang.hitch(this,function(question){
    3130                    this._editQuestion(question);
     
    3938            onSaveQuestion: function(evt) {
    4039                var value = this.questionWidget.get('value');
    41                 Deferred.when( this._store.put(value) )
     40                Deferred.when(store.put(value))
    4241                .then(lang.hitch(this,function(){
    4342                    this.questionDialog.hide();
  • Dev/branches/rest-dojo-ui/client/rft/pages/survey.js

    r316 r343  
    55        return declare('rft.pages.survey',[_Page],{
    66            object: null,
    7             postCreate: function() {
    8                 this.inherited(arguments);
    9                 this._surveyStore = store.getStore('Survey');
    10                 this._questionStore = store.getStore('Question');
    11             },
    127            onVisit: function() {
    138                if ( this.pageArgs.uid ) {
    14                     Deferred.when(this._surveyStore.get(this.pageArgs.uid))
     9                    Deferred.when(store.get(this.pageArgs.uid))
    1510                    .then(lang.hitch(this,function(obj){
    1611                        this.object = obj;
     
    2116                        this.creator.innerHTML = (obj && obj.email) || 'unknown';
    2217                    }));
    23                     Deferred.when(this._questionStore.query())
     18                    Deferred.when(store.query('_design/default/_view/by_type',{key:'Question'}))
    2419                    .then(lang.hitch(this,function(items){
    2520                        this._questionList = new AccordionList({
    26                             store: this._surveyStore,
    2721                            actions: {
    2822                                'Add': lang.hitch(this,'_addQuestion')
    2923                            },
    30                             idProperty: this._questionStore.idProperty,
    3124                            categoryProperty: 'category',
    3225                            titleProperty: 'title'
     
    4437            onSave: function(evt) {
    4538                lang.mixin(this.object,this.form.get('value'));
    46                 Deferred.when( this._store.put(this.object) )
     39                Deferred.when( store.put(this.object) )
    4740                .then(lang.hitch(this,function(obj){
    4841                    this.object = obj;
     
    5346                }));
    5447                event.stop(evt);
     48                evt.stopPropagation();
    5549                return false;
    5650            },
  • Dev/branches/rest-dojo-ui/client/rft/pages/surveys.js

    r303 r343  
    33        return declare('rft.pages.surveys',[_Page],{
    44            selectedObject: null,
    5             postCreate: function() {
    6                 this._store = store.getStore('Survey');
    7             },
    85            onVisit: function() {
    9                 this.grid.setStore(ObjectStore({objectStore: this._store}));
     6                this.grid.setStore(
     7                    ObjectStore({objectStore: store}),
     8                    "_design/default/_view/by_type",{key:'Survey'});
    109               
    1110                this.grid.on('rowclick',lang.hitch(this,function(evt){
     
    1615                this.grid.on('rowdblclick',lang.hitch(this,function(evt){
    1716                    var obj = evt.grid.getItem(evt.rowIndex);
    18                     content.goTo('/survey',{uid:obj.getUid()});
     17                    content.goTo('/survey',{uid:store.getIdentity(obj)});
    1918                }));
    2019                               
    2120                this.btnNew.on('click',lang.hitch(this,function(){
    22                     Deferred.when( this._store.add({'creator':auth.getUser()}) )
     21                    Deferred.when( store.add({type:'Survey',creator:auth.getUser()}) )
    2322                    .then(function(obj) {
    24                         content.goTo('/survey',{uid:obj.getUid()});
     23                        content.goTo('/survey',{uid:store.getIdentity(obj)});
    2524                    });
    2625                }));
     
    2827                this.btnEdit.on('click',lang.hitch(this,function(){
    2928                    if ( this.selectedObject ) {
    30                         content.goTo('/survey',{uid:this.selectedObject.getUid()});
     29                        content.goTo('/survey',{uid:store.getIdentity(this.selectedObject)});
    3130                    }
    3231                   
  • Dev/branches/rest-dojo-ui/client/rft/store.js

    r311 r343  
    1 define(['dojo/_base/lang','dojo/_base/array','dojo/_base/Deferred','dojo/store/JsonRest','./api'],
    2     function(lang,array,Deferred,JsonRest,api){
     1define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/Deferred','dojo/_base/xhr','dojo/json','dojo/store/util/QueryResults'],
     2    function(declare,lang,array,Deferred,xhr,JSON,QueryResults){
    33   
    4         var OBJ_TYPE = '__objectType';
    5         var OBJ_UID = '__objectUid';
    6         var REF_TYPE = '__referenceType';
    7         var REF_UID = '__referenceUid';
     4        var CouchStore = declare("rft.store", null, {
     5            /** dojo.Store implementation for CouchDB
     6             *
     7             * See for details on the REST API, the wiki
     8             * at http://wiki.apache.org/couchdb/HTTP_Document_API.
     9             */
     10                target: "",
     11                accepts: "application/json",
     12            idProperty: "_id",
     13            revProperty: "_rev",
     14            _responseIdProperty: "id",
     15            _responseRevProperty: "rev",
     16                constructor: function(options){
     17                        declare.safeMixin(this, options);
     18            },
     19                getIdentity: function(object){
     20                        return object[this.idProperty];
     21            },
     22                getRevision: function(object){
     23                        return object[this.revProperty];
     24            },
     25                get: function(id){
     26                var dfd = new Deferred();
     27                        xhr("GET", {
     28                                url: this.target + id,
     29                                handleAs: "json",
     30                                headers: {
     31                                Accept: this.accepts
     32                    }
     33                        }).then(function(result){
     34                    if ( result.error ) {
     35                        dfd.reject(result.reason);
     36                    } else {
     37                        dfd.resolve(result);
     38                    }
     39                }, function(err){
     40                    dfd.reject(err);
     41                });
     42                return dfd.promise;
     43            },
     44                put: function(object, options){
     45                        options = options || {};
    846
    9         var RftObject = function(values) {
    10             var self = this;
    11             lang.mixin(this,values);
    12             self.getUid = function() {
    13                 return self[OBJ_UID];
     47                var dfd = new Deferred();
     48                        var id = options.id ? options.id : this.getIdentity(object);
     49                        var hasId = typeof id != "undefined";
     50                        xhr(hasId ? "PUT" : "POST", {
     51                                    url: hasId ? this.target + id : this.target,
     52                                    postData: JSON.stringify(object),
     53                                    handleAs: "json",
     54                                    headers:{
     55                                            "Content-Type": "application/json",
     56                                            Accept: this.accepts
     57                                    }
     58                            }).then(function(result){
     59                    if ( result.error ) {
     60                        dfd.reject(result.reason);
     61                    } else {
     62                        object[this.idProperty] = result[this._responseIdProperty];
     63                        object[this.revProperty] = result[this._responseRevProperty];
     64                        dfd.resolve(object);
     65                    }
     66                }, function(err){
     67                    dfd.reject(err);
     68                });
     69                return dfd.promise;
     70            },
     71                add: function(object, options){
     72                return this.put(object,options);
     73            },
     74                remove: function(id,rev){
     75                var dfd = new Deferred();
     76                        xhr("DELETE",{
     77                                url: this.target + id,
     78                                    headers: {
     79                        'If-Match': rev
     80                    }
     81                        }).then(function(result){
     82                    if ( result.error ) {
     83                        dfd.reject(result.reason);
     84                    } else {
     85                        dfd.resolve();
     86                    }
     87                },function(err){
     88                    dfd.reject(err);
     89                });
     90                return dfd.promise;
     91            },
     92            query: function(query, options){
     93                        options = options || {};
     94
     95                var dfd = new Deferred();
     96                var queryOpts = {};
     97                if ( query === undefined ) {
     98                    query = '_all_docs';
     99                    queryOpts.include_docs = true;
     100                }
     101
     102                if (!lang.isString(query)) {
     103                    console.warn("Query must be a view name");
     104                }
     105
     106                // Standard options
     107                if (options.start >= 0) {
     108                    queryOpts.skip = options.start;
     109                }
     110                if (options.count >= 0) {
     111                    queryOpts.limit = options.count;
     112                }
     113                if (options.sort) {
     114                    if (options.sort[0]) {
     115                        if (options.sort[0].attribute && options.sort[0].attribute !== "key") {
     116                            console.warn("Can only sort on key");
     117                        }
     118                        if (options.sort[0].descending) {
     119                            queryOpts.descending = true;
     120                        }
     121                    }
     122                    if (options.sort.length > 1) {
     123                        console.warn("multiple sort fields not supported");
     124                    }
     125                }
     126
     127                // Custom options
     128                if (options.key) {
     129                    queryOpts.key = options.key;
     130                } else if (options.keys) {
     131                    queryOpts.keys = options.keys;
     132                } else if (options.startkey || options.endkey) {
     133                    queryOpts.startkey = options.startkey;
     134                    queryOpts.endkey = options.endkey;
     135                }
     136
     137                for (var qp in queryOpts) {
     138                    queryOpts[qp] = JSON.stringify(queryOpts[qp]);
     139                }
     140                            query += '?' + xhr.objectToQuery(queryOpts);
     141
     142                        xhr("GET", {
     143                                url: this.target + query,
     144                                handleAs: "json",
     145                                headers: {
     146                        Accept: this.accepts
     147                    }
     148                        }).then(function(result){
     149                    if (result.error) {
     150                        dfd.reject(result.reason);
     151                    } else  {
     152                        dfd.resolve(
     153                            array.map(result.rows,
     154                                      function(result){ return result.value; }));
     155                    }
     156                },function(err){
     157                    dfd.reject(err);
     158                });
     159                        return QueryResults(dfd.promise);
    14160            }
    15         };
    16    
    17         var rftStoreCtor = function(objectType) {
    18             var jsonStore = new JsonRest({
    19                 target:"../server/api.php/data/"+objectType+"/",
    20                 idProperty: OBJ_UID
    21             });
    22             var errHandler = api.defaultErrorHandler;
    23             return lang.delegate(jsonStore,{
    24                 query: function(query, directives){
    25                     return Deferred.when( jsonStore.query(query,directives), function(results){
    26                         return array.map(results,function(result){
    27                             return new RftObject(typeof result == "object" ? result : object);
    28                         });
    29                     },errHandler);
    30                 },
    31                 get: function(id, directives){
    32                     return Deferred.when( jsonStore.get(id,directives), function(result){
    33                         return new RftObject(typeof result == "object" ? result : object);
    34                     },errHandler);
    35                 },
    36                 add: function(object, directives){
    37                     return Deferred.when( jsonStore.add(object,directives), function(result){
    38                         return new RftObject(typeof result == "object" ? result : object);
    39                     },errHandler);
    40                 },
    41                 put: function(object, directives){
    42                     return Deferred.when( jsonStore.put(object,directives), function(result) {
    43                         return new RftObject(typeof result == "object" ? result : object);
    44                     },errHandler);
    45                 },
    46                 remove: function(id, directives){
    47                     return jsonStore.remove(id,directives);
    48                 }
    49             });
    50         };
    51    
    52         var storeCache = {};
    53    
    54         return {
    55             getStore: function(objectType) {
    56                 if ( !storeCache[objectType] ) {
    57                     storeCache[objectType] = new rftStoreCtor(objectType);
    58                 }
    59                 return storeCache[objectType];
    60             },
    61             dereference: function(object) {
    62                 if ( object[REF_TYPE] ) {
    63                     return this.getStore(object[REF_TYPE]).get(object[REF_UID]);
    64                 }
    65                 return object;
    66             }
    67         };
     161        });
     162
     163        return new CouchStore({target: '/couchdb/rft/'});
    68164
    69165    });
  • Dev/branches/rest-dojo-ui/client/rft/ui/QuestionWidget.js

    r305 r343  
    99            name: '',
    1010            value: null,
    11             _type: null,
     11            _scale: null,
    1212            _widgetCache: null,
    1313            constructor: function() {
     
    1818            postCreate: function() {
    1919                this._resetValue = this.value;
    20                 this.typeSelector.set('disabled', this.mode == 'edit');
     20                this.scaleSelector.set('disabled', this.mode == 'edit');
    2121            },
    2222            _setValueAttr: function(value) {
    2323                this.value = value;
    24                 this._onTypeChange(value.type || 'string');
     24                this._onTypeChange(value.scale || 'string');
    2525                this.ourForm.set('value',value);
    2626            },
     
    3030                return this.value;
    3131            },
    32             _onTypeChange: function(type) {
    33                 if ( this._type == type ) return;
    34                 this._type = type;
    35                 domConstruct.empty(this.typeDetails);
    36                 var widget = this._getTypeWidget(type);
    37                 widget && widget.placeAt(this.typeDetails,'only');
     32            _onTypeChange: function(scale) {
     33                if ( this._scale == scale ) return;
     34                this._scale = scale;
     35                domConstruct.empty(this.scaleDetails);
     36                var widget = this._getTypeWidget(scale);
     37                widget && widget.placeAt(this.scaleDetails,'only');
    3838            },
    39             _getTypeWidget: function(type) {
    40                 var widget = this._widgetCache[type];
     39            _getTypeWidget: function(scale) {
     40                var widget = this._widgetCache[scale];
    4141                if (!widget) {
    42                     switch(type) {
     42                    switch(scale) {
    4343                        case 'string':
    4444                            widget = new TextBox({
     
    5959                                name: 'answers',
    6060                                mode: this.mode,
    61                                 allowMultiple: type == 'multipleChoice'
     61                                allowMultiple: scale == 'multipleChoice'
    6262                            });
    6363                            break;
    6464                    }
    65                     this._widgetCache[type] = widget;
     65                    this._widgetCache[scale] = widget;
    6666                }
    6767                return widget;
  • Dev/branches/rest-dojo-ui/client/rft/ui/templates/QuestionWidget.html

    r303 r343  
    1212        </fieldset>
    1313        <fieldset>
    14             <select data-dojo-type="dijit.form.Select" data-dojo-attach-point="typeSelector" data-dojo-attach-event="onChange:_onTypeChange" name="type">
     14            <select data-dojo-type="dijit.form.Select" data-dojo-attach-point="scaleSelector" data-dojo-attach-event="onChange:_onTypeChange" name="scale">
    1515                <option value="string" selected>String</option>
    1616                <option value="text">Text</option>
     
    1818                <option value="multipleChoice">Multiple choice</option>
    1919            </select>
    20             <div data-dojo-attach-point="typeDetails"></div>
     20            <div data-dojo-attach-point="scaleDetails"></div>
    2121        </fieldset>
    2222    </form>
Note: See TracChangeset for help on using the changeset viewer.