========
Adapters
========

This functionality is added to plone content types using adapters.

Let's start by logging in as a manager.

  >>> self.setRoles(['Manager'])

JSONDetails
-----------

This class is used to get detail from a certain object. Let's create a Document.

  >>> document = portal.invokeFactory('Document', id='document')
  >>> portal[document]
  <ATDocument at /plone/document>

The basic details should return the following.

  >>> from Products.TinyMCE.adapters.interfaces.JSONDetails import IJSONDetails
  >>> object = IJSONDetails(portal[document])
  >>> object.getDetails()
  '{"thumb": "", "description": "", "anchors": [], "title": "document"}'

Let's set some more details like description and body text.

  >>> portal[document].setDescription('Test')
  >>> portal[document].setText(u'<p><a name="anchor">anchor</a></p>',
  ...                          mimetype='text/html')

The details will now contain a bit more info.

  >>> object.getDetails()
  '{"thumb": "", "description": "Test", "anchors": ["anchor"], "title": "document"}'

We can also get the details of an image object.

  >>> image = portal.invokeFactory('Image', id='image')
  >>> portal[image]
  <ATImage at /plone/image>

  >>> import os
  >>> imgdata = open(os.path.join(os.path.dirname(__file__), 'sample.png'))
  >>> portal[image].setImage(imgdata)

The details will now also include the thumbnail url and the imagescales.

  >>> object = IJSONDetails(portal[image])
  >>> object.getDetails()
  '{"scales":..."thumb": "http://nohost/plone/resolveuid/.../@@images/image/thumb"...}'

The first scale is always the original size:

  >>> object.getDetails()
  '{"scales": [{"size": [52, 43], "value": "", "title": "Original"}...

JSONFolderListing
-----------------

The folder listing is used in the link and image drawers to show the contents
of a folder. Let's see what items our current siteroot has.

  >>> from zope.component import getUtility
  >>> from Products.TinyMCE.interfaces.utility import ITinyMCE
  >>> utility = getUtility(ITinyMCE)
  >>> linkable_portal_types = utility.linkable.split('\n')
  >>> linkable_portal_types.extend(utility.containsobjects.split('\n'))
  >>> from Products.TinyMCE.adapters.interfaces.JSONFolderListing import \
  ...     IJSONFolderListing
  >>> object = IJSONFolderListing(portal)
  >>> object.getListing(filter_portal_types=linkable_portal_types,\
  ...                   rooted=False,\
  ...                   document_base_url='http://nohost/plone',\
  ...                   upload_type='File',\
  ...                  )
  '{"parent_url": "", "path": [...],..."items": [...]}'

Let's create some more content to get breadcrumbs.

  >>> folder = portal.invokeFactory('Folder', id='folder')
  >>> folder_object = portal[folder]
  >>> document = folder_object.invokeFactory('Document', id='document')

When we call the getListing method on the document we should get the listing of
its parent.

  >>> object = IJSONFolderListing(folder_object.get(document))
  >>> object.getListing(filter_portal_types=[],\
  ...                   rooted='False',\
  ...                   document_base_url='http://nohost/plone/folder',\
  ...                  )
  '{"parent_url": "http://nohost/plone"...}'

We can also select rooted so we don't get all the breadcrumbs.

  >>> object.getListing(filter_portal_types=[],\
  ...                   rooted='True',\
  ...                   document_base_url='http://nohost/plone/folder',\
  ...                  )
  '{..."path": [{"url": "http://nohost/plone/folder"...}'

JSONSearch
----------

The json search is used the look for content types within the portal. Let's see
if we can find some items containing the searchterm 'Events'

  >>> linkable_portal_types = utility.linkable.split('\n')
  >>> linkable_portal_types.extend(utility.containsobjects.split('\n'))
  >>> from Products.TinyMCE.adapters.interfaces.JSONSearch import IJSONSearch
  >>> object = IJSONSearch(portal)
  >>> object.getSearchResults(filter_portal_types=linkable_portal_types,\
  ...                         searchtext='Events',\
  ...                        )
  '{..."title": "Events", "url": "http://nohost/plone/events"...}'

Save
----

This class is used to save a document using json. Let's try and set some value.

  >>> from Products.TinyMCE.adapters.interfaces.Save import ISave
  >>> object = ISave(folder_object[document])
  >>> object.save(fieldname='text',\
  ...             text='<p>test</p>',\
  ...            )
  'saved'

The document should now contain our new text.

  >>> folder_object[document].getText()
  '<p>test</p>'

Upload
------

